Run the following code and answer these 5 questions: 1. Bowler Name: Harry Merse
ID: 3794922 • Letter: R
Question
Run the following code and answer these 5 questions:
1. Bowler Name: Harry Merser Highest Score: 235.0 is different from
Bowler Name: Harry Merser Highest Score: 235.0 why??
2. Bowler Name: Harry Merser Highest Score: 235.0 is now the same as
Bowler Name: Harry Merser Highest Score: 235.0 why the change??
3. After changing b1's score
b1's state is: Bowler Name: Harry Merser Highest Score: 99.0
b2's state is: Bowler Name: Harry Merser Highest Score: 99.0
explain what happened
4. Bowler Name: Harry Merser Highest Score: 235.0 is different from
Bowler Name: Harry Merser Highest Score: 235.0 why??
5. Bowler Name: Harry Merser Highest Score: 235.0 is now the same as
Bowler Name: Harry Merser Highest Score: 235.0 why the change??
6. After changing b1's score
b1's state is: Bowler Name: Harry Merser Highest Score: 99.0
b2's state is: Bowler Name: Harry Merser Highest Score: 99.0
explain what happened
/**
* This is a simple class that will be
* used to illustrate the need for an equals( )
*method, a compareTo( ) method and a copy contructor.
*
* @author Bobby
* @version v1
*/
public class Bowler
{
private String name; //the full name of a bowler
private double highScore; //the highest score the bowler has obtained
/** No args constructor
* Create a Bolwler object and sets the name field to the
* string "unknown" and
* the high score of this object to 0.
*/
public Bowler( )
{
name = new String("unknown");
highScore = 0;
}
/** Constructs a Bowler object using the values
* of the arguments.
* @param String a name for the Bowler
* @param a high score for the Bowler. If the
* argument sent in is less than zero, the field is set to zero.
*/
public Bowler(String pName, double pHighScore)
{
this.name = new String(pName);
if(pHighScore < 0)
this.highScore = 0;
else
this.highScore = pHighScore;
}
/**
* Sets the Bowler's name field to the String argument.
* @param a new value for the Bolwer's name
*/
public void setName(String pName)
{
name = new String(pName);
}
/**
* Set's the Bolwer object's high score field to the
* argument.
* @param the high score field is set to the argument.
* If the argument's value is less than zero, the value of the field
* remains unchanged.
*
*/
public void setHighScore(double pHighScore)
{
if(pHighScore >= 0)
this.highScore = pHighScore;
}
/**
* This method returns the the name of the Bowler
* @return a reference to a String object that holds the Bowler's name.
*/
public String getName( )
{
return name;
}
/**
* This method returns the value of the Bowler's high score.
* @return returns the value of the highSocre field.
*/
public double getHighScore( )
{
return highScore;
}
/**
* Returns the textual representation of a Bowler object.
* @return a reference to a String that concatinates the Bolwer's
* name and high socre field values.
*/
public String toString( )
{
return "Bowler Name: " + this.name + " Highest Score: " + highScore;
}
}
===============================================DRIVER===================================================
/**
* This ia a driver to illustrate the need for some
* special methods.
* @author Bobby
* @version v1
*/
public class BowlerDriver
{
public static void main(String [ ] args)
{
Bowler b1; //declare a reference variable for a Bolwer object
Bowler b2; //declare a reference variable for a Bowler object
b1 = new Bowler("Harry Merser", 235);//create a Bowler
b2 = new Bowler("Harry Merser", 235);//create a Bowler
if(b1 == b2) //use the == operator to test equality
{
System.out.println(b1.toString( ) + " is the same as " +
b2.toString( )+ " why??");
}
else
{
System.out.println(b1.toString( ) + " is different from " +
b2.toString( ) + " why??" );
}
//assign b1 to b2
b1 = b2;
if(b1 == b2)
{
System.out.println(" " + b1.toString( ) + " is now the same as " +
b2.toString( ) + " why the change??");
}
else
{
System.out.println(b1.toString( ) + " is different from " +
b2.toString( ) + " why??" );
}
//change the b1 high score field to 99
b1.setHighScore(99);
//print the state of each Bowler
System.out.println(" After changing b1's score b1's state is: "
+ b1.toString( ) +
" b2's state is: " + b2.toString( ) +
" explain what happened");
}
}
Explanation / Answer
1. Bowler Name: Harry Merser Highest Score: 235.0 is different from
Bowler Name: Harry Merser Highest Score: 235.0 why??
2. Bowler Name: Harry Merser Highest Score: 235.0 is now the same as
Bowler Name: Harry Merser Highest Score: 235.0 why the change??
3. After changing b1's score
b1's state is: Bowler Name: Harry Merser Highest Score: 99.0
b2's state is: Bowler Name: Harry Merser Highest Score: 99.0
explain what happened
--> In the first question Bowler b1 is not equal to b2 because both the objects have different references with them although the data in both the objects is same.
--> In the second question Bowler b1 is equal to b2 bacause b2's reference was assigned to b1 which means that both the objects have same reference now.
--> In the third question when we change the data of b1 the data of b2 automatically changes this is because the reference of both the objects is same.
4. Bowler Name: Harry Merser Highest Score: 235.0 is different from
Bowler Name: Harry Merser Highest Score: 235.0 why??
5. Bowler Name: Harry Merser Highest Score: 235.0 is now the same as
Bowler Name: Harry Merser Highest Score: 235.0 why the change??
6. After changing b1's score
b1's state is: Bowler Name: Harry Merser Highest Score: 99.0
b2's state is: Bowler Name: Harry Merser Highest Score: 99.0
explain what happened
--> The answers of question 4,5,6 os same as the question 1,2,3 respectively.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.