Design a class called Date that has integer data members to store month, day and
ID: 3874007 • Letter: D
Question
Design a class called Date that has integer data members to store month, day and year. THe class should have a three parameter defualt constructor that allows the date to be set at the time a new Date object is created. If the user creates a Date object without passing any arguments, or if the values passed are invalid, the default values of 1,1,2001 (i.e. January 1, 2001) should be used.
The class should have member functions to print the date in the following formats:
3/15/16
March 15, 2016
15 March 2016
Explanation / Answer
Following would be class structure. I have also added functionality in main method to set Date and then call member functions to print in required format. The main method section has been commented out using /**....**/. If you want to see how the whole program will run please uncomment it.
import java.text.DateFormatSymbols;
import java.util.Scanner;
public class Date
{
int day;
int month;
int year;
Date(int day, int month, int year)
{
if(day<0 || day >31)
{
this.day = 1;
}
else
{
this.day = day;
}
if(month<1 || month >12)
{
this.month = 1;
}
else
{
this.month = month;
}
//Take length of year
String lenYear = Integer.toString(year);
if(lenYear.length()>4 || lenYear.length()<4 || year<=0 )
{
this.year = 2001;
}
else
{
this.year = year;
}
}
String getMonth()
{
//To get month in textual format
String textMonth = new DateFormatSymbols().getMonths()[this.month-1].toString();
return textMonth;
}
void printDateOne()
{
//To fetch last two digits of year
String yy = Integer.toString(this.year).substring(2);
System.out.println("Date you entered is: "+" " + this.day+"/"+this.month+"/"+yy);
}
void printDateTwo()
{
String month = getMonth();
System.out.println("Date you entered is: "+" "+ month+" "+this.day+","+this.year);
}
void printDateThree()
{
String month = getMonth();
System.out.println("Date you entered is: "+" "+this.day+" "+month+" "+this.year);
}
public static void main(String[] args) {
/*
Scanner s = new Scanner(System.in);
int day, month, year,choice;
System.out.println("Please enter your date");
System.out.println("Day: ");
day = s.nextInt();
System.out.println("Month: ");
month = s.nextInt();
System.out.println("Year: ");
year = s.nextInt();
Date object = new Date(day, month, year);
String option = "y";
while(option.equalsIgnoreCase("y"))
{
System.out.println("Enter the format in which you want to display your date");
System.out.println("1. dd/mm/yy");
System.out.println("2. month day,yyyy");
System.out.println("3. day month yyyy");
choice = s.nextInt();
switch(choice)
{
case 1:
object.printDateOne();
break;
case 2:
object.printDateTwo();
break;
case 3:
object.printDateThree();
break;
default:
System.out.println("Invalid wrong choice");
break;
}
System.out.println("Do you wish to see entered date in another format y/n");
option = s.next();
}**/
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.