Lecture 13, OOP, Classes, Objects & References

Reading

JP chap 6, 9.1, 9.5, 9.6, 9.8-9.10, 10, 11.10.

Slide handouts 1.

Study Guide.

The goal of this lecture is to refresh your memory of objects, classes and references. But also to point out some tricky aspects of references that you might not have thought about. In general, there will be many topics in this course which was also talked about in introduction to programming. In this course we will look at them again, but look at them in greater detail.

I will assume you all have heard about what the following concepts:

In this lecture, we will take a closer look at how object,  references and methods work when the program is running. We will pay special attention to the structure of methods, and we will look at recursion.

The goal is that you after this lecture will be able to have a paper and pencil understanding of object, references and methods. The purpose is to give you a model of how an object oriented program is executed, and to get a better foundation for predicting what happens when you run your program. 

The textbook material for this lecture is the above mentioned. It is 12 pages. But those pages need to be read several times to be understood. The book is short on purpose, it cannot be read, but must be studied. There are no words that should be skipped. But after you have learned to use the book, it becomes a wonderful reference.

The programs from the lectures and for the exercises are on the course web.

Exercises

Exercise 2 is a hand in exercises, which must be accepted as part of the requirements for attending the final exam. If your Java is a bit rusty, however, exercise 3 is a good place to start.

Exercise 1. The questions in this exercise all deal with a class Dog that you have to program from scratch. 

  1. Create a class Dog. Dogs should have a name, and a sex.
  2. Equip the Dogs with a toString() method, so they are easy to print.
  3. Make a class DogTest with a main method in which you create the following Dogs:
  4. Change the Dog class so that each dog has a mother and a father.
  5. Add to the main method in DogTest, so that:
  6. Draw a object-diagram as in JP chapter 10, or as on the lecture slides of the object that exist at the end of the main method from the previous question. Again, a hand drawing is the easiest.
  7. Change the dog class to include a method fathersName that return the name of the father. If the father reference is null, return "Unknown". Test in the DogTest main method that it works.
    referenceToCoco.fathersName() returns the string "Buster"
    referenceToSparky.fathersName() returns the string "Unknown"
  8. Change the dog class to include a method boolean hasSameMotherAs(Dog otherDog). The method should return true on the call
    referenceToCoco.hasSameMotherAs(referenceToRocky). Show that the new method works in the DogTest main method.

Exercise 2 (Hand In). The equals method.

Write a class ITUStudent, which has fields fullName, CPR number (Social security number), BachelorDegree, and ITUGradeAverage. 
  1. For the student administration system, we need to be able to find out if two student objects are equal. Override the equals method from the Object class and write a small main method that test that the equals method works. Two persons are the same if the CPR number is the same.
  2. Every semester a number of students apply to be teaching assistants, we consider them equal if they have the same Bachelor degree and grade average. Write an equals method that must be able to find out if two students are equal if their  grade average are the same. Notice, one cannot have two equals methods in the same class, so comment out the one from a) when trying this on the computer..

Exercise 3, Recursion and investigating the "this" reference.

  1. In the constructor for the WaterSource class, insert a System.out.println at the end, which prints out the name and litres per hour of the WaterSource instance.
  2. Change the "this.name = name" to "name = this.name". What will be printed now? 
  3. Read section 6.3 and 11.10 in Java Precisely and give your own explanation of why this.name = name is correct, while name = this.name is not.
  4. Rewrite the static method totalLitersPerHours in the WaterWay class, so it is no a longer static method, but does recursion in the same manner as the totalLength method.
  5. Why is it necessary to have a this reference for non-static method calls? To understand, pay close attention to the Ball::move method call on slide 10.
  6. Why is it not necessary for static methods? 
  7. The compiler will give an error message if we try to use the this reference in a static method. What error message does it give? Is it the same error message for explicit and implicit this references? To find out, write a small example program that contains both types of errors.
  8. Consider the class Ball from the lecture slides. In the end of the main method on slide 11, insert the following two statements:
        b1 = b2;
        b1.move(4,3);

    Draw a diagram as on slide 11, showing the situation just after "x+=dx" in the call of b1.move(4,3). I suggest you do a hand drawing to save time.