Question

In: Computer Science

1.Create a standard Java project (i.e. not a JavaFX project) in NetBeans called Personsages 2.Declare a...

1.Create a standard Java project (i.e. not a JavaFX project) in NetBeans called Personsages

2.Declare a Scanner variable called keyboardInput and a variable called personAge as type int. Code the statement to read in an integer with a prompt to "Enter your age as an integer:".

3.Code an "if/else if" (also called a multi-way if) block to output the following text based on age ranges personAge might contain:

  • 0 through 12: Person is a pre-teen.
  • 13 through 19: Person is a Teenager.
  • 20 through 29: Person is in their 20's.
  • 30 through 39: Person is in their 30's.
  • 40 or greater: Person is 40 years or older.

4.Code an "if / else" statement to determine if the age entered in Step 9 is on/before the year 1990 or after 1990 outputting the message that the person was born on/before or after 1990.

5.Declare a String variable called aCityInNY and read in a String from the keyboard with a prompt "Enter Albany, Buffalo, New York, Rochester, or Binghampton to see its population:".

6.Code a switch block to compare the value input to the cities just mentioned. Output "Population is: " with the population of each city (you are not being tested on the actual population). If the user enters any other city or text, output an appropriate error message to the user.

7.Declare an integer variable called intInput and assign it a 0. Declare a Boolean called keepLooping and set to true. Code a while loop with keepLooping as the condition.

8.In the body of the while loop, code a prompt that says “Input a number – enter 999 to exit:”. Use a Scanner method to get an integer from the keyboard and assign it to intInput. Output the number only if it does not equal 999, else set keepLooping false.

9.Declare a variable called loopInt. Code a for loop that uses loopInt and counts from 0 to 9.

10.In the for loop body, output “For Loop Counter = “ + loopInt.

11.After the for loop, set the loopInt variable to 0. Code a do-while loop that ends after loopInt is 9.

12.In the body of the do-while, output “Do-While Loop Counter = “ + loopInt. Code a statement to increase loopInt by 1.

13.After the while loop, declare an int variable called loopIntInner and an int variable called loopIntOuter. Code a for loop that counts loopIntOuter from 0 to 4. This is the outer loop. In the body, code another for loop (this is the inner loop) that counts loopIntInner from 0 to 2. In the body of the inner loop put an output statement that outputs “loopIntOuter: “ + loopIntOuter + “ loopIntInner: “ + loopIntInner.

14.Run the program with 32 as the age prompt in Step 9.

15.Enter at least one valid city for Step 12.

16.Enter at least 2 numbers for 15 and then 999.

Solutions

Expert Solution

//drop a comment if anything is wrong

//modify the class/project name accordingly

//------------------------------------------------------------------------------------

import java.util.Scanner;

class Main {

    public static void main(String[] args) {

        //2

        Scanner keyboardInput = new Scanner(System.in);

        int personAge;

        System.out.print("Enter your age as an integer: ");

        personAge = keyboardInput.nextInt();

        //3

        if (personAge <= 12) {

            System.out.println("Person is a pre-teen.");

        } else if (personAge >= 13 && personAge <= 19) {

            System.out.println("Person is a Teenager.");

        } else if (personAge >= 20 && personAge <= 29) {

            System.out.println("Person is in their 20's.");

        } else if (personAge >= 30 && personAge <= 39) {

            System.out.println("Person is in their 30's.");

        } else if (personAge >= 40) {

            System.out.println("Person is 40 years or older.");

        }

        //4

        if (2019 - personAge > 1990) {

            System.out.println("Person was born after 1990");

        } else if (2019 - personAge < 1990) {

            System.out.println("Person was born before 1990");

        } else {

            System.out.println("Person was born in 1990");

        }

        //5

        System.out.print("Enter Albany, Buffalo, New York, Rochester, or Binghampton to see its population: ");

        keyboardInput.nextLine();

        String aCityInNY = keyboardInput.nextLine();

        

        //6

        switch (aCityInNY) {

        case "Albany":

            System.out.println("Population is: 1234");

            break;

        case "Buffalo":

            System.out.println("Population is: 3424");

            break;

        case "New York":

            System.out.println("Population is: 13242234");

            break;

        case "Rochester":

            System.out.println("Population is: 1233424");

            break;

        case "Binghampton":

            System.out.println("Population is: 154234");

            break;

        default:

            System.out.println("the city does not exist");

        }

        //7

        int intInput = 0;

        boolean keepLooping = true;

        //8

        while (keepLooping) {

            System.out.println("Input a number- enter 999 to exit: ");

            intInput = keyboardInput.nextInt();

            if (intInput != 999) {

                System.out.println("" + intInput);

            } else {

                keepLooping = false;

            }

        }


        //9

        int loopInt;

        for (loopInt = 0; loopInt <= 9; loopInt++) {

            //10

            System.out.println("For Loop Counter = " + loopInt);

        }

        //11

        loopInt = 0;


        do {

            System.out.println("Do-While Loop Counter = " + loopInt);

            loopInt++;

        } while (loopInt < 9);

        //13

        int loopIntInner;

        int loopIntOuter;

        for (loopIntOuter = 0; loopIntOuter <= 4; loopIntOuter++) {

            //14

            for (loopIntInner = 0; loopIntInner <= 2; loopIntInner++) {

                System.out.println("loopIntOuter: " + loopIntOuter + " loopIntInner: " + loopIntInner);

            }

        }

    }

}

//----------OUTPUT/INPUT------------------------------------------------------------------

Enter your age as an integer: 32
Person is in their 30's.
Person was born before 1990
Enter Albany, Buffalo, New York, Rochester, or Binghampton to see its population: Albany
Population is: 1234
Input a number- enter 999 to exit:
32
32
Input a number- enter 999 to exit:
34
34
Input a number- enter 999 to exit:
999
For Loop Counter = 0
For Loop Counter = 1
For Loop Counter = 2
For Loop Counter = 3
For Loop Counter = 4
For Loop Counter = 5
For Loop Counter = 6
For Loop Counter = 7
For Loop Counter = 8
For Loop Counter = 9
Do-While Loop Counter = 0
Do-While Loop Counter = 1
Do-While Loop Counter = 2
Do-While Loop Counter = 3
Do-While Loop Counter = 4
Do-While Loop Counter = 5
Do-While Loop Counter = 6
Do-While Loop Counter = 7
Do-While Loop Counter = 8
loopIntOuter: 0 loopIntInner: 0
loopIntOuter: 0 loopIntInner: 1
loopIntOuter: 0 loopIntInner: 2
loopIntOuter: 1 loopIntInner: 0
loopIntOuter: 1 loopIntInner: 1
loopIntOuter: 1 loopIntInner: 2
loopIntOuter: 2 loopIntInner: 0
loopIntOuter: 2 loopIntInner: 1
loopIntOuter: 2 loopIntInner: 2
loopIntOuter: 3 loopIntInner: 0
loopIntOuter: 3 loopIntInner: 1
loopIntOuter: 3 loopIntInner: 2
loopIntOuter: 4 loopIntInner: 0
loopIntOuter: 4 loopIntInner: 1
loopIntOuter: 4 loopIntInner: 2


Related Solutions

Java programming language: 1.      Create a new Netbeans project called ArrayLoop. 2.      Declare a fixed length array of...
Java programming language: 1.      Create a new Netbeans project called ArrayLoop. 2.      Declare a fixed length array of integers which can store 8 elements. Do not assign any values. int[] myAddresses = new int[8]; 3.      Create a statement which raises 2 to the power of your loop counter, then subtracts 1, and assigns that value to the corresponding element. For example, when i = 3, 2 to the third power is 8, minus 1 = 7. When i = 4, 2 to 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...
Step 1: Create a new Java project in NetBeans called “Wedding1” Step 2: Use appropriate data...
Step 1: Create a new Java project in NetBeans called “Wedding1” Step 2: Use appropriate data types to store the following information: The names of the bride and groom The total number of guests at the wedding The square footage of the location (must be accurate to 0.1 square feet). The names of each song in the DJ's playlist. You should use an ArrayList of Strings to store this, and the user should be able to enter as many song...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the class name YourlastnameLab7 with your actual last name. Create a Java class file for a Polygon class. Implement the Polygon class. Add a private instance variable representing the number of sides in the polygon. Add a constructor that takes a single argument and uses it to initialize the number of sides. If the value of the argument is less than three, display an error...
2. Create a new NetBeans project called PS1Gradebook. Your program will simulate the design of a...
2. Create a new NetBeans project called PS1Gradebook. Your program will simulate the design of a student gradebook using a two-dimensional array. It should allow the user to enter the number of students and the number of assignments they wish to enter grades for. This input should be used in defining the two-dimensional array for your program. For example, if I say I want to enter grades for 4 students and 5 assignments, your program should define a 4 X...
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?");...
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...
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?");...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT