Cumulative project 3: Game Logic Part 1

Hi,

I’ve went back and relooked at methods and I am still stuck on #2 of this project. this is what I put for #1 with no errors:

Define an isCorrect() method.
We want to add a method that returns true if the player answered the question correctly and false otherwise.
Begin by defining a method in Question.java named, isCorrect() .

// Add isCorrect() here public static void main(String[] args) { boolean isCorrect = true; }

Now I am lost on where to go for #2.

Determine whether the player answered the question correctly.
How do we know if the player selected the correct answer? Remember that your Question object has the member variables correctAnswer and playerAnswer —both integers.

The isCorrect() method should compare correctAnswer and playerAnswer . If they’re the same, the method should return true . Otherwise, it should return false .

And this is what I have so far??

// Add isCorrect() here static int isCorrect(int correctAnswer, int playerAnswer) { return correctAnswer == playerAnswer; } public static void main(String[] args) { }

my error:
Question.java:30: error: incompatible types: boolean cannot be converted to int
return correctAnswer == playerAnswer;

Your method signature indicates you want to return an int, but your return value is trying to return a bool. You need to decide which one you want to go with.

1 Like

So honestly I taught myself HTML and some CSS some time ago. I am finding Java hard to comprehend. maybe it’s because I am getting into my old age :). but honestly I am really having a hard time deciphering the code academy instructions in the projects. I’m guessing that is because I am unfamiliar with Java?

if the instructions say it should “return value true” then is it easier to just do it all as boolean?

1 Like

because as I have it in the post. I am lost on how to return value true using “int”?

So java methods are basically functions.

In math we have things like f(x)=2x, where if the input is 3, you get 6… if the input is 10, you get 20. In other words, x is some placeholder for input, and the 2x is some operation that is done to that input.

Method/functions work very much the same. You take in some input and you specify the type, do some operation and return the result.

int doubleMyNumber(int x)
{
   return 2 * x;
}
bool doYouLikeBread(char answer)
{
   if (answer == 'y')
   {
      return true;
   }
   else
   {
      return false;
   }
}

One reason types are important to define in a language is performance. Some information inherently holds more space than other information. So the CPU has to allocate more memory for its execution. Part of the reason Java performs so well is because of its type system (this is an over-simplification, and is in comparison to languages like Python). So the extra strictness comes with benefits.

(If your function is supposed to return some boolean value, just change the type of the method signature.)

3 Likes

ok I am going to look this over and try again.

Thank you

1 Like

I’m still working on it but honest question:
Should I have this much trouble with comprehension this early?

ok so I did not get any errors so is this correct for #2?

// Add isCorrect() here boolean isCorrect() { if (correctAnswer == playerAnswer); return true; } public static void main(String[] args) { }

Should I have this much trouble with comprehension this early?

I think java is definitely a different beast from html/css so I wouldn’t worry about speed of comprehension. It’s also harder than python and javascript in my opinion (two common starter languages).

The answer you’re looking for is probably something close to this

boolean isCorrect(int correctAnswer, int playerAnswer) {
             return correctAnswer == playerAnswer;
}

or

public boolean isCorrect(int correctAnswer, int playerAnswer) {
             return correctAnswer == playerAnswer;
}

note: I remember when I started programming the concepts of public, private, static were all very vague with me. They’re definitely not easy to pick up right away.

1 Like