Steps 7-8 on Cumulative Project 3

Hello, I am stuck with the 7th and 8th steps on the 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

The exact instruction is
" 7.

Calculate a random number between 0 and max

Use the result from task 6 to calculate a random number between 0 and max (the parameter you pass into generateRandomNumber() ) and save it to a local double variable."

Cast the result to an integer.

Ultimately, we need a whole number to decide which question to present next because presenting question number 3.2312 or question 0.7 is impossible (we can’t show 70% of a question… right? :thinking:).

Instead, we will use a programming technique called casting to convert one data type to another, in this case, from a double type to an int type.

An integer cannot retain any decimal values, so given the doubles above, 3.2312 casts down to 3 , and 0.7 casts down to 0 . In both cases, we lose the decimal value.

To cast, we place the resulting type we desire as a prefix to the original data (in parentheses). Check out this example of casting from an int to a boolean :"

I have no idea what these steps want me to do. I’d appreciate any hints . My source code so far is right here:

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

     

   }
// Add getGameOverMessage() here
  
   public static void main(String[] args) {
    
   }
   

}


Your code seems ok. Is it not working, or throwing an error?

If I try to put numbers into “value” and run it, it throws an error.
I actually can’t comprehend the steps. Looks like these ask me to test calculations but I’m not sure how to multiply values by “max” and “math”

If you call the generateRandomNumber() function inside main, using MainActivity.generateRandomNumber(someNumber), is there an error thrown? The way you’re calculating the random number seems to be correct.

If I do exactly what you told me, unfortunately there’s an error thrown. So I put the below code

// Add getGameOverMessage() here
  
   public static void main(String[] args) {
    MainActivity.generateRandomNumber(someNumber);
   }
   

}

Then the error pops out as follows:

MainActivity.java:13: error: cannot find symbol
    MainActivity.generateRandomNumber(someNumber);
                                      ^
  symbol:   variable someNumber
  location: class MainActivity
1 error

If I put a random number in there like

MainActivity.generateRandomNumber(7),
there’s no error thrown but the result is empty. Does it mean that I’m doing this task correctly?

Yes. Since the generateRandomNumber function uses return, nothing will be logged to the console. If you wrap the whole thing in System.out.println(), then you should see the result.

Thank you so much! I can see the result now. I hope I will not get into any more issues in this project.

1 Like