Question

Java Question Q)There are 3 instance variables. • month – an integer in the range 1-12....

Java Question

Q)There are 3 instance variables.

• month – an integer in the range 1-12.

• day - which stores an integer in the range 1-31

• year - any year between 2000 and 2025. Date provides the following functionality.

1. a zero-parameter constructor that stores the data representing Jan 1, 2000.

2. a parameterized constructor that accepts three parameters, one for each instance variable, and stores them if they are within acceptable ranges, or sets the date to Jan 1, 2000, if any value is out of range

3. a toString method that returns the date representation in the form yyyy-mm-day. For example, October 31, 2020, is represented as "2020-10-30"

4. • Add a boolean method comesBefore which accepts a Date as parameter and returns true if the date it is called on comes before the data that is sent as a parameter. For example, if date1 represents October 22, 2020, and date 2 represents October 30, 2020 then o data1.comesBefore(date2) return true o date2.comesBefore(date1) returns false o date1(comesBefore(date1) returns false

• Add a void (mutator) method nextDay that increments the day by 1. (For this problem we will assume that all months have 31 days.) For example, if the current date is February 31, 2020, then nextDay, will change the date to March 1, 2020. o if the current date is December 31, 2020, then nextDay, will change the date to January 1, 2021.

Homework Answers

Answer #1

CODE IN JAVA:


public class Date {
   int month ;
   int day ;
   int year ;
  
   public Date() {
       this.month = 1 ;
       this.day = 1 ;
       this.year = 2020 ;
   }

   public Date(int month, int day, int year) {
       if(month >=1 && month <= 12 && day >= 1 && day <= 31 && year >= 202 && year <= 2025) {
           this.month = month;
           this.day = day;
           this.year = year;
       }
       else {
           this.month = 1 ;
           this.day = 1 ;
           this.year = 2020 ;
       }

   }
   public String toString() {
       return this.year + "-" + this.month + "-" + this.day ;
   }
   public boolean comesBefore(Date d) {
       if(this.year < d.year)
           return true ;
       if(this.year == d.year) {
           if(this.month < d.month)
               return true;
           if(this.month == d.month) {
               if(this.day < d.day)
                   return true;
               return false ;
           }
           return false ;
       }
       return false ;
   }
   public void nextDay() {
       if(this.day != 31) {
           this.day += 1 ;
       }
       else {
           this.day = 1 ;
           if(this.month != 12)
               this.month += 1 ;
           else {
               this.month = 1 ;
               this.year += 1 ;
           }
       }
   }
  
}

Know the answer?
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
Similar Questions
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Using Java Write a class called Dog that contains instance data that represents the dog’s name,...
Using Java Write a class called Dog that contains instance data that represents the dog’s name, breed, weight, birth date, and medical history. Define the Dog constructor to accept and initialize instance data (begin the medical history with an empty line). Include accessor and mutator methods every attribute. For the medical history in particular, define the mutator method to simply add strings to the medical history so the user sees a printout of information about the dog (be mindful of...
java 1) Create a mutator method called setPosition that accepts one integer parameter. Update the position...
java 1) Create a mutator method called setPosition that accepts one integer parameter. Update the position variable by adding the position to the parameter variable. 2)debug the code public class food { public static void main(String[] args) { Fruits apple = new Fruits(20); // Write the statement to call the method that will increase the instance variable position by 6. Fruits.setPosition(6); apple.getPosition(); } }
Create your own date class. Create a MyDate class with these methods. A constructor that accepts...
Create your own date class. Create a MyDate class with these methods. A constructor that accepts a month, day, and year A method that determines if the year is a leap year A method that returns the date in this format, "1/9/2020" A method that returns the date in this format, "January 9, 2020" A method that returns the day A method that returns the name of the month A method that returns the number of days in the month...
Use object-oriented programming to write an application for a company that uses a subscription-based business model...
Use object-oriented programming to write an application for a company that uses a subscription-based business model to loan out clothes such as office and party wear to female clients. A subscription costs $69 per 4 weeks. A client can make unlimited loan requests during her subscription, as long as she has no previous loan that has not been returned. There can be only one piece of clothes in each loan request. The chosen piece of clothes takes a day to...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). Extend...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). Extend the ItemToPurchase class per the following specifications              Private fields string itemDescription - Initialized in default constructor to "none" Parameterized constructor to assign item name, item description, item price, and itemquantity (default values of 0).             Public instance member methods setDescription() mutator & getDescription() accessor (2 pts) printItemCost() - Outputs the item name followed by the quantity, price, and subtotal printItemDescription() - Outputs the...
TODO 1: Constructor. It assigns the songList member variable to a new instance of an ArrayList...
TODO 1: Constructor. It assigns the songList member variable to a new instance of an ArrayList that stores Song objects.. TODO 2: Implement the isEmpty method. Takes no parameters. Returns true if there are no songs on the list, false otherwise. TODO 3: Implement the addSong method. This method takes a Song object as a parameter and does not return a value. It adds the song to the songList. TODO 4: Implement the getSongListAsString method. This method takes no parameters....
Data structures in java Implement the following classes: 1. class Course that includes three instance variables:...
Data structures in java Implement the following classes: 1. class Course that includes three instance variables: private String Name; // the course name private int ID; // the course ID private Course next; // the link Your class should have the following:  A constructor that initializes the two instance variables id and name.  Set and get methods for each instance variable. 2. class Department that includes three instance variables: private String deptName; private Course head, tail; Your class...
Java: 1. Create a class called ArrayReview with one data field of an integer array. (2...
Java: 1. Create a class called ArrayReview with one data field of an integer array. (2 point) 2. Add a constructor to populate the array in the class using Java random number generator, and a setter/getter for the array, and toString(). The array length is passed to the constructor. (2 point) 3. The Big Blue Something Company only sells blue things. The quality control engineer has set up a machine to spot check the colors of the items coming off...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT