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:
- A class, with fields, constructors, and methods.
- A method which has a return type, parameters, and a body.
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.
- Create a class Dog. Dogs should have a name, and a sex.
- Equip the Dogs with a toString() method, so they are easy to print.
- Make a class DogTest with a main method in which you create the following
Dogs:
- Max, male
- Rocky, male
- Sparky, male
- Buster, male
- Sam, male
- Lady, female
- Molly, female
- Coco, female
- Change the Dog class so that each dog has a mother and a father.
- Add to the main method in DogTest, so that:
- Max has Lady as mother, and Rocky as father
- Coco has Molly as mother, and Buster as father
- Rocky has Molly as mother, and Sam as father
- Buster has Lady as mother, and Sparky as father
- 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.
- 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"
- 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.
- 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.
- 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.
- 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.
- Change the "this.name = name" to "name = this.name".
What will be printed now?
- 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.
- 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.
- 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.
- Why is it not necessary for static methods?
- 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.
- 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.