Conditionals Practice

Part A


Create a project and in it a class with a main. 

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

Remember when you are getting a value from the user, first print a request, then use the Scanner to read the right type from the user and put it in a variable of that type.

In the main, do the following:

 (You can use if, if-else, nesting, or boolean operators. You may not use special math methods, just relational and algebraic operators.)

Get an int n from the user and print "high" "middle" or "low" depending on whether n is above 75, between 25 and 75, or below 25, but  if it is exactly 75 or 25 print "hard to say"

Ask the user their favorite animal and get a String as their answer.  For five possible values (e.g., "cat" "dog" "capybara" etc... your choice) if the user said that animal, print a specialized comment about it (e.g. if they said "cat" you might print "oh, purr!").  If they said none of the types your program covers, instead print a general comment for all other types of animal.

At a certain amusement park, children under 5 get in for $4, children up to (inclusive) 12 get in for $10, teens up to 19 get in for $12, adults up to 59 for $15, seniors up to 79 for $10, and seniors 80 and over for $5. In your main, ask the user for their age.  Create a a single compound if-else statement that goes through all the possibilities for admission price above and prints out the appropriate price, given the user's age.


Part B

In the same project, add a class Owl. Give Owl a boolean instance variable night, and int instance variable hungry. 

Write a toString for Owl using conditionals to determine what to return, with "today" vs "tonight" based on whether it is night, and using "not" when hungry is at 0 so we get outputs like

The owl is 25% hungry today. // means hungry was at 25 and night was false
The owl is not hungry tonight.// means hungry was at 0 and night was true
The owl is not hungry today. // means hungry was at 0 and night was false
The owl is 87% hungry tonight. // means hungry was at 87 and night was true

This does NOT mean that 25 and 87 are the only possible values, they are just example values hungry might have

Give Owl a method

public void hunt()
Inside this method, use if to determine a printout to describe what the owl does.  If it is over 70 hungry, it will hunt, day or night, if it is over 30 hungry, it will hunt as long as it is night, otherwise it will refuse to hunt.

Later in the same main as before, create two owls and ask the user for values for their instance varaibles (we don't have a way to read in a boolean, but we could read "yes" or "no"...).

Print the owls  and call hunt for each.

Also use an if in the main to print whichever owl has higher hungry but print nothing if they are the same.