Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My question is Create a class called Date that includes 3 instance variables—a m

ID: 3852176 • Letter: M

Question

My question is Create a class called Date that includes 3 instance variables—a month (type int), a day (type int), and a year (type int). Provide a constructor that initializes the 3 instance variables and assumes the values provided are correct. Provide a set and a get method for each instance variable. Provide a method displayDate that displays the month, day, and year separated by forward slashes(/). Write a test application named DateTest that demonstrates class Date’s capabilities.

My code is

public class Date {

       int month;

       int day;

       int year;

       public Date(int month, int day, int year) {

              this.month = month;

              this.day = day;

              this.year = year;

       }

       public int getMonth() {

              return month;

       }

       public void setMonth(int month) {

              this.month = month;

       }

       public int getDay() {

              return day;

       }

       public void setDay(int day) {

              this.day = day;

       }

       public int getYear() {

              return year;

       }

       public void setYear(int year) {

              this.year = year;

       }

       public void displayDate() {

              System.out.println(month + "/" + day + "/" + year);

       }

}

public class DateTest {

       public static void main(String[] args) {

              Date day = new Date(4,16,2012);

              day.displayDate();

              System.out.println("Month: " + day.getMonth());

              System.out.println("Day: " + day.getDay());

              System.out.println("Year: " + day.getYear());

              day.setMonth(12);

              day.setDay(25);

              day.setYear(2011);

              day.displayDate();

       }

}

This was given when someone previously answered, however this code does not work. Please help

Explanation / Answer

You have two classes in the same file. You can only keep one class as public and it should have the same name as the name of the file.

Use the below code: The name of the file should be DateTest.java

class Date {
   int month;
   int day;
   int year;
   public Date(int month, int day, int year) {
       this.month = month;
       this.day = day;
       this.year = year;
   }
   public int getMonth() {
       return month;
   }
   public void setMonth(int month) {
       this.month = month;
   }
   public int getDay() {
       return day;
   }
   public void setDay(int day) {
       this.day = day;
   }
   public int getYear() {
       return year;
   }
   public void setYear(int year) {
       this.year = year;
   }
   public void displayDate() {
       System.out.println(month + "/" + day + "/" + year);
   }
}
public class DateTest {
   public static void main(String[] args) {
       Date day = new Date(4,16,2012);
       day.displayDate();
       System.out.println("Month: " + day.getMonth());
       System.out.println("Day: " + day.getDay());
       System.out.println("Year: " + day.getYear());
       day.setMonth(12);
       day.setDay(25);
       day.setYear(2011);
       day.displayDate();
   }
}

OUTPUT:

4/16/2012
Month: 4
Day: 16
Year: 2012
12/25/2011

If you face any issue let me know in comments, I will he happy to help you further.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote