Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Basics Perfecting the Prototype Using Logical ORs and ANDs

Why it is not true? boolean answer = (age < 40 || age > 50);

Assuming age is set to 42, what is the value stored in the boolean answer:

boolean answer = (age < 40 || age > 50);

TRUE False Why answer is not true?

2 Answers

Stuart Wright
Stuart Wright
41,120 Points

The || operator means OR, < means less than, and > means greater than.

The statement can therefore be read as:

"42 is less than 40 OR 42 is greater than 50"

Since 42 is neither less than 40 nor greater than 50, the statement evaluates to False OR False = False.

"Why this is showing syntax error"??

Example.java contains a program using the GoKart class we have been building (included for you to reference).

Protect the call to kart.drive by handling the IllegalArgumentException that is thrown when not enough battery remains. Print out the message from the exception to the screen as you catch the exception. Bummer! There is a compiler error. Please click on preview to view your syntax errors! Restart Preview Get Help Recheck work Example.java GoKart.java

1 class Example { 2 ā€‹ 3 public static void main(String[] args) { 4 GoKart kart = new GoKart("purple"); 5 if (kart.isBatteryEmpty()) { 6 System.out.println("The battery is empty"); 7 } 8 try{ 9 kart.drive(42); 10 }catch(IllegalArgumentException iae) 11 { System.out.println("NOT ENOUGH RICE");} System.out.printf("THE ERROR WAS %S",iae.getMessage()); 12 ā€‹ 13 } } 14 ā€‹ 15 ā€‹