Control - Conditionals


Control - Conditionals


IfExamples.java
Condition.java
Bunny.java.

Deitel 4.1-4.7


Classwork


Create a project  and a class with a main method.

You can do all of today's classwork in mains  --  you don't need instance variables, etc.

 

We will be using the Scanner class to read from the user. At the top of your main class , after the package statement, paste

import java.util.Scanner;
As the first line inside your main method, paste
Scanner scan = new Scanner(System.in);

Part A

 ☑ In your main, ask the user for an int.  Do this by first printing a request for the int, then creating an int x, and using 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 B

if x is below 12, double it, otherwise  halve it; either way print the result.

if s is "Hello" print "Hi there!" otherwise print "No hello?  RUDE!"

if d1 and d2 are the same, print "same" otherwise print whichever of d1 or d2 is higher, with the word "MAX:" in front of it

Part C

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(); 

If the Bunnys have the same name, print "That's confusing!"

Print congratulations to whichever Bunny has higher carrotCount (if they have the same, it doesn't matter which one you print) using the name and the carrotcount in what you print.

[EC+10] Do this last part with only one print statement which is not inside an if.  (hint: you can add an extra Bunny variable).