Control - Conditionals
IfExamples.java
Condition.java
Bunny.java.
Deitel 4.1-4.7
Part A
☑Paste the following into your main, and fix the code -- some parts are wrong, some are just silly.
int firstInt = 3; // this line is okay int secondInt = 4; // this line is okay boolean canFly = True; boolean isBlue = "false"; // warn the user if we can fly if (canFly == true); { System.out.println("Warning: I can FLY"); // this line is okay } // warn the user if firstInt is NOT smaller than secondInt if (firstInt < secondInt) { } else { System.out.println("Warning: firstInt is not smaller"); // this line is okay } // warn the user if the two booleans have the same value if (canFly = isBlue) { System.out.println("Warning: both are same"); // this line is okay }
Part B
We will be using the Scanner class to read from the user. At the top of your main class , after the package statement, pasteimport java.util.Scanner;As the first line inside your main method, paste
Scanner scan = new Scanner(System.in);☑ In your main, ask the user to enter an int. Do this by: first print a message asking for the int, then create an int x and use the Scanner to read an int from the user and put it in x, like this: int x = scan.nextInt(); ☑ Next read in two doubles d1 and d2 from the user. This will look a lot like what you did for the int, but the scanner reads doubles using
scan.nextDouble();
☑ Next read in a String s from the user. Scanner reads strings using
scan.next();
Part C
☑ if x is below 12, double it, otherwise halve it; either way print the result. (Just printing twice the value is NOT the same as changing the value and then printing!!!)
☑ if s is "Hello" print "Hi there!" otherwise print "No hello? RUDE!"
☑ print whichever of d1 or d2 is higher, with the word "MAX:" in front of it (if they are the same, it doesn't matter which one you print
Part D
Add the class Bunny to your project.
☑ In the same main as before, create two Bunnys and initialize them.
☑ Ask the user for and read in names and carrotCounts for each of the bunnys. In this case, read directly into the instance variables, instead of reading into a temporary variable and then setting the Bunny's variables afterward. For instance we can say things like
mrBunnyWunnykins.carrotCount = scan.nextInt();
☑ Print congratulations to whichever Bunny has higher carrotCount (if they have the same, it doesn't matter which one you print) using their name and carrotCount saying something like
Congrats Bugs, with 4 you have the highest carrot count.
☑ [EC] If the Bunnys have the same name, print "That's confusing!"