Create a Date class with integer data members for year, month, and day. Also inc
ID: 3731092 • Letter: C
Question
Create a Date class with integer data members for year, month, and day. Also include a string data member for the name of the month. Include a method that returns the month name (as a string) as part of the date. Separate the day from the year with a comma in that method. iInclude appropriate constructors, properties, and methods. Override theToString() method to display the date formatted withslashes(/) separating the month, day, and year. Create a second class that instanatiates and test the Date class.
Okay so I need to do this but with a twist
I need to inlcude a tryparse method for month that allows for usser valdation and a loop that allows me reenter the date until the correct date is givin
and a switch stamtment for month name for the display as String.
Please help me with this I just need a full programm that makes sense
Explanation / Answer
package example;
import java.util.Scanner;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class date {
public int day;
public int year;
public String monthStr;
public int month;
public static void main(String args[])
{
Scanner sc =new Scanner(System.in);
date m=new date();//object of the class
System.out.println("Please select month ");
System.out.println("1.Jan 2.Feb 3.March 4.April 5.May 6.June 7.July 8.August 9.September 10.October 11.November 12. December ");
int month=sc.nextInt();//month entered by user
m.month(month);//switch case is used in this method
System.out.println("Enter day ");
m.day= sc.nextInt();
System.out.println("Enter year");
m.year=sc.nextInt();
if(m.dayValidation==true && m.yearValidation==true)//validation
System.out.println("entered date is "+month+"/"+m.day+"/"+m.year);
else
System.out.println("Enter valid date");
}
public void month(int month){
switch(month){
case 1: monthStr="Jan"; break;
case 2: monthStr="Feb"; break;
case 3: monthStr="March"; break;
case 4: monthStr="April"; break;
case 5: monthStr="May"; break;
case 6: monthStr="June"; break;
case 7: monthStr="July"; break;
case 8: monthStr="August"; break;
case 9: monthStr="September"; break;
case 10: monthStr="October"; break;
case 11: monthStr="November"; break;
case 12: monthStr="December"; break;
}
}
public int getDay(){
return day;
}
public void setDay(int day)
{ day=this.day ;}
//Checks for validation of day
public boolean dayValidation(){
if(month%2==0)//month even value
if(month!=8 ||month!=2)//checkes if it is August or feb
{ if(day>30)
return false;
else return true;}else return false;
else if(month%2!=0||month==8)
{if(day==31)
return true;else return false;
}
else return false;
}
public boolean yearValidation(){
Date date=new Date();
if(year>=1900 && year <=date.getYear())
return true;
else return false;
}
}
Output: