Lab 3: Classes and Objects
CS 141: Introduction to Programming, Fall 2015

Due Date: Friday, Sept 18 , by the end of the lab


This lab walks you through the process of writing your own class. You need to follow 3 important steps:

  1. Design
  2. Implementation
  3. Testing

Part 1: Program Design and the UML Diagram

Chapter 3 of your text provides many examples of classes one can write for specific problem domains (e.g. BankAccount, Rectangle, Car etc). For each domain, the text identifies the relevant data (fields/attributes/member variables) and actions (methods, procedures) on this data. Be sure you understand these examples before proceeding.

Select a nontrivial problem domain which interests you (e.g. loans, countries, politicians), and which does not appear in the text. Examples include:

On a piece of paper or in a text editor (not Netbeans!) create a class design (i.e. create the UML diagram) for your chosen domain. Follow the example UML diagrams shown in Figures 3-23, 3-24, and 3-25. 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.

This is not a simple task - there is a whole lot you need to understand and to think about so, please ask questions!

Before continuing to the next step, bring your UML design to lab and have the instructor or lab assistant check 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.

  • Try to have this part completed by the end of lab on Sept 9.

    Part 2: Implementing the Class

    In this section of the lab, you will implement the class you designed above:

    1. In Netbeans, create a new project and give it a name that tells you something about the project domain, e.g. LoanProgram or CountryProgram. When you create the project, be sure to check the box "Create Main Class" so that Netbeans will create the class containing the main method.
    2. In the project window on the left, right-click on your project's package name, and select New → Java Class. In the "Class Name" textbox, enter the name of the class you designed above (e.g. Loan, Country, etc) and then click the "Finish" button. Netbeans will create a file for your class and open it in the Netbeans editor. Using your design as a guide, implement your class by adding the fields and methods.
    3. As you add code, Netbeans will indicate syntax errors with red underlines. The red lines will appear as you begin to type but should disappear once you have completed each statement. If not, hold your mouse over the error to see what hints Netbeans gives about the nature of the error.
    4. Compile your program to make sure there are no errors . If there is a compilation error, look at the error messages in the Output window (the window at the bottom) to try to understand the problem. If you click on the error in the Output window, Netbeans will often take you to the location in the code where the problem exists. Always start with the first error which is printed, fix it, and recompile before moving to the next error.

    Part 3: Instantiating and Testing your Class

    Once you have your class written, it is time to test your class code by instantiating the class, i.e. creating an object and testing the methods:

    1. In the main method, create several objects (instantiations) of your class. For example, if you had written a Car class with a constructor that takes arguments for the make, year, and mpg, then you need to add something like this statement to the main method (or better yet, create a Scanner object and prompt the user for the information):
      int mpg = 25;  // don't need this variable but makes code more readable
      Car myNewCar = new Car("Ford", 2014, mpg); 
      Car myOldCar = new Car("GM", 1999, 11.0); 
    2. Once you create the objects, you can "send messages" (i.e. call methods) to the objects, e.g.
      System.out.println("My new car is " + myNewCar); // calls the toString() method
      double gallons = 10.;
      double miles_new = gallons*myNewCar.getMPG(); // calculate the distance the new car can go.
      double miles_old = gallons*myOldCar.getMPG(); // calculate the distance the old car can go.
      System.out.println("My " + myNewCar.getName() + " can go " + miles_new + " miles on " + 
                          gallons  + " gallons of gas.");
      System.out.println("My old car can go " + miles_old + " miles on " + gallons + " gallons of gas.");

      Be thorough - write code to test every method in the class.

    Try to have this completed by the end of lab on Sept 16. Before continuing, it is recommended that you demonstrate the code to the instructor to make sure it is implemented and tested correctly.

    Part 4: Adding another Class

    In the above loan example, I might want to include the date the loan was purchased. A date consists of 3 items: month, day, and year. Rather than adding 3 more fields to my Loan class, it would be better to create a second class which stores a date (e.g. called Date). Or, in my PoliticalCandidate class, I could use the Date class to represent the candidate's birthdate. Once the Date class is written, I would need to modify your original classes to make use of the Date class.


    To receive full credit on this lab, you need to:

    1. Make sure that your Java code:

      • Is well commented, including at the top of each file, your name, lab name, program name, date, brief description of what the class does.
      • Is well formatted, i.e. proper indentation and line returns.
      • Delete any unused or commented out code.
    2. Design: When you demonstrate your code, you should have your design available to compare to your code.

    3. Demonstrate your program to the instructor or lab assistant during the lab on the due date. You will be asked to display your commented/formatted code and to run the code. If your code is not properly formatted, does not conform to your design, or does not give the appropriate output, you will be given the opportunity to correct the code but only if there is still time to make corrections before the due date/time. This is why it is to your benefit to complete and demonstrate your code early. You also may be asked questions about what the code is doing, how it works, and how it might be changed slightly to alter the output.

    4. Submit your code as attachments to Lab 3 in Wise. Specifically, you need to submit the following 3 Java classes (i.e. the files with the .java extension and NOT the files with the .class extension!):

      • your Java class containing the main method and the code to test your class
      • your Java class for your problem domain.
      • your simple class which is used within the class for your problem domain.