Question
I need to see the java code for the following so I can compare it the one I created New Class. In the project you
I need to see the java code for the following so I can compare it the one I created
New Class. In the project you created, click on the + next to Source Packages and select the package named lab1. Then click on the New File button in the toolbar at the top, first from the left. From the list of options, select Java Main Class. If the Package line is empty, write in lab1. Click Next, then give it the name Date, then click Finish to create it. The class should contain the main function. Proceed to point b.
b. Class Attributes
The class stub should already be there. Add three instance attributes (or variables) for the day, the month, and the year.
At the top of the file, inside the package but before the class, add a statement to import the module java.util.Scanner. Then add a class attribute (static) called scan of type Scanner and initialize it with new Scanner(System.in). This will be useful for input.
c. Class Methods
Add a public method called input with no parameters and of type void. In this function, ask the user for the values of the date, then input them in the three attributes. Use scan.nextInt() to input the three values. If the value of the month is larger than 12, make it to 12. If it's less than 1, make it 1. The same way, restrict the date to the range [1, 31].
Add a public method called output with no parameters and of type void. In this function, output the date in the format "mm/dd/yyyy". You don't have to verify if the numbers have the right number of digits.
d. Constructor
Add a constructor for the class with parameters for the day, month, and year. You can use other names for the parameters for convenience. Then in the constructor, initialize each of the attributes with the value of the corresponding parameter. Add a second default constructor with no parameters and assign the value 0 to all 3 parameters.
e. Comparison
Write a method called before comparing one date with another and telling us if the first date is earlier. The first date will be the target object (or calling object, identified by this), and the second one will be a parameter to this function. A date is before a second one if the year part has a smaller value. If the years are equal, then we compare the months. If the months are also equal, we compare the days. The method should return true or false.
f. Test
In the main, declare a variable of type Date and assign to it a new date using the constructor with the values for today. Output this variable. Declare a second variable and assign it an object using the default contructor, then call the function input to enter a new date in this object, and then output it. Finally, compare the two objects with the method before and output a message based on the result. This is the end of the lab.
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