CS 1063
Week 4: An Introduction to Objects and Classes
Modifying a Class
Objectives
- Construct and use Objects
- State the public interface of a class given the class
- Use the Dice class
- Use and modify the Greeter class
Assignments
- Reading Week 4
- Lab 2 due
(Monday)
- Begin Lab 3
- Take Quiz 3 (Thursday, Friday and Saturday)
The Dice Class
Given the following Dice class, state the public interface.
public class Dice {
// Private data
private int mySides;
private int myLastRoll;
|
// construct and initialize the data
public Dice (int sides) {
mySides = sides;
myLastRoll = 1;
}
|
// accessor methods - These methods will not change the values of the private data
// return the number of sides of the Dice
public int getSides() {
return mySides;
}
// return the value of the last roll
public int getLastRoll() {
return myLastRoll;
}
public String toString() {
return "sides: " + mySides + " Last Roll: " + myLastRoll;
}
// modifier method - This method will change the value(s) of the private data
// Rolls the Dice and returns this value
public int roll() {
myLastRoll = (int)(Math.random() * mySides) + 1;
return myLastRoll;
}
|
}
|
Activity 1 Write the public interface for the
Dice class
Activity 2 Testing the Dice Class
Setup:
Part 1. Construct and initialize (instantiate) two
Dice objects named d1 and d2 with 6
sides.
Part 2. Print d1 and d2 using the
toString method.
Part 3. Roll d1 and d2 placing each value
in int variables roll1 and roll2.
Print both values with appropriate labels.
int roll1 = d1.roll();
...
Part 4. Find the sum of the two rolls and place this value in
an int variable sum. Print the sum
with an appropriate label.
Part 5. Roll each of the two Dice 3 more times and
print the final value of each Dice using the
getLastRoll method.
Part 6. Test the getsides method for each Dice
object using the System.out.println method
Modifying the Greeter Class
public class Greeter {
// Default Constructor
public Greeter() {
}
|
// Public accessor methods
public String sayHello() {
String message = "Hello World!";
return message;
}
public String sayGoodbye() {
String message = "Goodby World!";
return message;
}
|
}
|
Activity 3 List the public interface for the Greeter class
Activity 4 Testing the Greeter Class
Setup:
Part 1. Construct and initialize (instantiate) a
Greeter object named g1.
Part 2. Ask g1 to sayHello and store the
message in a String variable named message.
Part 3. Print message.
Part 4. Ask g1 to sayGoodbye and store the
message in the variable message. (What will happen to
the first hello message?)
Part 5. Print message.
Activity 5: Modifying the Greeter Class
The Greeter class can
only say Hello World or Goodbye World. Suppose we would like our
class to be able to say Hello Cathy or Hello Jeff. In order to do
this the Greeter class
must store a name to place after Hello. This means that we would
have to add a private data field and modify the constructor so that it
can initialize the data field. The name should come from the
client that is using our Greeter
class. Below are some very important rules that we will follow
when adding data fields and methods.
- Instance fields are generally declared private
- An instance field (data) declaration consists of the following
parts:
- An access specifier (private)
- The type of the variable such as String
- The name of the variable such as myName
- Constructors contain instructions to initialize objects. The
constructor name is always the same as the class name.
- The new operator invokes the constructor.
- A method definition contains the following parts:
- An access specifier such as public
- The return type of the method (such as String or
void)
- The name of the method (such as sayHello)
- A list of the parameters of the method, enclosed in
parentheses(the sayHello method has no parameters)
- The body of the method: a sequence of statements enclosed in
braces
Part 1: Modify the Greeter class to say
Hello <some name>
Add the following instance field to our Greeter class:
private String myName;
The constructor method for the Greeter Class will be:
public Greeter(String name) {
myName = name;
}
How will the sayHello and sayGoodbye methods
need to be
modified? Modify the assignment statement:
String message = ...
for the sayHello and the sayGoodbye methods.
Part 2: Add a method refuseHelp
to the Greeter class.
It should return a
string such as "I am sorry, Dave. I am afraid I can't do that."
Activity 6: Vocabulary
Explain the difference between
a. an object and an object reference
b. an object and a class
c. a constructor
and a method
d. an instance field and a local variable
e. a local variable
and a parameter variable