Question

Use "Eclipse IDE for Java EE Developers" Design a class named MyDate. The class contains: The...

Use "Eclipse IDE for Java EE Developers"

Design a class named MyDate. The class contains:

The Date fields year, month, and day that represent a date. month is 0-based, i.e., 0 is for January.

A no argument 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.

Implement the class and 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 day is 18. You may use the GregorianCalendar class in the java.util package, which you can use to obtain the year, month, and day of a date.)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;
import java.util.*;
public class MyDate
{
private static int[] monthLength = {31,28,31,30,31,30,31,31,30,31,30,31};
private static int[] monthOffset = {0,31,59,90,120,151,181,212,243,273,304,334};
private int leapMonth;
private int leapOffset;   
private int debugLevel = 0;
private int date;
MyDate(int year, int month, int day) throws BadDateException
{
if (debugLevel > 2) { System.out.println("MyData instance constructed with year = " + year + " month = " + month + " day = " + day); }
if ((year < 0) ||(year > 199) ||(month < 1) ||(month > 12))
{
throw new BadDateException("-->Error in Year (= " + year +") or Month (= " + month +")");
}
leapMonth = 0;
if (year%4 == 0)
{
if (year > 4)
{
leapOffset = (year-1)/4;
}
else
{
leapOffset = 0;
}
if (month == 2)
{ // Is it February?
leapMonth = 1;
}
else if (month > 2)
{ // Is it after February?
leapOffset += 1; // Can count leap day of this year.
} // Must be January, nothing else required.
}
else
{ // NOT a leap year.
leapOffset = year/4; // Leap days for all previous years, this one doesnt have one.
}
if ((day < 1) ||(day > monthLength[month-1] + leapMonth))
{
throw new BadDateException("-->Error in Day (= " + day +" where Month = " + month +")");
}
date = day + monthOffset[month-1] + leapOffset + year*365; // Actual date calculation.
}
public int getDate()
{
return date;
}
public void setDebugLevel(int dLevel)
{
debugLevel = dLevel;
}
public String toString()
{
return Integer.toString(date);
}
public int between(MyDate someOtherDate)
{
int between = Math.abs(date - someOtherDate.getDate())-1; // basic calculation.
if (between < 0) // have to watch for case where the two dates are the same.
between = 0;
return between;
}
}

Add a comment
Know the answer?
Add Answer to:
Use "Eclipse IDE for Java EE Developers" Design a class named MyDate. The class contains: The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Part I: (The Myate class) Design a class named MyDate. The class contains: • The data...

    Part I: (The Myate 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 hat constructs a MyDate object with the specified year, month, and...

  • In Python - Design a class named Time. The class contains: -Data fields hour, minute, and...

    In Python - Design a class named Time. The class contains: -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 respresent the current time.) -A constructor that constructs a Time object with a specified hour, minute, and second, respectively. -A constructor that constructs a Time object with a specified elapsed time since midnight, Jan 1, 1970, in milliseconds. (The values of...

  • Design a class named StopWatch. The class contains: - Private data fields startTime and endTime with...

    Design a class named StopWatch. The class contains: - Private data fields startTime and endTime with getter methods. - A no-arg constructor that initializes startTime with the current time. - A method named start() that resets the startTime to the cur- rent time. - A method named stop() that sets the endTime to the current time. A method named getElapsedTime() that returns the elapsed time for the stopwatch in milliseconds. - The System.currentTimeMillis() method returns the current number of milliseconds....

  • JAVA ONLY Design two classes: Flight and Itinerary. The Flight class stores the information about a...

    JAVA ONLY Design two classes: Flight and Itinerary. The Flight class stores the information about a flight with the following members: 1. A data field named flightNo of the String type with getter method. 2. A data field named departureTime of the GregorianCalendar type with getter and setter methods. 3. A data field named arrivalTime of the GregorianCalendar type with getter and setter methods. 4. A constructor that creates a Flight with the specified number, departureTime, and arrivalTime. 5. A...

  • Design a class named Account that contains: A private int datafield named id(default 0) A private...

    Design a class named Account that contains: A private int datafield named id(default 0) A private double datafield named balance(default 0) A no-arg constructor that creates default account A constructor that creates an account with specified id & balance The getter and setter methods for id and balance. Create an object of account class using default constructor and update the balance to 2000$ .

  • Design a class named MyPoint to represent a point with x and y-coordinates. The class contains:...

    Design a class named MyPoint to represent a point with x and y-coordinates. The class contains: Two data fields x and y that represent the coordinates. . A no-arg constructor that creates a point (0, 0) .A constructor that constructs a point with specified coordinates. Two get functions for data fields x and y, respectively. A function named distance that returns the distance from this point to another point of the MyPoint type Write a test program that creates two...

  • Java Programming Design a class named Person and its two subclasses named Student and Employee. A...

    Java Programming Design a class named Person and its two subclasses named Student and Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, date hired. Define a class named MyDate that contains the fields year, month, and day. Override the toString method in each class to display the class name and the person's name....

  • . Design a class named MyInteger. The class contains: An int data field named value that...

    . Design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int value. A getter method that returns the int value. The methods isEven(), is Odd(), and isPrime() that return true if the value in this object is even, odd, or prime, respectively. The static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value...

  • (The Account class) Design a class named Account that contains: A private int data field named...

    (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT