Create a Java application that will Create a class called Team that will be used for creating team objects.
Data Members:
Name, HomeGround, and Goals.
Methods:
A default constructor which will initialize all data members to proper initial values
An overloaded constructor which receives two arguments; a team name and home ground name and it should also initialize the goals to a proper initial value
scoreGoals() should be 0 to 4 goals which will choose random number in that range
getName(), getHomeGround(), getGoals(), create get methods for each data member
In the main function use your classes to create object and demonstrate their functionality.
Create two team object prompt the user to enter the team name and the home ground name of each. Create Match object initializing it with the two team objects created above. Simulate play by calling the PlayMatch method of the Match object. Note that the isDrawMatch method should be called to determine if match is draw and display different output
import java.util.Random;
public class Team {
private String name;
private String homeGround;
private int goals;
public Team() {
this("Unknown", "Unknown");
}
public Team(String name, String homeGround) {
this.name = name;
this.homeGround = homeGround;
goals = 0;
}
public void scoreGoals() {
goals = new Random().nextInt(5);
}
public String getName() {
return name;
}
public String getHomeGround() {
return homeGround;
}
public int getGoals() {
return goals;
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter a team name:");
String name = in.nextLine();
System.out.println("Enter a team home ground:");
String homeGround = in.nextLine();
Team one = new Team(name, homeground);
System.out.println("Enter a team name:");
name = in.nextLine();
System.out.println("Enter a team home ground:");
homeGround = in.nextLine();
Team two = new Team(name, homeground);
Match match = new Match(one, two);
match.playMatch();
if(match.isDrawMatch){
System.out.println("Draw");
}else{
System.out.println(match.getWinner());
}
}
}
Comments
Leave a comment