a class, Team.java, that has the following: * two private instance variables: a String named color and an int named score. * a default constructor with no parameters that initializes color to "Red" and score to zero. * a constructor that takes two parameters (one for each instance variable) and sets the values of the instance variables. * a public getter and setter method for each instance variable. * a toString method that returns the color and score in the following format: "Red Team with 0 points."
I need to write a jave program to keep track of a team sport.
Write a class, Team.java, that has the following:
* two private instance variables: a String named color and an int named score.
* a default constructor with no parameters that initializes color to "Red" and score to zero.
* a constructor that takes two parameters (one for each instance variable) and sets the values of the instance variables.
* a public getter and setter method for each instance variable.
* a toString method that returns the color and score in the following format: "Red Team with 0 points."
// Java Code:
import java.io.*;
class Team{
//declaring the private variables
private String color;
private int score;
//default constructor
Team()
{
this.color="Red";
this.score=0;
}
//parameterized constructor
Team(String c, int s)
{
this.color=c;
this.score=s;
}
//public getter method to get color
public String getColor()
{
return color;
}
//public getter method to get score
public int getScore()
{
return score;
}
// public setter method to set color
public void setColor(String c)
{
this.color=c;
}
//public setter method to set score
public void setScore(int s)
{
this.score=s;
}
//toString method
public String toString()
{
return color+" Team with "+score+" points.";
}
//the working of the constructors and the methods are demonstrated using the main function
public static void main(String[] args)
{
//invoking the default constructor
Team t1=new Team();
//invoking the parameterized constructor
Team t2=new Team("Pink",23);
//using the getter functions
System.out.println(t1.getColor());
//using the to string method
System.out.println(t1);
System.out.println(t2);
//using setter function
t1.setColor("Green");
t2.setScore(46);
//using the toString method after the values have been changed using the setter method
System.out.println(t1);
System.out.println(t2);
}
}
OUTPUT: (Please refer the comments in the java code to see the details regarding of the output)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images