Chapter 6 contains a number of important concepts about classes and objects which can be confusing such as method overloading, static variables and methods, methods for comparing objects, deep copying, etc. In this lab, you will have the opportunity to practice some of these concepts.
You will also have more practice building Graphical User Interfaces (GUIs) in Netbeans.
Begin with the Programming Challenge, Problem 5: Month Class, on page 443. The purpose of this problem is to give you experience overloading constructors and writing methods for comparing objects (equals, greaterThan, lessThan).
In addition to the methods listed, also add the following:
numberOfMonths
, which keeps track of how many Month objects have been created.
You will need to modify your constructors to update this variable each time a Month
object is created.
numberOfMonths
. These methods should also be static.
Create a class containing a main method and test your Month class. Create at least 3 Month objects each one using a different constructor. Apply each of your methods, in particular, the equals(), lessThan(), greaterThan(), and getNumberOfMonths(). For example, your main method might look like (note the syntax, especially for the static method!) :
// Create 2 months
Month m1 = new Month(); // use the no arg constructor
Month m2 = new Month(4);
// Print out static variable (note, the class name is used and not the object name)
System.out.println("There are now " + Month.getNumberOfMonths() + " Month objects.");
// Create 2 more months
Month m3 = new Month(m2); // use the copy constructor
Month m4 = new Month("September");
System.out.println("There are now " + Month.getNumberOfMonths() + " Month objects.");
System.out.println("The month objects are " + m1 + ", " + m2 + ", " + m3 + ", " + m4);
// Test comparison methods:
System.out.println( m1 +" is less than " + m2 + ": " + m1.lessThan(m2));
System.out.println( m3 +" is less than " + m1 + ": " + m3.lessThan(m1));
System.out.println( m1 +" is greater than " + m2 + ": " + m1.greaterThan(m2));
System.out.println( m3 +" is greater than " + m1 + ": " + m3.greaterThan(m1));
System.out.println( m1 +" is equal to " + m4 + ": " + m1.equals(m4));
System.out.println( m2 +" is equal to " + m3 + ": " + m2.equals(m3));
Creating a GUI: The purpose of this part of the lab is to introduce you to several more GUI components: TextAreas, ScrollPanes, and ComboBoxes.
If you want to do something a little different than what is below, please do. The main requirement is that you have a GUI which makes use of a separate class which you wrote (below, we use the Month class) and that the GUI includes a ComboBox and a ScollPane/TextArea. If you do decide to do something different, please run it by the instructor first to make sure it is feasible.
Your goal is to create a GUI that looks something like this:
Begin by adding a JFrame form to your existing Netbeans project which already contains your Month class. In order for Netbeans to use the correct main method when the program is run, right click on the project name (in the projects window), select properties, click on the Run category, and click the Browse button to the right of the Main Class textfield. Select your JFrame class. If you did this correctly, the JFrame window should appear when you build and run your program.
Next, lay out your components as shown above. Don't forget to rename the components (in the Navigator window) so your code is easier to read.
In the Design tab, select the ComboBox. In the properties, click the "..." next to the model property. Replace the item 1, item 2, ... with the month names January, February, ... When you compile and run the code, you should see the months listed in the ComboBox drop down menu, as shown here:
Note, to add text to a TextArea without clearing the existing text, you call append("my_text"). If you want to clear the existing text in a TextArea, you call setText("my_text").
Add an actionPerformed to the ComboBox. Modify the actionPerformed method in the code so that each time a month name is selected in the ComboBox, you do the following:
Add an actionPerformed to the clear button that 1) calls the setNumberOfMonths() method in the Month class and 2) clears the TextArea using the TextArea's setText() method. After you hit the clear button, the TextArea should look like
To receive full credit, you need to:
Make sure that your Java code:
Demonstrate your Month program (Part 1), for example, by running the main method above, to the instructor or lab assistant during the lab on the due date. . Also demo your gui (Part 2).
Submit the following to Wise as attachments to Lab 7