C211 Problem Solving and Programming II

C211 Lab 1

Due Date: Wednesday, October 30, 2024.

Ex. 1. a. Declaring a Class.

Open either Eclipse or NetBeans.

Installing an IDE. If you don't have either Eclipse or Netbeans installed on your computer, here are the links to download them:

Eclipse: https://www.eclipse.org/downloads/

NetBeans: https://netbeans.apache.org/download/

Beside these IDEs, you may also have to update Java the latest version, and also install the Java SE Development kit:
https://www.oracle.com/java/technologies/javase-downloads.html

Create Project Create a project called Lab1. Inside this project, create a class called Date. For this, Follow the instructions below for the specific IDE that you chose.

Eclipse

If this is the first time you open this program, it will ask you to choose a directory for your workspace. The path proposed by default should be fine. Click on Launch.

New Project. Click on Create a new Java project. Name the project lab1, then click Finish. You will be asked next if you want to create a file for module info. You can click on Don't Create. This is not necessary, but it shouldn't hurt anything if you do create it.

New Class. With the project selected, click on the New button in the toolbar at the top, first from the left. From the pop-up menu, select Class. If the Package line is empty, write in lab1. Under Name write Date (no spaces). Below that, there's a question about method stubs to create. Check the first one to add the public static void main. Now you can click on Finish. Proceed to point b.

NetBeans

If this is the first time you open this program, go to Tools - Options and click on each of the tabs. Some of the tools need to be activated this way. Under the tab Java, click on each of the tabs under it (Ant, Gui, etc) and for each of them where it asks if you want to install the tool, click Next or Yes or Install to do it. It's better to have these tools even if we're not using them this semester.

New Project. Click on New Project at the top left of the window (second button from the left). Choose Java with Maven in the options, then Java Application. Name the project lab1 (with the prefix they give you), and give the package name lab1, then click Finish.

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.

Lab Submission

Upload the file Date.java in Canvas, Assignments, Lab 1 1.