In: Computer Science
Part A
Java netbeans
☑ 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 ("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.
☑ [EC+10] read in an x from the user and print "even" if x is even and "odd" otherwise. You do not need any special math methods to do this.
//create class NestedIF.java and paste the code given below
import java.util.Scanner;
public class NestedIF {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
/*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"
*/
System.out.print("Enter number n:");
int number = scan.nextInt();
if (number > 75) {
System.out.println("high");
} else if (number > 25 && number < 75) {
System.out.println("middle");
} else if (number < 25) {
System.out.println("low");
}else if(number==75 || number==25)
{
System.out.println("hard to say");
}
/*Ask the user their favorite animal and get a String as their
answer.
For five possible values ("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.*/
System.out.print("Enter Animal Name:");
String animal=scan.next();
if(animal.equalsIgnoreCase("cat"))
{
System.out.println("oh, purr!");
}else if(animal.equalsIgnoreCase("dog"))
{
System.out.println("Oh, Bark!");
}else if(animal.equalsIgnoreCase("capybara"))
{
System.out.println("Oh, Bark!");
}else if(animal.equalsIgnoreCase("lion"))
{
System.out.println("Oh, Roar!");
}else if(animal.equalsIgnoreCase("monkey"))
{
System.out.println("Oh,scream");
}else
{
System.out.println("Oh, General 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.
*/
System.out.print("Enter Your age:");
int age=scan.nextInt();
if(age<5)
{
System.out.println("Admission price :$4");//under 5
}else if(age<=12)
{
System.out.println("Admission price :$10");// children up to
(inclusive) 12 get in for $10
}else if(age<19)
{
System.out.println("Admission price :$12");//teens up to 19 get in
for $12
}else if(age<59)
{
System.out.println("Admission price :$15");//adults up to 59 for
$15
}else if(age<79)
{
System.out.println("Admission price :$10");//seniors up to 79 for
$10,
}else
{
System.out.println("Admission price :$5");//seniors 80 and over for
$5.
}
/*
read in an x from the user and print "even"
if x is even and "odd" otherwise. You do not need
any special math methods to do this.
*/
System.out.print("Enter number x:");
int x=scan.nextInt();
if(x %2==0)//check if x is even or odd
{
System.out.println("even");
}else
{
System.out.println("odd");
}
}
}
____________________________________________OUTPUT_______________________________________
Enter number n:20
low
Enter Animal Name:cat
oh, purr!
Enter Your age:18
Admission price :$12
Enter number x:20
even