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:
Java class :
//import package
import java.util.Scanner;
//Java class
public class XYCLass {
// main() method
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();
// checking value of
userhappy
if
(userhappy.toLowerCase().contentEquals("y")) {
// if userhappy
is y then set happy=true
happy =
true;
} else {
happy =
false;
}
// checking value of userdead
if
(userdead.toLowerCase().contentEquals("y")) {
// if userdead
is y then set dead=true
dead =
true;
} else {
dead =
false;
}
// checking value of x , y and
z
if (x == y && y == z
&& z == x && happy == true) {
// if x, y, and
z all have the same value and the user is happy
System.out.println("Same and happy");
} else if (w > 12 &&
happy == true && dead == false) {
// if w is over
12 and the user is both happy and alive then print "Over 12,
// happy,
alive"
System.out.println("Over 12, happy, alive");
} else if (x < z && (w
> x && w < z) && (y > x && y <
z) && (happy == true && dead == true)
|| (happy == false && dead == false))
{
// if x is less
than z and both w and y are in the range between x and z, and
// either the
user is happy and dead, or sad and alive then print "Long
// expression is
. . . long"
System.out.println("Long expression is . . . long");
}
}
}
=================================
Screen showing output :
Screen showing output :