Overview

Up to this point, we have worked with several different classes (e.g. Turtle, Picture, String, Random). We have modified some of these (e.g. the Picture class) to add new capabilities/methods but we have not created any from scratch.

In this reading we will learn the basics of writing our own classes, including the three major components of a class: methods, fields, and constructors. Luckily there isn’t a lot of new syntax to learn: we’ll just need to apply what we already know (e.g. variable declaration) to a slightly different context.

This reading also contains a introduction to using a debugger to help you find errors in your program. Taking a few minutes to become comfortable with DrJava’s debugger will save you many headaches and possibly hours of frustration down the road.

Required Reading

Sections 11.1 - 11.2 (pp. 343-356), excluding Sections 11.2.2 and 11.2.3, from the course textbook.

Learning Objectives

BASIC Learning Objectives

Each student will be responsible for learning and demonstrating proficiency in the following objectives PRIOR to the class meeting. The reading quiz will test these objectives.

  1. Use the nouns analogy to determine whether something should be a class or a field.
  2. Write a class that contains one or more fields.
  3. Describe why the visibility of most fields is “private”.
  4. Compare and contrast constructors and methods.
  5. Given a class definition and the creation of an object using the “new” operator, determine the value of each field after the object has been created.
  6. Use DrJava to create a breakpoint, examine the contents of variables, and execute one instruction at a time.

ADVANCED Learning Objectives

The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.

  1. Given a description of an object, determine the fields and methods needed for a class that will be used to create that type of object.
  2. Identify common errors made by novices in defining their own classes.
  3. Write a class that contains fields, methods, and constructors.

Pre-class Exercises

These exercises are geared towards mastering the BASIC learning objectives listed above. You are expected to submit them before class and it is highly recommended that you complete them before attempting the reading quiz.

  1. Write a class named Dog. This class should have the following fields:

    This class should also have a method named getHumanAge which returns the dog’s age in human years. To calculate this value, divide the dog years age by 7.

  2. Consider the following class.

    public class Car {
        private String make;
        private String model = "Roadster";
        private int year;
    
        public Car() 
        {
            this.make = "Tesla";
            this.model = "Model S";
        }
    
        public Car(String carModel, int modelYear)
        {
            this.model = carModel;
            this.year = modelYear;
        }
    }

    Part A: Assume that we create a car object as follows.

    Car myCar = new Car();

    What are the values for each of the fields in this car object? For each field, explain why it has that value.

    Part B: Repeat Part A, but assuming the following way to create a car.

    Car anotherCar = new Car("IS300", 1999);
  3. In DrJava perform the following steps: