In: Computer Science
in Java using 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;
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:
Sample output:
The screenshots are attached below for reference.
Please follow them for proper indentation and output.
Program to
copy:
import java.util.*;
public class Main{
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);//starter code
provided
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(userhappy.equals("y"))//check if userhappy is y or not
happy=true;
else
happy=false;
if(userdead.equals("y"))//check if userdead is y or not
dead=true;
else
dead=false;
if(x==y && y==z && happy )//check the conditions
and print appropriate messages on console
System.out.println("Same and happy");
if(w>12 && happy && !(dead))
System.out.println("Over 12, happy, alive");
if(x<z){
if(w>x && w<z){
if(y>x && y<z){
if((happy && dead) || (!happy && !dead))
System.out.println("Long expression is . . . long");
}
}
}
}
}