Create a class called Team that will be used for creating team objects, the class must have the
following data members and methods:
Data Members:
Name,the name of the team
HomeGround, the name of the Home stadium of the team
Goals, the number of goals that the team scores during the match
Choose appropriate types for the data members
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(), since a team can score 0 to 4 goals, this helper method must assign the
Goals data member to a random number in that range
getName(), getHomeGround(), getGoals(), create get methods for each data member
package com.task;
import java.util.Random;
public class Team {
private String name;
private String homeGround;
public String getName() {
return name;
}
public String getHomeGround() {
return homeGround;
}
public Integer getGoals() {
return goals;
}
private Integer goals;
public Team() {
name = "Team Name";
homeGround = "Stadium Name";
goals = 0;
}
public Team(String name, String homeGround) {
this.name = name;
this.homeGround = homeGround;
goals = 0;
}
public void scoreGoals() {
goals = new Random().nextInt(5);
}
}
Comments
Leave a comment