Cumulative Project 3 Steps 10-13

I am totally lost again in the last few steps of Cumulative Project 3.

https://www.codecademy.com/paths/introduction-to-android-with-java/tracks/programming-logic/modules/cumulative-project-3/projects/unquote-game-logic-pt-i

I’m sure that I coded something wrong as my code threw a bunch of errors on the step 14 (very end of this project). I particularly didn’t comprehend the step 13:

Return the expected String from getGameOverMessage() .

Previously, you have encountered one return statement per method, and usually at the bottom.

However, it is possible to code multiple return statements in the same method body.

Use one or more return statements to return the correct String back from your getGameOverMessage() method.

I put my codes in question, so any insights on where I went wrong much appreciated!

public class MainActivity {
// Add generateRandomNumber() here
   public static int generateRandomNumber(int max){
     double value = Math.random() * max;
     return (int) value;

     

   }
// Add getGameOverMessage() here
   public getGameOverMessage(int totalCorrect, int totalQuestions) {
    
    if (totalCorrect = totalQuestions) {
      String 1 = "You got all" + totalCorrect + " right! You won! ";

      System.out.println(1);

      return "You got You got all 6 right! You won!";
    } else {
      String 2 = "You got" + totalCorrect + " right out of" + totalQuestions + ". Better luck next time! ";

      System.out.println(2);

      return "You got 3 right out of 6. Better luck next time!";
    }
   }
  
   public static void main(String[] args) {
    MainActivity.generateRandomNumber(7);
    System.out.println(MainActivity.generateRandomNumber(7));
   }
   

}


Hello! You have not specified the return type for the getGameOverMessage() function. Also, variable names cannot start with a number. Is this what’s causing the error?

Ok I added the return type add in there and changed the variable name in String as follows:

public class MainActivity {
// Add generateRandomNumber() here
   public static int generateRandomNumber(int max){
     double value = Math.random() * max;
     return (int) value;

     

   }
// Add getGameOverMessage() here
   public void getGameOverMessage(int totalCorrect, int totalQuestions) {
    
    if (totalCorrect == totalQuestions) {
      String allCorrect = "You got all" + totalCorrect + " right! You won! ";

      System.out.println(allCorrect);

      return "You got You got all 6 right! You won!";
    } else {
      String partiallyCorrect = "You got" + totalCorrect + " right out of" + totalQuestions + ". Better luck next time! ";

      System.out.println(partiallyCorrect);

      return "You got 3 right out of 6. Better luck next time!";
    }
   }
  
   public static void main(String[] args) {
    MainActivity.generateRandomNumber(7);
    System.out.println(MainActivity.generateRandomNumber(7));
   }
   

}

It still throws the below error:

MainActivity.java:18: error: incompatible types: unexpected return value
      return "You got You got all 6 right! You won!";
             ^
MainActivity.java:24: error: incompatible types: unexpected return value
      return "You got 3 right out of 6. Better luck next time!";
             ^
2 errors

I have some idea why this went wrong, the step 10 is very confusing to me.

Game. Over.

When the player submits their answer to the final question, the game ends. At which point, you present the player with one of two possible messages:

“You got all 6 right! You won!”
Or “You got 3 right out of 6. Better luck next time!”
You will create a method that generates this String message.

Begin by defining a method in MainActivity named getGameOverMessage(), it must accept two integer inputs (totalCorrect and totalQuestions), and return a String object.

So if I specify the return type as

void

for the getGameOverMessage, I don’t have to put like

getGameOverMessage (int totalCorrect, int totalQuestions) ?

When you specify a return type of void, you’re telling the computer that the function returns a void, or, in other words, the function doesn’t return anything.

When declaring a function, the syntax is:

[private/public/protected] [return type] [function name](parameter type parameter name){
//code
}

So, a function declaration might look like:

public void someFunc(String someString){
  System.out.println(someString);
}

I ended up declaring it as boolean not void after I reviewed the steps 10-12. Until the step 12, it threw no error so the boolean was correct I guess?

But the hint on the step 13 is still very confusing to me.

You can place return statements inside of an if / else, for example,
String getDailySpecial() {
 
    if (chocoTacosExpired == true) {
 
        return "Choco Tacos are today's daily special!";
 
    } else if (klondikeBarsExpired == true) {
 
        return "What would you doOooOoo for today's daily special, the Klondike Bar!?"
 
    } else if ...
}

For the step 13 I typed in the two return statements with the full sentences just like the above Chocotako and Klondlike Bar example, but it throws an error. Still no idea where it went wrong.

MainActivity.java:19: error: incompatible types: String cannot be converted to boolean
      return "You got all 6 right! You won! ";
             ^
MainActivity.java:26: error: incompatible types: String cannot be converted to boolean
      return "You got 3 right out of 6. Better luck next time! ";
             ^
2 errors

My updated code is here:

// Add getGameOverMessage() here
   boolean getGameOverMessage(int totalCorrect,int totalQuestions) {
    
    if (totalCorrect == totalQuestions) {

      String allCorrect = "You got all" + totalCorrect + " right! You won! ";

      System.out.println(allCorrect);

      return "You got all 6 right! You won! ";

    } else {
      String partiallyCorrect = "You got" + totalCorrect + " right out of" + totalQuestions + ". Better luck next time! ";

      System.out.println(partiallyCorrect);
      
      return "You got 3 right out of 6. Better luck next time! ";
    }
   }

I feel like I start banging my head against my computer now.

You are telling the program that getGameOverMessage() should return a boolean (true or false, nothing else). Then, you go and return a String from that function. As the error says, the type of return you’ve specified in the code and the actual type of object you are returning are incompatible (you can’t turn a String into a boolean without some data being lost).

Ok I think I finally got the hang of this. I switched the return type of getGameOverMessage() from boolean to String . Then I ran the Main.Java as required on the step 14 (the very last step for this project). Looks like it all went well on my side. Thank you so much for the assistance and the insights @codeneutrino .

public class MainActivity {
// Add generateRandomNumber() here
   public static int generateRandomNumber(int max){
     double value = Math.random() * max;
     return (int) value;

     

   }
// Add getGameOverMessage() here
   public String getGameOverMessage(int totalCorrect,int totalQuestions) {
    
    if (totalCorrect == totalQuestions) {

      String allCorrect = "You got all" + totalCorrect + " right! You won! ";

      System.out.println(allCorrect);

      return "You got all 6 right! You won! ";

    } else {
      String partiallyCorrect = "You got" + totalCorrect + " right out of" + totalQuestions + ". Better luck next time! ";

      System.out.println(partiallyCorrect);
      
      return "You got 3 right out of 6. Better luck next time! ";
    }
   }
  
   public static void main(String[] args) {
    MainActivity.generateRandomNumber(7);
    System.out.println(MainActivity.generateRandomNumber(7));

   }
   

}


The player’s guess is: -1
Default answer selected!
The player’s guess is: 2
The player’s guess is correct!
A random number between 0 and 50: 27
A random number between 0 and 1000: 75
A random number between 0 and 1 million: 119394
You got2 right out of3. Better luck next time! 
Game over test 1 (should be 2 out of 3): You got 3 right out of 6. Better luck next time! 
You got all4 right! You won! 
Game over test 2 (should be 4 out of 4): You got all 6 right! You won!
1 Like