Another method that might be desired is one that updates the Student’s number of credit hours. This method will receive a number of credit hours and add these to the Student’s current hours. Which of the following methods would accomplish this?

a) public int updateHours( )

{

return hours;

}



b) public void updateHours( )

{

hours++;

}



c) public updateHours(int moreHours)

{

hours += moreHours;

}



d) public void updateHours(int moreHours)

{

hours += moreHours;

}



e) public int updateHours(int moreHours)

{

return hours + moreHours;

}



The Coin class, as defined in Chapter 4, consists of a constructor, and methods flip, isHeads and toString. The method isHeads returns true if the last flip was a Heads, and false if the last flip was a Tails. The toString method returns a String equal to "Heads" or "Tails" depending on the result of the last flip. Using this information, answer questions 4-5.






4) A set of code has already instantiated c to be a Coin and has input a String guess, from the user asking whether the user guesses that a coin flip will result in "Heads" or "Tails". Which of the following sets of code will perform the coin flip and see if the user’s guess was right or wrong?

a) c.flip( );

if(c.isHeads( ).equals(guess)) System.out.println("User is correct");



b) if(c.flip( ).equals(guess)) System.out.println("User is correct");



c) if(c.isHeads( ).equals(guess)) System.out.println("User is correct");



d) c.flip( );

if(c.toString( ).equals(guess)) System.out.println("User is correct");



e) c.flip( ).toString( );

if(c.equals(guess)) System.out.println("User is correct");





5) What does the following code compute?

int num = 0;

for(int j = 0; j < 1000; j++)

{

c.flip( );

if(c.isHeads()) num++;

}

double value = (double) num / 1000;

a) the number of Heads flipped out of 1000 flips

b) the number of Heads flipped in a row out of 1000 flips

c) the percentage of heads flipped out of 1000 flips

d) the percentage of times neither Heads nor Tails were flipped out of 1000 flips

e) nothing at all

Respuesta :

CPED

Answer:

Part 1:

Option d) is correct one.

public void updateHours(int moreHours)  

{

hours += moreHours;  

}

Explanation:

The variable more hours will be given as argument to the function that will add the previous counted hours to the new ones. So the function updateHours will return total number of credit hours as required.

Part 2:

Option d) is the correct one.

c.flip( );

if(c.toString( ).equals(guess)) System.out.println("User is correct");

Explanation:

If the result of c.flip() gets equal to the guess the string "User is correct" will be printed on the screen to lag that the user is right otherwise the user will be wrong.

Part 3:

Option c) is the correct one.

the percentage of heads flipped out of 1000 flips

Explanation:

As we can see through the code that the total number of heads is divided by the number 1000 that is the total o all flips so it will give the percentage of the heads flipped out of 1000 lips.

I hope it will help you!

Q&A Education