JAVA LANGUAGE Generation The approximate ranges of the generations in Western cu
ID: 3588255 • Letter: J
Question
JAVA LANGUAGE
Generation
The approximate ranges of the generations in Western culture are defined as follows:
1) The Lost Generation: 1883 – 1900
2) GI Generation: 1901 – 1924
3) Silent Generation: 1925 – 1945
4) Baby Boomers: 1946 – 1964
5) Generation X: 1965 – 1984
6) Millennial: 1985 – 2004
7) Generation Z: 2005 – present
Write a program that asks the user to enter a birth year. The program should then indicate which generation a person of that year belongs/would have belonged to.
You will need:
A scanner object for input
Input validation: Make sure the year entered is within the range of 1883 – 2017. (Use a while loop for best results)
A variable to store the year entered
An if else if statement to select the appropriate generation
A sample of the output is shown below:
Enter a year: 206
Sorry, that year is not within the valid range.
Re-enter the year: 2006
You are part of Generation Z.
Chinese Zodiac
Write a program that asks the user to enter a year. The program should then indicate the type of year it is based on the Chinese Zodiac.
The order of the Chinese Zodiac shown below is based on a 12-year cycle. The first zodiac corresponds to years divisible by 12 (remainder 0), the second year corresponds to years that a produce a remainder of 1 and so on. For instance, 2017 is the year of the rooster because 2017 divided by 12 produces a remainder of 1.
The years are as follows:
Monkey
Rooster
Dog
Pig
Rat
Ox
Tiger
Rabbit
Dragon
Snake
Horse
Sheep
You will need:
A scanner object for input
Input validation: Make sure the year entered is positive! (Use a while loop for this part.)
A variable to store the year entered
A switch statement to select the appropriate zodiac sign
A sample of the output is shown below:
Enter a year: -89
Enter anno domini (AD) years please!
Re-enter the year: 1989
The year 1989 is the year of the snake
Explanation / Answer
Generation.java
import java.util.Scanner;
public class Generation {
public static void main(String[] args) {
//Declaring variables
int year = 0;
String generation = null;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Enter a year: ");
year = sc.nextInt();
/* This while loop continues to execute
* until the user enters a valid Year
*/
while (true) {
if (year >= 1883 && year <= 1900) {
generation = "The Lost Generation";
break;
} else if (year >= 1901 && year <= 1924) {
generation = "GI Generation";
break;
} else if (year >= 1925 && year <= 1945) {
generation = "Silent Generation";
break;
} else if (year >= 1946 && year <= 1964) {
generation = "Baby Boomers";
break;
} else if (year >= 1965 && year <= 1984) {
generation = "Generation X";
break;
} else if (year >= 1985 && year <= 2004) {
generation = "Millennial";
break;
} else if (year >= 2005) {
generation = "Generation Z";
break;
} else {
System.out.println("Sorry, that year is not within the valid range.");
System.out.print("Re-enter the year: ");
year = sc.nextInt();
continue;
}
}
//Displaying the output
System.out.println("You are part of " + generation);
}
}
____________________
Output:
Enter a year: 206
Sorry, that year is not within the valid range.
Re-enter the year: 2006
You are part of Generation Z
___________________
ChineeseZodiac.java
import java.util.Scanner;
public class ChineeseZodiac {
public static void main(String[] args) {
//Declaring variables
int index;
int year;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
//Declaring an String array and initialize with year names
String zodiac[] = { "Monkey", "Rooster", "Dog", "Pig", "Rat", "Ox","Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Sheep" };
//Getting the year entered by the user
System.out.print("Enter a year (Negative number ends the program ):");
year = sc.nextInt();
/* This loop continues to execute until the user
* enters a negative number
* */
while (true) {
switch (year % 12) {
case 0:
{
System.out.println("The year " + year + " is the year of " + zodiac[year % 12]);
break;
}
case 1:
{
System.out.println("The year " + year + " is the year of " + zodiac[year % 12]);
break;
}
case 2:
{
System.out.println("The year " + year + " is the year of " + zodiac[year % 12]);
break;
}
case 3:
{
System.out.println("The year " + year + " is the year of " + zodiac[year % 12]);
break;
}
case 4:
{
System.out.println("The year " + year + " is the year of " + zodiac[year % 12]);
break;
}
case 5:
{
System.out.println("The year " + year + " is the year of " + zodiac[year % 12]);
break;
}
case 6:
{
System.out.println("The year " + year + " is the year of " + zodiac[year % 12]);
break;
}
case 7:
{
System.out.println("The year " + year + " is the year of " + zodiac[year % 12]);
break;
}
case 8:
{
System.out.println("The year " + year + " is the year of " + zodiac[year % 12]);
break;
}
case 9:
{
System.out.println("The year " + year + " is the year of " + zodiac[year % 12]);
break;
}
case 10:
{
System.out.println("The year " + year + " is the year of " + zodiac[year % 12]);
break;
}
case 11:
{
System.out.println("The year " + year + " is the year of " + zodiac[year % 12]);
break;
}
default:
{
System.out.println("Enter anno domini (AD) years please!");
System.out.print("Re-enter the year:");
year = sc.nextInt();
continue;
}
}
break;
}
}
}
_________________
Output:
Enter a year (Negative number ends the program ):-89
Enter anno domini (AD) years please!
Re-enter the year:1989
The year 1989 is the year of Snake
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.