Please read the instructions for each part carefully. Comments out the codes and
ID: 3648256 • Letter: P
Question
Please read the instructions for each part carefully. Comments out the codes and their functionality as much as possible.
In the following, we will apply the object-oriented method to develop a simple java program that consists of classes.
Write a multi-class desktop application (two classes). Name the application class (driver) "TestDate." Name the worker class "Date." The Date class represents a day of the year. This class should have a type String data field for month and type int data fields for day and year, a constructor that initializes the above three object variables, three accessor (getter), one mutator (setter) method for month, and a toString() method that return the values of the above three object variables in a string.
After analysis, we decide to use two classes to do the job. The two classes are the worker class and the control (application, tester)class. The worker class is problem specific and the control class is generic. The control class will take care of the setup overhead and manage the interface with users. Finally, the controlclass will create an instance of the worker class, initialize it and run it. The relationship between the worker class and the controlclass is an association relationship. We name the worker class "Date" and the controlclass "TestDate". Using UML notation, we get the following class diagram.
Using the above class diagram we transform the design concepts to the follow java code.
/**
This is a worker class.
The name of the class is Date.
*/
public class Date {
// data fields
private int day;
private String month;
private int year;
// methods
// constructor method.
public Date(int aDay, String aMonth, int aYear) {
day = aDay;
month = aMonth;
year = aYear;
}
public void setMonth(String aMonth) {
month = aMonth;
}
public int getDay() {
return day;
}
public String getMonth() {
return month;
}
public int getYear() {
/* Supply the details of this method.
This method needs to return the value of
Class variable year.
*/
}
public String toString() {
/* Supply the details of this method .
This method needs to return a String with the
following value (in the following, the year, month, and day
should be the actual value of the class variable):
"This date object has the following data: year, month, day"
*/
}
}
Use Eclipse to create a new class "TestDate"
/**
This is a controlclass that create a new instance of
Worker class "Date". It is a test driver.
*/
public class TestDate {
/**
The main method. This is a must for all Java applications.
*/
public static void main(String[] args) {
Date firstDate = new Date(1, "January", 2001);
Date secondDate = new Date(2, "February", 2002);
System.out.println("The first object day is " + firstDate.getDay());
System.out.println("The first object month is " + firstDate.getMonth());
System.out.println("The first object year is " + firstDate.getYear());
System.out.println(firstDate.toString());
System.out.println();
System.out.println("The second object day is " + secondDate.getDay());
System.out.println("The second object month is " + secondDate.getMonth());
System.out.println("The second object year is " + secondDate.getYear());
System.out.println(secondDate.toString());
System.out.println();
firstDate.setMonth("March");
System.out.println(firstDate.toString());
}
}
Compile and run the program.
Explanation / Answer
please rate - thanks
/**
This is a worker class.
The name of the class is Date.
*/
public class Date {
// data fields
private int day;
private String month;
private int year;
// methods
// constructor method.
public Date(int aDay, String aMonth, int aYear) {
day = aDay;
month = aMonth;
year = aYear;
}
public void setMonth(String aMonth) {
month = aMonth;
}
public int getDay() {
return day;
}
public String getMonth() {
return month;
}
public int getYear() {
/* Supply the details of this method.
This method needs to return the value of
Class variable year.
*/
return year;
}
public String toString() {
/* Supply the details of this method .
This method needs to return a String with the
following value (in the following, the year, month, and day
should be the actual value of the class variable):
"This date object has the following data: year, month, day"
*/
return "The day is "+month+"/"+day+"/"+year;
}
}
------------
/**
This is a controlclass that create a new instance of
Worker class "Date". It is a test driver.
*/
public class TestDate {
/**
The main method. This is a must for all Java applications.
*/
public static void main(String[] args) {
Date firstDate = new Date(1, "January", 2001);
Date secondDate = new Date(2, "February", 2002);
System.out.println("The first object day is " + firstDate.getDay());
System.out.println("The first object month is " + firstDate.getMonth());
System.out.println("The first object year is " + firstDate.getYear());
System.out.println(firstDate.toString());
System.out.println();
System.out.println("The second object day is " + secondDate.getDay());
System.out.println("The second object month is " + secondDate.getMonth());
System.out.println("The second object year is " + secondDate.getYear());
System.out.println(secondDate.toString());
System.out.println();
firstDate.setMonth("March");
System.out.println(firstDate.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.