Question

Write a Java program for a fortune teller. The program should begin by asking the user...

Write a Java program for a fortune teller.

The program should begin by asking the user to enter their name. Following that the program will display a greeting.

Next, the program will ask the user to enter the month (a number 1-12) and day of the month (a number 1-13) they were born. Following that the program will display their zodiac sign.

Next, the program will ask the user their favorites color (the only choices should be: red, green and blue). Following that the program will display a fortune based on the chosen color. The three fortunes corresponding to the colors are:

Red: You will become a gold miner.

Green: You will become a forest ranger.

Blue: You will sing the blues.

Besides the main() method, your program must include a method that returns an integer (1-12) representing the zodiac sign (there are 12 zodiac signs). The method should accept as input parameters two integers (a month and day). The prototype for the method should be:

// Returns an integer 1-12 representing the zodiac sign for the given date int GetZodiac (int month, int day);

Your program should also contain a method to print the fortune given an integer (1-3) corresponding to the color chosen by the user. The prototype for the method should be:

// Prints the fortune (1-3)

void PrintFortune (int n); // n=1-3

For example, the output might look like:

What is your name? Elvis Welcome Elvis.

What month were you born on (1-12)? 3

What day of the month were you born on (1-13)? 18

Elvis, I see your sign is Pisces.

Choose your favorite color (1=red, 2=green, 3=blue): 3

You will sing the blues.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you like the code, give it a thumbs up. Incase any clarifications are required, let me know in the comments.

The code starts below this line.

import java.util.Scanner;
public class FortuneTeller {
   
    //GetZodiac function takes 2 int arguments month and day and returns the integer corresponding to the zodiac sign
    //If the days do not match with the days possible in that months, it returns -1
    int GetZodiac (int month, int day){
        if((month==3 && day>=21 && day<=31) || (month==4 && day>0 && day<=19))
            return 1; //return 1 for Aries
        else if((month==4 && day>=20 && day<=30) || (month==5 && day>0 && day<=20))
            return 2; //return 2 for Taurus
        else if((month==5 && day>=21 && day<=31) || (month==6 && day>0 && day<=20))
            return 3; //return 3 for Gemini
        else if((month==6 && day>=21 && day<=30) || (month==7 && day>0 && day<=22))
            return 4; //return 4 for Cancer
        else if((month==7 && day>=23 && day<=31) || (month==8 && day>0 && day<=22))
            return 5; //return 5 for Leo
        else if((month==8 && day>=23 && day<=31) || (month==9 && day>0 && day<=22))
            return 6; //return 6 for Virgo
        else if((month==9 && day>=23 && day<=30) || (month==10 && day>0 && day<=22))
            return 7; //return 7 for Libra
        else if((month==10 && day>=23 && day<=31) || (month==11 && day>0 && day<=21))
            return 8; //return 8 for Scorpio
        else if((month==11 && day>=22 && day<=30) || (month==12 && day>0 && day<=21))
            return 9; //return 9 for Sagittarius
        else if((month==12 && day>=22 && day<=31) || (month==1 && day>0 && day<=19))
            return 10; //return 10 for Capricorn
        else if((month==1 && day>=20 && day<=31) || (month==2 && day>0 && day<=18))
            return 11; //return 11 for Aquarius
        else if((month==2 && day>=19 && day<=29) || (month==3 && day>0 && day<=20))
            return 12; //return 12 for Pisces
        else
            return -1; //return -1 if days do not match with months
    }

    //function main starts
    public static void main(String[] args) {
        String name; //name will store the name of a person
        //month and day will store the birth month and day of that person and 
        //color will store an integer corresponding to the favorite color of that person
        int month, day, color, zodiac; 
        //reader is a Scanner instance that will help in getting user input
        Scanner reader = new Scanner(System.in);
        //fortuneTeller is an instance of FortuneTeller class and this will help in calling GetZodiac function
        //main can not call GetZodiac directly as it is a static function
        FortuneTeller fortuneTeller = new FortuneTeller();

        //Get user input for name and display welcome message
        System.out.print("What is your name? ");
        name = reader.next();
        System.out.println("Welcome "+name+".");

        //get user input for month and display error message if month is incorrect and exit
        System.out.print("What month were you born on (1-12)? ");
        month = reader.nextInt();
        if(month<1 || month>12) {
            System.out.println("In our calendar there are only 12 months. Go find your correct birth date and comeback ");
            System.exit(0);
        }

        //get user input for day and call GetZodiac function to get integer equivalent of the zodiac sign in variable zodiac
        System.out.print("What day of the month were you born on (1-31)? ");
        day = reader.nextInt();
        zodiac = fortuneTeller.GetZodiac(month,day);
        //if zodiac is -1, display the error message and exit
        if(zodiac==-1) {
            System.out.println("Your birth date does not match the number of days in your birth month. Go find your correct birth date and comeback ");
            System.exit(0);
        }
        //otherwise display the zodiac sign using switch statement
        System.out.print(name+", I see your sign is ");
        switch(zodiac){
            case 1:
                System.out.println("Aries");
                break;
            case 2:
                System.out.println("Taurus");
                break;
            case 3:
                System.out.println("Gemini");
                break;
            case 4:
                System.out.println("Cancer");
                break;
            case 5:
                System.out.println("Leo");
                break;
            case 6:
                System.out.println("Virgo");
                break;
            case 7:
                System.out.println("Libra");
                break;
            case 8:
                System.out.println("Scorpio");
                break;
            case 9:
                System.out.println("Sagittarius");
                break;
            case 10:
                System.out.println("Capricorn");
                break;
            case 11:
                System.out.println("Aquarius");
                break;
            case 12:
                System.out.println("Pisces");
                break;
        }

        //get user input for color as an integer
        System.out.print("Choose your favorite color (1=red, 2=green, 3=blue): ");
        color = reader.nextInt();
        //diplay the fortune based on color using the switch case. 
        //Incase the value is invalid, display the error message in default case and exit
        switch(color){
            case 1:
                System.out.println("You will become a gold miner.");
                break;
            case 2:
                System.out.println("You will become a forest ranger.");
                break;
            case 3:
                System.out.println("You will sing the blues.");
                break;
            default:
                System.out.println("You had to enter 1-3 only. Try again from the start");
                System.exit(0);
        }
    }
}

Welcome Elvis. What month were you born on (1-12)? 3 What day of the month were you born on (1-31)? 18 Elvis, I see your sign

Add a comment
Know the answer?
Add Answer to:
Write a Java program for a fortune teller. The program should begin by asking the user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a java program that asks the user to enter an integer. The program should then...

    Write a java program that asks the user to enter an integer. The program should then print all squares less than the number entered by the user. For example, if the user enters 120, the program should display 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100.

  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to input 10 int numbers from the user...

  • Write this is Java. The program will randomly select a color from one of the 5...

    Write this is Java. The program will randomly select a color from one of the 5 green red blue yellow orange. And ask you to guess which was randomly selected. It will select a random color by choosing a random number from 0-4 0 can represent green and 1 red. It will ask you to guess the color randomly selected and reveal what it chose after you guessed. After it will do the same 10 times loop and will display...

  • Please help with 3.12 and 3.14 Write a program that prompts the user to enter the...

    Please help with 3.12 and 3.14 Write a program that prompts the user to enter the length of the star and draw a star, as shown in Figure 3.5a. (Turtle: display a STOP_sign) write a program that displays a STOP sign, as shown in Figure 3.5b. The hexagon is in red and the text is in white. (Turtle: draw the Olympic symbol) write a program that prompts the user to enter the radius of the rings and draws an Olympic...

  • (InputMismatchException) and (ArrayIndexOutOfBoundsException): Using the two arrays shown below, write a program that prompts the user...

    (InputMismatchException) and (ArrayIndexOutOfBoundsException): Using the two arrays shown below, write a program that prompts the user to enter an integer between 1 and 12 and then displays the months and its number of days corresponding to the integer entered. 1. Your program should display “Wrong number - Out of Bounds” if the user enters a wrong number by catching ArrayIndexOutOfBoundsException. 2. Also the program should display “Mismatch – not a number” if the user enters anything other than an integer...

  • Write an interactive Java program, ColorRange.java, which when given a wavelength in nanometers will return the...

    Write an interactive Java program, ColorRange.java, which when given a wavelength in nanometers will return the corresponding color in the visible spectrum. Color Wavelength (nm) Violet 380-450 Blue 451-495 Green 496-570 Yellow 571-590 Orange 591-620 Red 621-750 Task You must implement the following using a suitable decision statement. 1. Use a GUI prompt for the user to enter the wavelength, the wavelength should be of type double. 2. For each range (e.g. 380-450) the number on the left is included...

  • use python Write a program that asks the user for day and month of a birthday....

    use python Write a program that asks the user for day and month of a birthday. The program then tells the Zodiac signs that will be compatible with that birthday. #Example for Aries The Ram    Mar. 21–Apr. 19 day = 24 # user enters this month = 3 # user enters this if ( month == 3 and day >=21) or (month == 4 and day <= 19): print("You are Aries, Fire group, compatible with Aries, Leo, Sagittarius") Zodiac Signs:...

  • Please write a C++ program that will accept 3 integer numbers from the user, and output...

    Please write a C++ program that will accept 3 integer numbers from the user, and output these 3 numbers in order, the sum, and the average of these 3 numbers. You must stop your program when the first number from the user is -7. Design Specifications: (1) You must use your full name on your output statements. (2) You must specify 3 prototypes in your program as follows: int max(int, int, int); // prototype to return maximum of 3 integers...

  • Modular program mathlab: Write a program in Matlab that would continuously ask the user for an...

    Modular program mathlab: Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “You got it” If the user’s die is smaller, it should display, “Nice try” If the results are the...

  • Hello, I am wondering what the source code for this problem would be. Thank you so...

    Hello, I am wondering what the source code for this problem would be. Thank you so much. You will write a java program using the Eclipse IDE. The program will allow a user to perform one of two options. The user can look up the dates for a given zodiac sign or enter a date and find what sign corresponds to that date. SIGN START DATE END DATE Aries 3/21 4/19 Taurus 4/20 5/20 Gemini 5/21 6/20 Cancer 6/21 7/22...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT