In: Computer Science
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;
Part A
☑ In your main method, paste this code.
Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?"); int w = scan.nextInt(); boolean happy; boolean dead; System.out.println("Are you happy (y/n)?"); String userhappy = scan.next(); System.out.println("Are you dead (y/n)?"); String userdead = scan.next();
☑ After this, if what they enter for userhappy is "y" set happy to true, otherwise to false.
Do the same again to set a value for dead, so that we can better serve our zombie users.
☑ Write single if statements using boolean operators to do as follows:
CODE FOR THE FOLLOWING PROGRAM:-
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("What is x?");
int x = scan.nextInt();
System.out.println("What is y?");
int y = scan.nextInt();
System.out.println("What is z?");
int z = scan.nextInt();
System.out.println("What is w?");
int w = scan.nextInt();
boolean happy;
boolean dead;
System.out.println("Are you happy (y/n)?");
String userhappy = scan.next();
System.out.println("Are you dead (y/n)?");
String userdead = scan.next();
//If user is happy assign happy to true
if(userhappy.compareTo("y")==0){
happy=true;
}else{
happy=false;
}
//If user is dead assign dead to true
if(userdead.compareTo("y")==0){
dead=true;
}else{
dead=false;
}
System.out.println(happy);
//if x, y, and z all have the same value and the user is happy then print "Same and happy"
if((x==y && y==z) && (happy==true)){
System.out.println("Same and happy");
}
//if w is over 12 and the user is both happy and alive then print "Over 12, happy, alive"
if(w>12 && happy==true && dead==false){
System.out.println("Over 12, happy, alive");
}
if(x<z){
if((w>x && w<z) && (y>x && y<z)){
if((happy==true && dead==true) || (happy==false && dead==false)){
System.out.println("Long expression is . . . long");
}
}
}
}
}
SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-
SAMPLE OUTPUT:-
HAPPY LEARNING