COMP2011 Tutorial Exercises 1, Week 2
0. Preliminaries
-
Begin reading the textbook and some Java tutorials.
-
Be sure you can use
javac
to compile
Java source code (.java
files)
and then use java
to run the Java bytecode
(.class
files)
-
Prepare answers to the tutorial questions below.
For any questions involving code, it is best to compile and run your
solution before the tutorial. To run solutions, you may need to write
some test code, which is always good practice.
-
Come to the tutorial prepared to
ask questions about issues that arose for you in attempting the exercises.
Even if you can do all the exercises you should still think about
broader issues: come prepared to raise these with your tutor.
-
Solutions will be published at the end of the week, so do not use
the tutorial time to extract a solution from the tutor. There
probably will not be enough time to cover all solutions in detail
anyway. Instead
try to use the time to develop your understanding,
and open a dialogue with the tutor.
Don't just be a passive listener.
Participate!
-
If you are still unsure about how to compile and run a Java program,
be sure to work through the first step of
Sun's
Java Tutorial.
1. Java Programming
The questions in this section should be doable with little knowledge of
object orientation.
-
R-1.13
Write a short Java function that takes an integer n
and returns the sum of all the odd integers smaller than n.
-
Write a short Java function
isTwoPower()
that takes an int
i and returns
true
if and only if i is a power of 2.
Your function cannot use multiplication or division, however.
2. Object Oriented Design
The questions here need an elementary knowledge of object orientation.
Not all of them may be answerable now, so attempt as many as time permits.
We will re-visit some in the next tutorial. (The section and page numbers
refer to the 3rd edition; 4th edition references are nearly the same.)
-
R-2.4
Draw a class inheritance diagram for the following set of classes:
- Class
Goat
extends Object
and adds an instance variable tail
and methods milk()
and jump()
.
- Class
Pig
extends Object
and adds an instance variable nose
and methods eat()
and wallow()
.
- Class
Horse
extends Object
and adds instance variables height
and color
and methods run()
and jump()
.
- Class
Racer
extends Horse
and adds method race()
.
- Class
Equestrian
extends Horse
and adds an instance variable weight
and methods trot()
and isTrained()
.
(Note: see Goodrich, page 74 for an example of an inheritance diagram.)
-
R-2.5
Give a short fragment of Java code that uses the Progression classes
from lectures (Section 2.2.3 of Goodrich - also in
java/tut01)
to find the 8th value of a
Fibonacci progression starting with 2 and 2 as its first two values.
-
R-2.7
Suppose we have an instance variable
p
that is declared
of type Progression
(Section 2.2.3 of Goodrich - also in
java/tut01).
Suppose further that p
actually refers to
an instance of the class GeomProgression
that was created with the default constructor. If we cast
p
to type Progression
and call
p.firstValue()
, what will be returned? Why?
-
R-2.8
Consider the inheritance of classes from R-2.4 above,
and let d be an object variable of type
Horse
.
If d refers to an actual object of type Equestrian
,
can it be cast to the class Racer
? (Explain why or why not.)