I am creating a this program for my class project and I need help to finish it.
ID: 3682055 • Letter: I
Question
I am creating a this program for my class project and I need help to finish it. Please help !
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.*;
import java.time.*;
3
4 public class fortune teller
5 {
6 public static void main(String[] args)
7 {
8 Scanner keyboard = new Scanner(System.in);
9 System.out.println("Enter your numerical birth month (Ex: 8 for September):");
10 int bMonth = keyboard.nextInt();
11 System.out.println("Enter the day of the month you were born: ");
12 int bDay = keyboard.nextInt();
13 MonthDay birthdayMonth = MonthDay.of(bMonth, bDay);
14 MonthDay capricorn = MonthDay.of(01, 19); //format is MonthDay.of(int month, int day)
15 MonthDay saggitarius = MonthDay.of(12, 21);
16 if (birthdayMonth.isBefore(capricorn) || birthdayMonth.isAfter(saggitarius))
17 {
18 System.out.println("You are a capricorn.");
19 }
20 else
21 {
22 System.out.println("You are not a capricorn");
23 }
24 }
25 }
Explanation / Answer
import java.util.Scanner;
public class Zodiac{
public static int readMonthOfYear(){
String[] Months = {"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};
Scanner scanner = new Scanner(System.in);
while (true){
System.out.print("Enter the month you were born: ");
String input = scanner.nextLine().toUpperCase();
for(int i=0; i<Months.length; ++i){
if(input.equals(Months[i])){
return i;
}
}
System.out.println("Sorry invalid Month. Try Again.");
}
}
public static int readDayOfBirth() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("Enter the day you were born: ");
int input = sc.nextInt();
if (input > 0 && input < 32) {
return input;
} else {
System.out.println("Not a valid day. Try again.");
}
}
}
public static boolean shouldUpdateIndex(int month, int day) {
int[] cutoffs = {20, 19, 20, 20, 21, 21, 22, 22, 23, 23, 22, 21};
if (day > cutoffs[month]) {
return true;
} else {
return false;
}
}
public static String getZodiacSign(int index) {
String[] sign = {"Capricorn", "Aquarius", "Pisces", "Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius"};
if (index >= sign.length) {
index = 0;
}
return sign[index];
}
public static void main(String[] args){
int month = readMonthOfYear();
int day = readDayOfBirth();
if (shouldUpdateIndex(month, day)) {
month++;
}
System.out.printf("Your zodiac sign is: %s ", getZodiacSign(month));
}
}
SAMPLE OUTPUT
Enter the month you were born: MAY
Enter the day you were born: 28
Your zodiac sign is: Gemini
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.