In: Computer Science
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:
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.
//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