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);In order to read from the user, we use the Scanner like this:
int x = scan.nextInt(); // use this if you want to read an int double d = scan.nextDouble(); // use this if you want to read a double String s = scan.next(); // use this if you want to read a StringThese are just EXAMPLES. You don't have to use these variable names, or do it on the same line as you declare. Only use the ones of these that match what you're doing, not all of them.
Part A
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. Do this by printing a request for the age, creating an int userage to hold the age, and using the appropriate code from above to read an int from the user and put it in userage. ☑ Create a a single compound if-else statement that goes through all the possibilities above and prints out the appropriate price, given the userage.Part B
☑ Add a new class and paste in this code
public static void test(int x, int y, int z) { System.out.println("input> " + x+ " "+ y +" " + z); /* PUT YOUR CODE HERE */ } public static void main(String[] args) { test(1,2,3); test(1,3,2); test(2,1,3); test(2,3,1); test(3,1,2); test(3,2,1); }The method test has three int parameters x, y, and z. Inside the test method, where indicated, write code that prints x, y, and z in increasing order (You can ignore the case where two numbers are the same). If you run this code, it will run the method test with all the possible orders of three numbers as input. In every case, we should end up with output "1 2 3" There are many correct solutions to this problem! here is one approach you could try:
Part A
☑ Create a project
and in it a class with a main.
In the main, write appropriate conditional statements to do the following:
(You can use if, if-else, nesting, or boolean operators (once you have them). You may not use special math methods, just relational and algebraic operators.)
☑ print "high" "middle" or "low" depending on if a number n is above 75, between 25 and 75, or below 25, but if it is exactly 75 or 25 print "hard to say"
☑ if x is below 12, double it, but if it is above 12 halve it, and either way print the result.
☑ if y and z are different, print whichever one is higher. If the higher one is negative also print a message saying so.
☑ print "even" if w is even and "odd" otherwise. You do not need any special math methods to do this.
You may choose to read in values from the user or to run your code multiple times with different values built in, but it is a good idea to check that your code works somehow.
☑ In the same project, add a class Owl. Give Owl a boolean instance variable night, an int instance variable hungry, and a String instance variable owlName.
☑ Write a toString for Owl with the format
Hooty the owl is 55% hungry and it is night: true.
[EC+25 instead, use conditionals to determine what to return in toString, so that depending on the owl's instance variables we might see things like:
Hooty the owl is very hungry today.Hooty the owl is a little bit hungry tonight.Hooty the owl is not hungry tonight]
☑ Later in the same main as before, create several owls with different values for their instance variables
For one of the owls, use if statements so that if that owl thinks it is night, a message prints saying the owl (use the name) is flying, but if it is not night, a message prints saying the owl refuses to fly in daytime. If the owl does fly, increase its hungry by 10.☑ Back in the Owl
class, give the owl a method
public void eat()Inside eat, use if statements. If hungry is above 70 , print a message saying the owl eats a lot, and decrease hunger by 30. If hungry is between 30 and 70 (inclusive) print a message saying the owl eats normally, and decrease hunger by 20. If hungry is between 1 and 29 (inclusive) say the owl just nibbles, and set hungry to 0. If hungry is 0, say the owl is not hungry and refuses to eat.
☑ Back in main, check that eat works for the owls you created.