Object-oriented Programming
Problem with solution 2
1. (The MyDate class) Design a class named MyDate. The class contains:
▪ The data fields year, month, and day that represent a date. month is 0-based, i.e., 0 is
for January.
▪ A no-arg constructor that creates a MyDate object for the current date.
▪ A constructor that constructs a MyDate object with a specified elapsed time since
midnight, January 1, 1970, in milliseconds.
▪ A constructor that constructs a MyDate object with the specified year, month, and
day.
▪ Three getter methods for the data fields year, month, and day, respectively.
▪ A method named setDate(long elapsedTime) that sets a new date for the object using
the elapsed time. *
Write a test program that creates two MyDate objects (using new MyDate() and new
MyDate(34355555133101L)) and displays their year, month, and day. (Hint: The first
two constructors will extract the year, month, and day from the elapsed time. For
example, if the elapsed time is 561555550000 milliseconds, the year is 1987, the month
is 9, and the day is 18. You may use the GregorianCalendar class discussed in
Programming Exercise 9.5 to simplify coding.)
public class TestMyDate {
/** Main method */
public static void main(String[] args) {
// Create two MyDate objects
MyDate date1 = new MyDate();
MyDate date2 = new MyDate(34355555133101L);
[Link]("Date1: " + [Link]() + "/" + [Link]() +
"/" + [Link]());
[Link]("Date2: " + [Link]() + "/" + [Link]() +
"/" + [Link]());
}
}
// Implement MyDate class
public class MyDate {
private int year; // Data Fields
private int month;
private int day;
MyDate() { /** Creates a MyDate object for the current date */
GregorianCalendar calander = new GregorianCalendar();
year = [Link]([Link]);
month = [Link]([Link]);
day = [Link](GregorianCalendar.DAY_OF_MONTH);
}
/** Creates a MyDate object with a specified elapsed time
*since midnight, January 1, 1970, in milliseconds */
MyDate(long elapsedTime) {
setDate(elapsedTime);
}
/** Creates a MyDate object with the specified year, month, and day */
MyDate(int year, int month, int day) {
[Link] = year;
[Link] = month;
[Link] = day;
}
public int getYear() {
return year; /** Return year */
}
public String getMonth() { /** Return month */
String m = [Link](month + 1);
return (month < 10 ? "0" + m : m);
}
public String getDay() {
String d = [Link](day);
return (day < 10 ? "0" + d : d);
}
/** Sets a new date for the object using the elapsed time */
public void setDate(long elapsedTime) {
GregorianCalendar calander = new GregorianCalendar();
[Link](elapsedTime);
year = [Link]([Link]);
month = [Link]([Link]);
day = [Link](GregorianCalendar.DAY_OF_MONTH);
}
}
2. (The Time class) Design a class named Time. The class contains:
▪ The data fields hour, minute, and second that represent a time.
▪ A no-arg constructor that creates a Time object for the current time. (The values of
the data fields will represent the current time.)
▪ A constructor that constructs a Time object with a specified elapsed time since
midnight, January 1, 1970, in milliseconds. (The values of the data fields will
represent this time.)
▪ A constructor that constructs a Time object with the specified hour, minute, and
second.
▪ Three getter methods for the data fields hour, minute, and second, respectively.
▪ A method named setTime(long elapseTime) that sets a new time for the object
using the elapsed time. For example, if the elapsed time is 555550000 milliseconds,
the hour is 10, the minute is 19, and the second is 10.
Write a test program that creates two Time objects (using new Time() and new
Time(555550000)) and displays their hour, minute, and second in the format
hour:minute:second. (Hint: The first two constructors will extract the hour, minute, and
second from the elapsed time. For the no-arg constructor, the current time can be
obtained using [Link](), as shown in Listing 2.7,
[Link].)
public class TestTime {
/** Main method */
public static void main(String[] args) {
// Create two Time objects
Time time1 = new Time();
Time time2 = new Time(555550000);
// Display Time objects hour, minute, and
// second in the format hour:minute:second
[Link]([Link]() + ":" + [Link]() +
":" + time1. getSecond());
[Link]([Link]() + ":" + [Link]() +
":" + time2. getSecond());
}
}
// Implement Time class
public class Time {
// Data fields
private long hour;
private long minute;
private long second;
// Creates a Time object for the current time
Time() {
this(0);
}
// Constructs a Time object with a specified elapsed
// time since midnight, January 1, 1970, in milliseconds.
Time(long elapseTime) {
setTime(elapseTime);
}
// Constructs a Time object with the specified hour, minute, and second
Time(long hour, long minute, long second) {
[Link] = hour;
[Link] = minute;
[Link] = second;
}
// Return hour
public long getHour() {
return hour;
}
// Return minute
public long getMinute() {
return minute;
}
// Return second
public long getSecond() {
return second;
}
// Sets a new time for the object using the elapsed time
public void setTime(long elapseTime) {
long totalMilliseconds = [Link]();
long totalSeconds = totalMilliseconds / 1000;
second = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
minute = totalMinutes % 60;
long totalHours = totalMinutes / 60;
hour = totalHours % 24;
if (elapseTime > 0) {
totalSeconds = elapseTime / 1000;
second = totalSeconds % 60;
totalMinutes = totalSeconds / 60;
minute = totalMinutes % 60;
totalHours = totalMinutes / 60;
hour = totalHours % 24;
}
}
}
3. (The Triangle class) Design a class named Triangle that extends GeometricObject. The
class contains:
▪ Three double data fields named side1, side2, and side3 with default values 1.0 to
denote three sides of the triangle.
▪ A no-arg constructor that creates a default triangle.
▪ A constructor that creates a triangle with the specified side1, side2, and
▪ The accessor methods for all three data fields.
▪ A method named getArea() that returns the area of this triangle.
▪ A method named getPerimeter() that returns the perimeter of this triangle.
▪ A method named toString() that returns a string description for the triangle.*
The toString() method is implemented as follows: * return "Triangle: side1 = " + side1 +
" side2 = " + side2 +" side3 = " + side3;
Write a test program that prompts the user to enter three sides of the triangle, a color, and
a Boolean value to indicate whether the triangle is filled. The program should create a
Triangle object with these sides and set the color and filled properties using the input.
The program should display the area, perimeter, color, and true or false to indicate
whether it is filled or not.
import [Link];
public class TestGeometricObjectTriangle {
public static void main(String[] args) {
// Create a Scanner object
Scanner input = new Scanner([Link]);
// Prompt the user to enter three sides of the triangle
[Link]("Enter three side of the triangle: ");
double side1 = [Link]();
double side2 = [Link]();
double side3 = [Link]();
// Prompt the user to enter a color
[Link]("Enter a color: ");
String color = [Link]();
// Prompt the user to enter whether the triangle is filled
[Link]("Is the triangle filled (true / false)? ");
boolean filled = [Link]();
// Create triangle object with user input
Triangle triangle = new Triangle(side1, side2, side3);
[Link](color);
[Link](filled);
[Link]([Link]());
[Link]("Area: " + [Link]());
[Link]("Perimeter: " + [Link]());
[Link]("Color: " + [Link]());
[Link]("Triangle is" + ([Link]() ? "" : " not ")
+ "filled");
}
}
// Implement GeometricObject class
public class GeometricObject {
private String color = "white";
private boolean filled;
private [Link] dateCreated;
public GeometricObject() {/** Construct a default geometric object */
dateCreated = new [Link]();
}
/** Construct a geometric object with the specified color * and filled value */
public GeometricObject(String color, boolean filled) {
dateCreated = new [Link]();
[Link] = color;
[Link] = filled;
}
public String getColor() { /** Return colot */
return color;
}
public void setColor(String color) {/** Set a new color */
[Link] = color;
}
/** Return filled. Since filled is boolean, its getter method is named isFilled */
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {/** Set a new filled */
[Link] = filled;
}
public [Link] getDateCreated() {/** Get dateCreated */
return dateCreated;
}
public String toString() {/** Return a string representation of this object */
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
}
// Implement Triangle class
public class Triangle
extends GeometricObject {
private double side1;
private double side2;
private double side3;
Triangle() {/** Construct default Triangle object */
side1 = side2 = side3 = 1.0;
}
/** Construct a triangle with specified side1, side2 and side3 */
Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() { /** Return side1 */
return side1;
}
public double getSide2() { /** Return side2 */
return side2;
}
public double getSide3() {/** Return side3 */
return side3;
}
public double getArea() {/** Return area of this triangle */
double s = (side1 + side2 + side3) / 2;
return [Link](s * (s - side1) * (s - side2) * (s - side3));
}
public double getPerimeter() {/** Return perimeter of this triangle */
return side1 + side2 + side3;
}
/** Return a string description for the triangle */
public String toString() {
return "Triangle: side1 = " + side1 + " side2 = " + side2 +
" side3 = " + side3;
}
}