Write a JAVA program called ZodiacFinderjava that takes two

Write a JAVA program called ZodiacFinder.java that takes two integers as input (month and day) and prints the Zodiac sign corresponding to the month (1 = January, 12 = December) and the day (1 - 31).

Use a method (called userInput() )for user input – use one method to input an integer called twice – but, prompt the user for the appropriate data item. Error trap your user – the user can enter a month between 1 and 12, the user can only enter a day between 1 and 31.

Use a method (called determineZodiac () ) to determine and return the Zodiac sign.

Use a method (called displayAll () ) to display the month, day and the corresponding Zodiac sign

Solution


// ZodiacFinder.java

import java.util.Scanner;

public class ZodiacFinder
{

public static int userInput(int limit)
{
Scanner sc = new Scanner(System.in);
int input;

while(true)
{
input = sc.nextInt();

if(input >= 1 && input <= limit)
break;
else
System.out.print(\"value should be 1-\" + limit + \"\ \");
System.out.print(\"Enter again: \");
}
  
return input;
}

public static String determineZodiac(int day, int month)
{
if ((month == 12 && day >= 22 && day <= 31) || (month == 1 && day >= 1 && day <= 19))
return \"Capricorn\";
else if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day >= 1 && day <= 17))
return \"Aquarius\";
else if ((month == 2 && day >= 18 && day <= 29) || (month == 3 && day >= 1 && day <= 19))
return \"Pisces\";
else if ((month == 3 && day >= 20 && day <= 31) || (month == 4 && day >= 1 && day <= 19))
return \"Aries\";
else if ((month == 4 && day >= 20 && day <= 30) || (month == 5 && day >= 1 && day <= 20))
return \"Taurus\";
else if ((month == 5 && day >= 21 && day <= 31) || (month == 6 && day >= 1 && day <= 20))
return \"Gemini\";
else if ((month == 6 && day >= 21 && day <= 30) || (month == 7 && day >= 1 && day <= 22))
return \"Cancer\";
else if ((month == 7 && day >= 23 && day <= 31) || (month == 8 && day >= 1 && day <= 22))
return \"Leo\";
else if ((month == 8 && day >= 23 && day <= 31) || (month == 9 && day >= 1 && day <= 22))
return \"Virgo\";
else if ((month == 9 && day >= 23 && day <= 30) || (month == 10 && day >= 1 && day <= 22))
return \"Libra\";
else if ((month == 10 && day >= 23 && day <= 31) || (month == 11 && day >= 1 && day <= 21))
return \"Scorpio\";
else if ((month == 11 && day >= 22 && day <= 30) || (month == 12 && day >= 1 && day <= 21))
return \"Sagittarius\";
else
return \"Illegal date\";

}

public static void displayAll(int month,int day, String zodiacSign)
{
if(zodiacSign.equals(\"Illegal date\"))
System.out.println(\"No zodiac sign found for give day and month\ \");
else
System.out.println(\"\ Day: \" + day + \"\ Month: \" + month + \"\ Zodiac Sign: \" + zodiacSign);
}

public static void main(String[] args)
{
System.out.print(\"Enter month: \");
int month = userInput(12);
System.out.print(\"Enter day: \");
int day = userInput(31);

String zodiacSign = determineZodiac(day,month);

displayAll(month,day,zodiacSign);

}

}

/*
output:

Enter month: 31
value should be 1-12
Enter again: 12
Enter day: 31

Day: 31
Month: 12
Zodiac Sign: Capricorn


*/

Write a JAVA program called ZodiacFinder.java that takes two integers as input (month and day) and prints the Zodiac sign corresponding to the month (1 = Januar
Write a JAVA program called ZodiacFinder.java that takes two integers as input (month and day) and prints the Zodiac sign corresponding to the month (1 = Januar

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site