Question

In: Computer Science

/////////////////JAVA PLEASE///////////////////////////////// Create a class called GVdate to keep track of a calendar date including month,...

/////////////////JAVA PLEASE/////////////////////////////////

Create a class called GVdate to keep track of a calendar date including month, day and year.  You can do simple things like checking if it is your birthday, advancing to the next day, checking if a given date is valid and checking if it is a leap year.

Class Fields/Instance Variables

  • Provide appropriate names and data types for each of the private instance variables:
    • the month, day and year (int)
    • two final integers that represent YOUR birthday (month and day)

Constructors

  • public GVdate( )– this is the default constructor; it assigns the following initial values to the instance variables:  
    • month: 10
    • day: 12
    • year: 2020
  • public GVdate (int m, int d, int y) – set the instance variables to the provided input parameters. For now, assume that the input parameters are correct.

Accessor Methods

  • public int getMonth( ) – return the month.  
  • public int getDay( ) – return the day.  
  • public int getYear( ) – return the year
  • public String toString() – return a formatted date string (for example "10/12/2020").  
  • public boolean isMyBirthday () – return true if the date is YOUR birthday. Otherwise return false.

Mutator Methods

  • public void setMonth(int m)- set the month to the input parameter. For now, assume the value for the month will not create an invalid date.
  • public void setDay(int d)- set the day to the input parameter. For now, assume the value for the day will not create an invalid date.
  • public void setYear(int y)- set the year to the input parameter. For now, assume the value for the year will not create an invalid date.
  • public void setDate (int m, int d, int y) – set the instance variables to the values of the input parameters. For now, assume the values of the input parameters will not create an invalid date.

Solutions

Expert Solution

Please find the below solution for this problem:-

package qvdate;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class GVdate {
   private int calDate;
   private int calMonth;
   private int calYear;
   private final int birthday=26;
   private final int birthMonth=12;
  
   public GVdate() {
       this.calDate=12;
       this.calMonth=10;
       this.calYear=2020;  
   }
   public GVdate (int m, int d, int y) {
       this.calDate=d;
       this.calMonth=m;
       this.calYear=y;
   }
   public String toString() {
       return calMonth+"/"+calDate+"/"+calYear;
   }
  
   public boolean isMyBirthday() {
       if(calMonth==birthMonth &&calDate==birthday) {
           return true;
       }
       return false;
   }
   public void setDate (int m, int d, int y) {
       this.calDate=d;
       this.calMonth=m;
       this.calYear=y;
      
   }
  
   public boolean isDateValid() {
       String dateStr=toString();
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
try {
sdf.parse(dateStr);
} catch (ParseException e) {
return false;
}
return true;
}
   public boolean isLeapYear() {
boolean leapYear = false;

if(calYear % 4 == 0)
{
if( calYear % 100 == 0)
{
if ( calYear % 400 == 0)
   leapYear = true;
else
   leapYear = false;
}
else
   leapYear = true;
}
else
   leapYear = false;
  
return leapYear;
   }
   public Date advancingtonextday() {
       Calendar c = Calendar.getInstance();
       DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
Date currentDate = null;
try {
   currentDate=sdf.parse(toString());
} catch (ParseException e) {

}
       c.setTime(currentDate);
       c.add(Calendar.DATE, 1);
       Date newDate = c.getTime();
       return newDate;
   }
  
   public int getCalDate() {
       return calDate;
   }
   public void setCalDate(int calDate) {
       this.calDate = calDate;
   }
   public int getCalMonth() {
       return calMonth;
   }
   public void setCalMonth(int calMonth) {
       this.calMonth = calMonth;
   }
   public int getCalYear() {
       return calYear;
   }
   public void setCalYear(int calYear) {
       this.calYear = calYear;
   }
  
  
  
  
}


Related Solutions

Write a class called Animal that contains a static variable called count to keep track of...
Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate,...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also...
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY b. Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first...
(Using Date Class) The following UML Class Diagram describes the java Date class: java.util.Date +Date() +Date(elapseTime:...
(Using Date Class) The following UML Class Diagram describes the java Date class: java.util.Date +Date() +Date(elapseTime: long) +toString(): String +getTime(): long +setTime(elapseTime: long): void Constructs a Date object for the current time. Constructs a Date object for a given time in milliseconds elapsed since January 1, 1970, GMT. Returns a string representing the date and time. Returns the number of milliseconds since January 1, 1970, GMT. Sets a new elapse time in the object. The + sign indicates public modifer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT