This is a 2 week lab.
Part 1 should be
completed by Sept 13.
Part 2 should be
completed by Sept 18.
Part 3 is due Friday, Sept 22.
We will be using Pair Programming. You will be assigned a partner to work with. If you don't recall the process, please re-read the instructions from Lab 2
Part 1 will not be collected but should be completed by no later than Sept 13. Note, you need to understand this material in order to do parts 2 and 3.
Chapter 2 which gives examples using the String and Rectangle class. So, please read these sections in Chapter 2 before continuing.
Look up the String class in the Java API. Scroll on down to look at the section called Method Summary. You should see a list of the names and usage of the String class methods. These methods are part of the public interface for the String class. You do not have to understand how the String class is implemented in order to create and use String objects. Peruse through the methods and try the following:
Create a practice Netbeans java Application project, checking the "Create Main Class" option box. In main, practice using the String class. For example,
Strings are unusual in that you can, but do not need to, use the "new" operator in order to create a String object. Your text gives examples of the Rectangle class, where you must use the "new" operator to create a Rectangle object. Look up the Rectangle class in the java API. There are several different constructors.
Part 2 will not be collected but should be completed by no later than Sept 18. Note, part 3 builds on what is done in part 2.
Read Chapter 3 before continuing. Chapter 3 provides examples of classes (e.g. BankAccount). Before starting to code in Netbeans, you should think about the design, that is, you should identify all the data (instance or member variables) and all of the actions (methods, constructors) on this data. For this simple case, it may seem a bit overkill but be assured that as programs get more complex, designing ahead will save you time and prevent many headaches. It is also the only way to effectively work with multiple people on the same program.
In this part, you will create a simple Date class that contains 3 integer member variables the month (1 to 12), day, and the year.
On a piece of paper or in a text editor (not Netbeans!) design your class by creating a Unified Modeling Language (UML) diagram which lists the member variables and method headers but no implementation. For example, the UML diagram for the Rectangle class might look like (for brevity, not all the methods are listed)
The left side shows the class diagram and the right shows the object diagram. You are to draw the class diagram for the Date class. Be sure to indicate which fields are private(- sign)/public(+ sign). Include constructors, accessors (getters) & mutators (setters) where appropriate, and a toString() method (discussed in lecture). Indicate the field types, and the method parameters and return types.
Remember that, by convention, all class names start with an uppercase letter, while all variables, objects (including member variables), and methods (except constructors) start with a lowercase letter.
Before continuing to the next step, have the instructor or lab assistant check your UML diagram to make sure it is complete and correct. If there are things that need to be fixed, it is important that you fix them before starting the implementation.
You will now implement your Date class:
Unit Testing: Once you have your Date class written, it is time to test your class code by instantiating the class, i.e. creating an object and testing the methods. In the main method, create several Date objects (instantiations) using the "new" operator. For example, add something like this below to the main method (or better yet, create a Scanner object and prompt the user for the information):
int month = 9;
int year = 2017;
int day = 11;
Date today = new Date(month, day, year);
Date yesterday = new Date(9,10,2017);
Date tomorrow = new Date(9,day+1,2017);
Once you create the objects, you can "send messages" (i.e. call methods) to the objects, e.g.
System.out.println("today is " + today); // calls the toString() method
System.out.println("The year is " + today.getYear()); // calls the getYear() method
tomorrow.setYear(2018); // changes the year by calling the setYear() method
etc...
Be thorough - write code to test every method in the class.
Before continuing, it is recommended that you demonstrate the code to the instructor to make sure it is implemented and tested correctly.
In the above example, all of the member variables of the Date class were primitive types, i.e, integers. Member variables, however, can also be other classes. In this part of the lab, you are to write a second class called Person which contains your Date class as one of its member variables. (You can choose to do something else that is similar if you want).
The Person class should contain the person's first name , last name,
birthday (i.e. a Date object), and such things as their profession, favorite food, and music (add some other items of your own choosing). Besides
the normal constructors, setters/getters, and toString method, you should add a method called age
, which takes a Date object as a parameter, and
returns the age of the person on that particular date (for simplicity, just go by the year).
Add some other methods of your choosing.
Include several different constructors. One constructor can have parameters for the month, day, and year, and then create a Date object in the constructor itself. Another can have a Date object as a parameter which is used to set the Date member variable. Drawing an Object Diagram can help you see what is going on.
Design: Before starting to code, create a UML diagram containing all of the member variables and methods. In this case, there are 2 classes you must include in your diagram, Person and Date. Note how the relationship between Person and Date is represented in the UML diagram:
Before continuing, please have the instructor check your UML diagram.
Implementation: Add a new java file to your project called Person.java. Use your UML diagram to guide the implementation of your Person class. You should be able to use the same Date class that you implemented before. Compile and run your program to test for errors. At the moment, you are not using the Person class for anything so the output should be from whatever you had in main from before. If you have not changed your Date class, the code in main should still work.
Unit Testing: Delete the code you have in main and add code to test your Person class. Be thorough.
Make sure that your Java code:
Demonstrate your program from Part 3 to the instructor by the end of lab on the due date.
Submit your Part 3 code to Wise. There should be 3 java files to submit as attachments: the java file containing main, along with Date.java and Person.java