Hello, I am wondering what the source code for this problem would be. Thank you
ID: 3880098 • Letter: H
Question
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
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
Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
//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!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.