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...
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...
(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 Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT