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."
![](/static/compass_v2/shared-icons/check-mark.png)
// 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
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)