Question

In: Computer Science

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?");
        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

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 :


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 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 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...
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();...
Create a new Java project using NetBeans, giving it the name L-14. In java please Read...
Create a new Java project using NetBeans, giving it the name L-14. In java please Read and follow the instructions below, placing the code statements needed just after each instruction. Leave the instructions as given in the code. Declare and create (instantiate) an integer array called List1 that holds 10 places that have the values: 1,3,4,5,2,6,8,9,2,7. Declare and create (instantiate) integer arrays List2 and List3 that can hold 10 values. Write a method called toDisplay which will display the contents...
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder....
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder. 3. Import Scanner and Stacks from the java.util package. 4. Create a Stack object named basket. 5. The output shall: 5.1.Ask the user to input the number of fruits s/he would like to catch. 5.2.Ask the user to choose a fruit to catch by pressing A for apple, O for orange, M for mango, or G for guava. 5.3.Display all the fruits that the...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT