Date Class: Design and Implement a class called Date that has data members to st
ID: 3693279 • 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 Assignment5.java 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 Assignment5{
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();
}
}
These codes are prvided
and
Explanation / Answer
Date.java
package org.students;
public class Date {
private int month;
private int day;
private int year;
public Date(int month, int day, int year) {
super();
this.month = month;
this.day = day;
this.year = year;
}
public Date() {
this.month = 1;
this.day = 1;
this.year = 2001;
}
public void showDate1() {
System.out.println("The date is ::" + month + "/" + day + "/" + year);
}
public void setDate(int i, int j, int k) {
this.month = i;
this.day = j;
this.year = k;
}
public void showDate3() {
String mon = "";
if (month == 1) {
mon = "January";
} else if (month == 2) {
mon = "Febrauary";
} else if (month == 3) {
mon = "March";
} else if (month == 4) {
mon = "April";
} else if (month == 5) {
mon = "May";
} else if (month == 6) {
mon = "June";
} else if (month == 7) {
mon = "July";
} else if (month == 8) {
mon = "August";
} else if (month == 9) {
mon = "September";
} else if (month == 10) {
mon = "October";
} else if (month == 11) {
mon = "November";
} else if (month == 12) {
mon = "December";
}
System.out.println("The date is :" + day + " " + mon + " " + year);
}
public void showDate2() {
String mon = "";
if (month == 1) {
mon = "January";
} else if (month == 2) {
mon = "Febrauary";
} else if (month == 3) {
mon = "March";
} else if (month == 4) {
mon = "April";
} else if (month == 5) {
mon = "May";
} else if (month == 6) {
mon = "June";
} else if (month == 7) {
mon = "July";
} else if (month == 8) {
mon = "August";
} else if (month == 9) {
mon = "September";
} else if (month == 10) {
mon = "October";
} else if (month == 11) {
mon = "November";
} else if (month == 12) {
mon = "December";
}
System.out.println("The date is :" + mon + " " + day + "," + year);
}
}
__________________________________________________________________________________________
output:
The date is ::1/1/2001
The date is :Febrauary 12,2010
The date is :29 August 1986
__________________________________________________________________________________________
SimpleCar.java
package org.students;
public class SimpleCar
{
public double enginePower;
public String color;
private int speed;
public SimpleCar()
{
enginePower = 3.2;
color = "White";
speed = 0;
}
public SimpleCar(int iniSpeed, String iniColor, double iniEPower)
{
enginePower = iniEPower;
color = iniColor;
speed = iniSpeed;
}
public SimpleCar(String iniColor)
{
enginePower = 3.2;
speed = 0;
color = iniColor;
}
public void speedUp(int newSpeed)
{
speed = newSpeed;
}
public void paint(String newColor)
{
color = newColor;
}
public String toString()
{
return ("Your car has the engine power " + enginePower + "Currently it is running " + speed + " miles per hour" + "Its color is " + color);
}
public int getCurrentSpeed()
{
return speed;
}
}
__________________________________________________________________________________________
SimpleCarDriver.java
package org.students;
public class SimpleCarDriver
{
public static void main(String args[])
{
//1.creates the SimpleCar object by calling Default constructor.
SimpleCar car1 = new SimpleCar(); //--------------1
//2.creates the SimpleCar object by calling Parameterized constructor.
//The values which we will passed as arguments will be set to the instance variables of SimpleCar class
SimpleCar car2 = new SimpleCar(10, "Red", 3.2); //-----------2
//3.Set the instance variable enginePower of SimpleCar object referenced by car1.
car1.enginePower = 3.6; //------------3
//4.Set the instance variable color of SimpleCar object referenced by car1.
car2.color = "Blue"; //---------4
//5.calling the method of SimpleCar object referenced by car1 by passing the value as argument.
car1.speedUp(100); //---------5
//6.calling the method of SimpleCar object referenced by car1 by passing the value as argument.
car1.paint("Red"); //--------6
//Displaying the contents of SimpleCar object referenced by car1
System.out.println(car1.toString());
//Displaying the contents of SimpleCar object referenced by car2
System.out.println(car2.toString());
}
}
__________________________________________________________________________________________
output:
Your car has the engine power 3.6Currently it is running 100 miles per hourIts color is Red
Your car has the engine power 3.2Currently it is running 10 miles per hourIts color is Blue
___________________________________________________________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.