Question

In: Computer Science

create a project and in it a class with a main. We will be using the...

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:

  • if x, y, and z all have the same value and the user is happy then print "Same and happy"
  • if w is over 12 and the user is both happy and alive then print "Over 12, happy, alive"
  • [EC+10] 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"

Solutions

Expert Solution

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


Related Solutions

in Java using netbeans create a project and in it a class with a main. We...
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?");...
in Java using netbeans create a project and in it a class with a main. We...
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?");...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑ Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. ☑ Add a method addToCollection. In this method,...
Part A Java netbeans ☑ Create a project and in it a class with a main....
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...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
Create a Netbeans project with a Java main class. (It does not matter what you name...
Create a Netbeans project with a Java main class. (It does not matter what you name your project or class.) Your Java main class will have a main method and a method named "subtractTwoNumbers()". Your subtractTwoNumbers() method should require three integers to be sent from the main method. The second and third numbers should be subtracted from the first number. The answer should be returned to the main method and then displayed on the screen. Your main() method will prove...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
create a public class and a main class to deletebyStudentID number with get ID and getStudentByName...
create a public class and a main class to deletebyStudentID number with get ID and getStudentByName in public class. when the user is prompted to search for the student and delete them by their. ID has seven digits and 6 students each. Ex. Delete student by their ID         Enter students name and their ID         Vanessa,         1111-111         Vanessa is now deleted
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT