Question

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

Leo

7/23

8/22

Virgo

8/23

9/22

Libra

9/23

10/22

Scorpio

10/23

11/21

Sagittarius

11/22

12/21

Capricorn

12/22

1/19

Aquarius

1/20

2/18

Pisces

2/19

3/20

Your program will use a Date class to store dates.

The Date class should have fields for the month and the day.

Be sure to include appropriate constructors, getters and setters.

You must override the equals() method.

You must also supply at least one other method to compare dates (i.e. a lessThan method).

Your program should a Zodiac class which will be responsible for creating the above table and for the lookup methods.

You MUST use three arrays to create the zodiac table (and not ArrayList or any other type).

One array will hold a string (the zodiac sign).

A second array will hold the start date of the sign.

A third array will hold the end date of the sign.

The constructor is responsible for initializing the table.

There should be a method to find the sign given a date.

There should be a method to find the start date given a sign.

There should be a method to find the end date given a sign.

Your program should have a ZodiacProgram class that is responsible for retrieving user input and using the Zodiac class to lookup the requested information.

One field will be a Zodiac object (you may have others).

The constructor will create the Zodiac object (and possibly others).

There should be one public method called start(). This method will be responsible for running the zodiac program.

The structure of the start method is as follows:

Do

{

               Ask user which operation is requested

               If (user requests to look up dates by sign)

                              Ask user to input sign

                              Lookup dates for given sign

                              If (found)

                                             Ouput dates

                              Else

                                             Output “invalid sign”

               Else

                              Do

                              {

                                             Ask user to input date

                              } while (date is invalid)

                              Lookup sign for given date

                              Output sign

               Ask user if they want to continue

} while (user wants to continue)

              

Be sure to use private methods so that your start method is not too long and does not perform too many tasks directly.

Be sure to use appropriate prompts for getting user input.

The user should not have to worry about case when entering a sign (Hint: look at the String class).

Be sure to format your output in a readable manner

Your program should have a Driver class with a main method that simply creates a ZodiacProgram object and invokes the start() method

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

Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.

  1. Created a Date class, with month and day attributes, implemented required methods and constructors, also verified the inputs before assigning them to the attributes.
  2. Created a class Zodiac, with three arrays-signs, startdates and enddates. Initialized all three arrays in a constructor, implemented methods to find a sign of a given date, find the start date or end date of a given sign.
  3. Created a class ZodiacProgram.java, defined a Zodiac object and a scanner. Implemented constructor and start() method according to the requirements. Implemented some private methods for gathering user input for the date and sign.
  4. Created a Driver class with a main method, started the zodiac program and verified the outputs.
  5. Comments are included, If you have any doubts, feel free to ask, Thanks

//Date.java

public class Date {

      /**

      * an array which keeps the track of number of days in a month, (not

      * including leap year), so that it can be accessed with month number as the

      * index. i.e daysInMonths[2] will contain the number of days in February

      */

      private static int[] daysInMonths = { 0, 31, 28, 31, 30, 31, 30, 31, 31,

                  30, 31, 30, 31 };

      private int month;

      private int day;

      // constructor

      public Date(int month, int day) {

            setMonth(month);

            this.day = day;

      }

      public int getMonth() {

            return month;

      }

      public void setMonth(int month) {

            /**

            * Verifying the month before assigning,

            * in case of invalid month, using default month

            *

            */

            if (month < 1 || month > 12) {

                  month = 1;

                  System.out.println("Invalid month, using default value");

            } else {

                  this.month = month;

            }

      }

      public int getDay() {

            return day;

      }

      public void setDay(int day) {

            /**

            * Verifying the day before assigning

            */

            if(day<1 || day>daysInMonths[month]){

                  System.out.println("Invalid day, using default value");

                  day=1;

            }else{

                  this.day=day;

            }

      }

      /**

      * method to check if two dates are equal

      */

      @Override

      public boolean equals(Object o) {

            if(o instanceof Date){

                  Date d=(Date) o;

                  if(d.day==this.day && d.month==this.month){

                        return true;

                  }

            }

            return false;

      }

      /**

      * method to check if a given date is less than this date

      */

      public boolean lessThan(Date d){

            /**

            * Comparing months first

            */

            if(this.month<d.month){

                  return true;

            }else if(this.month>d.month){

                  return false;

            }else{

                  /**

                  * months are equal, so comparing day

                  */

                  if(this.day<d.day){

                        return true;

                  }else{

                        return false;                      

                  }

            }

      }

      /**

      * method to check if a given date is greater than this date

      */

      public boolean greaterThan(Date d){

            /**

            * Comparing months first

            */

            if(this.month>d.month){

                  return true;

            }else if(this.month<d.month){

                  return false;

            }else{

                  /**

                  * months are equal, so comparing day

                  */

                  if(this.day>d.day){

                        return true;

                  }else{

                        return false;                      

                  }

            }

      }

      @Override

      public String toString() {

            return month+"/"+day;

      }

}

//Zodiac.java

public class Zodiac {

      private String[] signs;

      private Date[] startDates;

      private Date[] endDates;

      //constructor

      public Zodiac() {

            /**

            * Initializing the arrays

            */

            signs = new String[] { "Aries", "Taurus", "Gemini", "Cancer", "Leo",

                        "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn",

                        "Aquarius", "Pisces" };

            startDates = new Date[] { new Date(3, 21), new Date(4, 20),

                        new Date(5, 21), new Date(6, 21), new Date(7, 23),

                        new Date(8, 23), new Date(9, 23), new Date(10, 23),

                        new Date(11, 22), new Date(12, 22), new Date(1, 20),

                        new Date(2, 19) };

            endDates = new Date[] { new Date(4, 19), new Date(5, 20),

                        new Date(6, 20), new Date(7, 22), new Date(8, 22),

                        new Date(9, 22), new Date(10, 22), new Date(11, 21),

                        new Date(12, 21), new Date(1, 19), new Date(2, 18),

                        new Date(3, 20) };

      }

      /**

      * method to find the sign, given a date

      */

      public String findSign(Date d){

            int index=-1;

            for(int i=0;i<signs.length;i++){

                  if(startDates[i].equals(d) || endDates[i].equals(d)){

                        /**

                        * checking if d is either a startDate or endDate of a sign

                        */

                        index=i;

                  }else if(d.greaterThan(startDates[i]) && d.lessThan(endDates[i])){

                        /**

                        * checking if d is in between the startDate and endDate of a sign

                        */

                        index=i;

                  }

            }

            if(index!=-1){

                  return signs[index];

            }else{

                  return null;

            }

      }

      /**

      * method to find the startingDate, given a sign

      * returns null if invalid sign given

      */

      public Date findStartDate(String sign){

            int index=-1;

            for(int i=0;i<signs.length;i++){

                  if(sign.equalsIgnoreCase(signs[i])){

                        /**

                        * found

                        */

                        index=i;

                  }

            }

            if(index!=-1){

                  /**

                  * returning the start date at the found index

                  */

                  return startDates[index];

            }else{

                  /**

                  * Invalid sign

                  */

                  return null;

            }

      }

      /**

      * method to find the end date, given a sign

      * returns null if invalid sign given

      */

      public Date findEndDate(String sign){

            int index=-1;

            for(int i=0;i<signs.length;i++){

                  if(sign.equalsIgnoreCase(signs[i])){

                        /**

                        * found

                        */

                        index=i;

                  }

            }

            if(index!=-1){

                  /**

                  * returning the end date at the found index

                  */

                  return endDates[index];

            }else{

                  /**

                  * Invalid sign

                  */

                  return null;

            }

      }

     

}

//ZodiacProgram.java

import java.util.Scanner;

public class ZodiacProgram {

      private Zodiac zodiac;

      private Scanner scanner;

      public ZodiacProgram() {

            /**

            * initializing the zodiac

            */

            zodiac = new Zodiac();

            /**

            * initializing scanner object to receive user input

            */

            scanner = new Scanner(System.in);

      }

      /**

      * method to start the program and interact with the user

      */

      public void start() {

            boolean quit = false;

            do {

                  System.out.println("1. Look up dates by sign");

                  System.out.println("2. Look up sign by date");

                  System.out.println("3. Exit");

                  System.out.println("# Please enter your choice...");

                  try {

                        /**

                        * getting user choice

                        */

                        int choice = Integer.parseInt(scanner.nextLine());

                        switch (choice) {

                        case 1:

                              lookForDates();

                              break;

                        case 2:

                              lookForSign();

                              break;

                        case 3:

                              System.out.println("Thank you!");

                              quit=true;

                              break;

                        default:

                              System.out.println("That's not a valid choice");

                              break;

                        }

                  } catch (Exception e) {

                        System.out.println("Invalid input");

                  }

            } while (!quit);

      }

      /**

      * method to get a sign from user and checks for the start and end dates,

      * will display an error if the sign is not valid

      */

      private void lookForDates() {

            System.out.println("Enter sign: ");

            String sign = scanner.nextLine();

            Date startDate = zodiac.findStartDate(sign);

            Date endDate = zodiac.findEndDate(sign);

            if (startDate != null && endDate != null) {

                  System.out.println(sign + ", Start date: " + startDate

                              + ", End date: " + endDate);

            } else {

                  System.out.println("Invalid sign");

            }

      }

      /**

      * method to get a date from user and checks for the sign,

      * will display an error if the date is not valid

      */

      private void lookForSign(){

           

            System.out.println("Enter month: ");

            int month=Integer.parseInt(scanner.nextLine());

            if(month<1 || month>12){

                  System.out.println("Invalid month");

                  return;

            }

            System.out.println("Enter day: ");

            int day=Integer.parseInt(scanner.nextLine());

            if(day<1){

                  System.out.println("Invalid day");

                  return;

            }

            Date date=new Date(month, day);

            String sign=zodiac.findSign(date);

            System.out.println("Zodiac sign for the date "+date+" is "+sign);

           

      }

}

//Driver.java

public class Driver {

      public static void main(String[] args) {

            /**

            * starting the ZodiacProgram

            */

            ZodiacProgram program=new ZodiacProgram();

            program.start();

      }

}

/*OUTPUT*/

1. Look up dates by sign

2. Look up sign by date

3. Exit

# Please enter your choice...

1

Enter sign:

Pisces

Pisces, Start date: 2/19, End date: 3/20

1. Look up dates by sign

2. Look up sign by date

3. Exit

# Please enter your choice...

2

Enter month:

6

Enter day:

22

Zodiac sign for the date 6/22 is Cancer

1. Look up dates by sign

2. Look up sign by date

3. Exit

# Please enter your choice...

1

Enter sign:

Ariesse

Invalid sign

1. Look up dates by sign

2. Look up sign by date

3. Exit

# Please enter your choice...

3

Thank you!

Add a comment
Know the answer?
Add Answer to:
Hello, I am wondering what the source code for this problem would be. Thank you so...
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 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...

  • Hello. I am using a Java program, which is console-baed. Here is my question. Thank you....

    Hello. I am using a Java program, which is console-baed. Here is my question. Thank you. 1-1 Write a Java program, using appropriate methods and parameter passing, that obtains from the user the following items: a person’s age, the person’s gender (male or female), the person’s email address, and the person’s annual salary. The program should continue obtaining these details for as many people as the user wishes. As the data is obtained from the user validate the age to...

  • All code will be in Java, and there will be TWO source code. I. Write the...

    All code will be in Java, and there will be TWO source code. I. Write the class  MailOrder to provide the following functions: At this point of the class, since we have not gone through object-oriented programming and method calling in detail yet, the basic requirement in this homework is process-oriented programming with all the code inside method processOrderof this class. Inside method processOrder, we still follow the principles of structured programming.   Set up one one-dimensional array for each field: product...

  • This should be in Java Create a simple hash table You should use an array for...

    This should be in Java Create a simple hash table You should use an array for the hash table, start with a size of 5 and when you get to 80% capacity double the size of your array each time. You should create a class to hold the data, which will be a key, value pair You should use an integer for you key, and a String for your value. For this lab assignment, we will keep it simple Use...

  • Hello there, So I got an assignment that I just cant get further with. So trying...

    Hello there, So I got an assignment that I just cant get further with. So trying to start from scratch again.. So im supposed to make a class with a UML aswell and a main file. This is how far ive came, https://imgur.com/a/MGqUN , Thats the car class code and the UML. This is the assignment Make a program where you can handle different cars at a car company. The user should be able to choose do the following: a)...

  • Hello I am confused about this C++ program. I need to create a payroll C++ program...

    Hello I am confused about this C++ program. I need to create a payroll C++ program following these steps. Source file structure Your program will consist of three source files: main.cpp the main program Payroll.hppclass declaration for the Payroll class. Make sure you have an include guard. Payroll.cpp Payroll's member functions Class definition Create a Payroll class definition. The class has private members which originate from user input: number of hours worked hourly rate float float - and private members...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • how would i write code in java/netbeans for this: You will be creating the driver class...

    how would i write code in java/netbeans for this: You will be creating the driver class for this homework (PA05_TVs.java) and the object/element class, TV.java. Your TV class should include only a channel value for instance data (3-99) and the following methods: constructor [requires an initial channel value], getter, setter, randomChannel() [does not return the new channel value], and toString(). The driver class must complete the following: Instantiate a TV object and set the initial channel to 97. Instantiate a...

  • General Requirements: • You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; • You sho...

    General Requirements: • You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; • You should create identifiers with sensible names; • You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve. • Logical structures and statements are properly used for specific purposes. Objectives This assignment requires you to write...

  • Hello, Could you please input validate this code so that the code prints an error message...

    Hello, Could you please input validate this code so that the code prints an error message if the user enters in floating point numbers or characters or ANYTHING but VALID ints? And then could you please post a picture of the output testing it to make sure it works? * Write a method called evenNumbers that accepts a Scanner * reading input from a file with a series of integers, and * report various statistics about the integers to the...

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