Date Class: Design and Implement a class called Date that has data members to st
ID: 3633209 • Letter: D
Question
Date Class:
Design and Implement a class called Date that has data members to store month (as a number), day, year, and name of the month. The class should have a three-parameter constructor that allows the data to be set at the time of new Data object instances are created. Default constructor that does not take any parameters should set the default values of 1 (month), 1 (day), 2001 (year). The class should have following three member functions to display date following formats
showDate1() should display the date in 1/1/2001 format
showDate2() should display the date in January 1, 2001 format
showDate3() should display the date in 1 January 2001 format
Also the class should have method to set the date (setDate()). This method should take month, day, and year as parameters and set the object instance data member values. Then your Date class should work with the Assignment6.cpp given below and produces the following output.
1/1/2001
February 12, 2010
29 August 1986
Press any key to continue . . .
Assignment5.java
public class Assignment6{
public static void main(String[] args){
Date d1 = new Date();
Date d2= new Date(2, 12, 2010);
d1.showDate1();
d2.showDate2();
d1.setDate(8, 29, 1986);
d1.showDate3();
}
}
Explanation / Answer
public class Date { public int day; public int month; public int year; Date(int d,int m,int y) { day=d; month=m; year=y; } void showdate1() { System.out.println(day+"/"+month+"/"+year); } void showdate2() { String monthname=""; switch(month) { case 1: monthname=monthname+"January"; break; case 2: monthname=monthname+"February"; break; case 3: monthname=monthname+"March"; break; case 4: monthname=monthname+"April"; break; case 5: monthname=monthname+"May"; break; case 6: monthname=monthname+"June"; break; case 7: monthname=monthname+"July"; break; case 8: monthname=monthname+"August"; break; case 9: monthname=monthname+"septermber"; break; case 10: monthname=monthname+"October"; break; case 11: monthname=monthname+"November"; break; case 12: monthname=monthname+"December"; break; } System.out.println(monthname+day+""+year); } void showdate3() { String monthname=""; switch(month) { case 1: monthname=monthname+"January"; break; case 2: monthname=monthname+"February"; break; case 3: monthname=monthname+"March"; break; case 4: monthname=monthname+"April"; break; case 5: monthname=monthname+"May"; break; case 6: monthname=monthname+"June"; break; case 7: monthname=monthname+"July"; break; case 8: monthname=monthname+"August"; break; case 9: monthname=monthname+"septermber"; break; case 10: monthname=monthname+"October"; break; case 11: monthname=monthname+"November"; break; case 12: monthname=monthname+"December"; break; } System.out.println(day+""+monthname+""+year); } } import java.lang.String; public class Assignment6{ public static void main(String args[]){ Date d1 = new Date(); Date d2= new Date(2, 12, 2010); d1.showDate1(); d2.showDate2(); d1.setDate(8, 29, 1986); d1.showDate3(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.