Question: Learning Objectives: Refresher for basic Java features and the use of classes and objects. Create your own Date class (not the existing Java API Date
Learning Objectives: Refresher for basic Java features and the use of classes and objects.
Create your own Date class (not the existing Java API Date class) that can output the date in multiple formats. You may assume that the user enters the data correctly and that there is not a leap year. Output the date entered by the user in all three of the following formats (with today's date as an example):
MM/DD/YYYY (01/26/2021)
Month name, DD, YYYY (January 26, 2021)
DDD YYYY (026 2021)
Use overloaded constructors to create Date objects initialized with dates of the formats and content enters by the user. There should be a default constructor, the second constructor should receive three integer values, the third a string and two integers, the fourth two integers.
The user should get a menu of options to enter the date in any one of the three formats. Then the program will convert that entry and display all three formats with a well formatted output to the display. The program should loop continually until the user is done entering dates and chooses to exit.
Your files should include the Date class and a DateTest class which contains the code to test your class functions.
My code: NEED HELP TO RUN THE CODE.
public class Date{ private int day; private int month; private int year; public Date(){ day=month=year=0; } public Date(int d,int m,int y){ day=d; month=m; year=y; } public Date(int d,String m,int y){ day=d; year=y; if(m.equals("January")){ month=1; } else if(m.equals("February")){ month=2; } else if(m.equals("March")){ month=3; } else if(m.equals("April")){ month=4; } else if(m.equals("May")){ month=5; } else if(m.equals("June")){ month=6; } else if(m.equals("July")){ month=7; } else if(m.equals("August")){ month=8; } else if(m.equals("September")){ month=9; } else if(m.equals("October")){ month=10; } else if(m.equals("November")){ month=11; } else { month=12; } } } public date(int day,int yr) { year=yr; if(day<=31){ month=1; day=days; } else if(days<=59){ month=2; day=days-31; } else if(days<=90){ month=3; day=days-59 } else if(days<=120){ month=4; day=days-90; } else if(days<=151){ month=5; day=days-120; } else if(days<=181){ month=6; day=days-151; } else if(days<=212){ month=7; day=days-181; } else if(days<=243){ month=8; day=days-212; } else if(days<=273){ month=9; day=days-242; } else if(days<=304){ month=10; day=days-273; } else if(days<=334){ month=11; day=days-304; } else { month=12; day=days-334; } } public String toString(){ String str="DD/MM/YYYY:"+month+"/"+day+"/"+year+" Month DD,YYYY:"; if(month==1){ str+="January"; } else if(month==2){ str+="Ferbruary"; } else if(month==3){ str+="March"; } else if(month==4){ str+="April"; } else if(month==5){ str+="May"; } else if(month==6){ str+="June"; } else if(month==7){ str+="July"; } else if(month==8){ str+="Augst"; } else if(month==9){ str+="September"; } else if(month==10){ str+="October"; } else if(month==11){ str+="November"; } else { str+="December"; } str+=day+","+year+" DDD YYYY:"; str+=((month-1)*30+day)+""+year; return str; } import java.util.Scanner; public class DateTest{ public static void main(String[]args){ Scanner sc=new Scanner(System.in); System.out.println("This program converts dates entered to multiple formats."); System.out.println("Enter your date in the format you choose and all three formats provided as an output."); int opt=getOptions(); while(opt!=4){ if(opt==1){ System.out.print("Enter Month (1-12):"); int m=sc.nextInt(); System.out.print("Enter Day of Month:"); int d=sc.nextInt(); System.out.print("Enter Year:"); int y=sc.nextInt(); Date date=new Date(d,m,y); System.out.println(date); } else if(opt==2){ sc.nextLine(); System.out.print("Enter Month Name:"); String m=sc.nextLine(); System.out.print("Enter Day of Month:"); int d=sc.nextInt(); System.out.print("Enter Year:"); int y=sc.nextInt(); Date date=new Date(d,m,y); System.out.println(date); } else if(opt==3){ System.out.print("Enter Day of Year:"); int d=sc.nextInt(); System.out.print("Enter Year:"); int y=sc.nextInt(); Date date=new Date(d,y); System.out.println(date); } opt=getOptions(); } System.out.println("Thank You for Using my Program."); } public static in getOptions(){ Scanner sc=new Scanner(System.in); System.out.println("Enter 1 for format: MM/DD/YYYY "+ "Enter 2 for Format: Month DD, YYYY "+ "Enter 3 for Format: DDD YYYY/n"+ "Enter 4 to Exit"); System.out.print("Choice:"); int ch=sx.nextInt(); while(ch<1||ch>4){ System.out.println("Wrong choice!!Try again..."); System.out.print("Choice:"); ch=sc.nextInt(); } return ch; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
