Question
Create a class called Date that includes 3 instance variablesa month (type int), a day (type int), and a year (type int). Provide a constructor
Create a class called Date that includes 3 instance variablesa 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 Dates capabilities.
The user is supposed to input the month day and year and then the constructor initializes the 3 instance variables. I am having a problem with the DateTest. I cannot figure out how to write the rest of the code to make it work and without it giving me an error when I create the Date object.
So far this is what I have:
public class Date { private int month; // Instance Variable private int day; // Instance Variable private int year; // Instance Variable // Constructor initializes variable public Date(int month, int day, int year){ this.month = month; this.day = day; this.year = year; }
// Method to set the month public void setMonth(int month){ this.month = month; // Store the month } // Method to rerieve the month public int getMonth(){ return month; } // Method to set the day public void setDay (int day){ this.day = day; // Store the day } // Method to retrieve the day public int getDay(){ return day; } // Method to set the year public void setYear(int year){ this.year = year; // Store the year } // Method to retrieve the year public int getYear(){ return year; } // Method to display the date public void displayDate(){ System.out.printf("%d/%d/%d", getMonth(), getDay(), getYear()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started