I\'m trying to write a constructor that supplements this program. The TeamClient
ID: 3773933 • Letter: I
Question
I'm trying to write a constructor that supplements this program. The TeamClient program can not be edited. It needs to include a constructor, accessor, and mutator, and methods toString and equals.
public class TeamClient
{
public static void main( String [] args )
{
Team t1 = new Team( "Orioles" );
Team t2 = new Team( "Yankees" );
System.out.println( "The name of team #1 is " + t1.getName( ) );
System.out.println( "Team #2: " + t2.toString( ) );
if ( t1.equals( t2 ) )
System.out.println( "Original teams #1 and #2 are identical" );
else
System.out.println( "Original teams #1 and #2 are different" );
System.out.println( " Setting team #2 name to Orioles" );
t2.setName( "Orioles" );
if ( t1.equals( t2 ) )
System.out.println( "Original team #1 and modified team #2 are identical" );
else
System.out.println( "Original team #1 and modified team #2 are different" );
}
}
My constructor so far which doesn't seem to be working. The main program gives the error
TeamClient.java:21: error: cannot find symbol
t2.setName( "Orioles" );
^
symbol: method setName(String)
location: variable t2 of type Team
1 error
public class Team {
private String name;
public Team()
{
name="";
}
public Team(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public String toString()
{
return "Name: "+name;
}
public boolean equals(Object o)
{
Team t=(Team)o;
return name.equals(t.getName());
}
}
Explanation / Answer
Team.java
public class Team {
//Declaring instance variable
private String name;
//Parameterized constructor
public Team(String name) {
this.name = name;
}
//getter and setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//This method compare the two Team Object contents
public boolean equals(Team tm) {
String name1=this.name;
String name2=tm.getName();
/* If the two Team Object names are same then return true
* if not return false
*/
if(name1.equals(name2))
return true;
else
return false;
}
//This toString() method will display the contents of an object
@Override
public String toString() {
return "Team "+ name;
}
}
______________________
TeamClient.java
public class TeamClient {
public static void main(String[] args) {
//Creating a Team1 Object by passing the name as argument
Team t1 = new Team( "Orioles" );
//Creating a Team2 Object by passing the name as argument
Team t2 = new Team( "Yankees" );
//Displaying the name of the team1
System.out.println( "The name of team #1 is " + t1.getName( ) );
//Displaying the name of the team2
System.out.println( "Team #2: " + t2.toString( ) );
//Comparing the names of the two teams
if ( t1.equals( t2 ) )
System.out.println( "Original teams #1 and #2 are identical" );
else
System.out.println( "Original teams #1 and #2 are different" );
System.out.println( " Setting team #2 name to Orioles" );
//Setting the name of the team2 to "Orioles"
t2.setName( "Orioles" );
//Comparing the names of the two teams
if ( t1.equals( t2 ) )
System.out.println( "Original team #1 and modified team #2 are identical" );
else
System.out.println( "Original team #1 and modified team #2 are different" );
}
}
_____________________________
Output:
The name of team #1 is Orioles
Team #2: Team Yankees
Original teams #1 and #2 are different
Setting team #2 name to Orioles
Original team #1 and modified team #2 are identical
______Thank You