102 Chapter 7: Object-Oriented Design
Chapter 7: Object-Oriented Design Lab Exercises
Topics Lab Exercises
Parameter Passing Changing People
Interfaces Using the Comparable Interface
Method Decomposition A Modified MiniQuiz Class
Overloading A Flexible Account Class
A Biased Coin
Chapter 7: Object-Oriented Design 103
Changing People
The file ChangingPeople.java contains a program that illustrates parameter passing. The program uses Person objects
defined in the file Person.java. Do the following:
2. Compile and run the program to see if your trace was correct.
3. Modify the changePeople method so that it does what the documentation says it does, that is, the two Person objects
passed in as actual parameters are actually changed.
// ******************************************************************
// ChangingPeople.java
//
// Demonstrates parameter passing — contains a method that should
Person person2 = new Person (“Sam”, 15);
int age = 21;
String name = “Jill”;
System.out.println (“\nParameter Passing… Original values…”);
System.out.println (“person1: ” + person1);
// name given in the third and fourth parameters.
// ——————————————————————-
public static void changePeople (Person p1, Person p2, int age, String name)
{
System.out.println (“\nInside changePeople… Original parameters…”);
System.out.println (“person1: ” + p1);
104 Chapter 7: Object-Oriented Design
p1.changeName (name);
p1.changeAge (age);
// Print changes
System.out.println (“\nInside changePeople… Changed values…”);
System.out.println (“person1: ” + p1);
System.out.println (“person2: ” + p2);
// ———————————————————-
public Person (String name, int age)
{
this.name = name;
this.age = age;
}
// ———————————————————-
// Returns the person’s name and age as a string.
// ———————————————————-
public String toString()
{
return name + ” – Age ” + age;
}
}
Chapter 7: Object-Oriented Design 105
Using the Comparable Interface
1. Write a class Compare3 that provides a static method largest. Method largest should take three Comparable parameters
2. Write a class Comparisons whose main method tests your largest method above.
First prompt the user for and read in three strings, use your largest method to find the largest of the three strings, and
print it out. (It’s easiest to put the call to largest directly in the call to println.) Note that since largest is a static
method, you will call it through its class name, e.g., Compare3.largest(val1, val2, val3).
106 Chapter 7: Object-Oriented Design
A Modified MiniQuiz Class
Files Question.java, Complexity.java, and MiniQuiz.java contain the classes in Listings 6.8-6.10of the text. These classes
demonstrate the use of the Complexity interface; class Question implements the interface, and class MiniQuiz creates two
Question objects and uses them to give the user a short quiz.
Save these three files to your directory and study the code in MiniQuiz.java. Notice that after the Question objects are
created, almost exactly the same code appears twice, once to ask and grade the first question, and again to ask and grade the
second question. Another approach is to write a method askQuestion that takes a Question object and does all the work of
asking the user the question, getting the user’s response, and determining whether the response is correct. You could then
simply call this method twice, once for q1 and once for q2. Modify the MiniQuiz class so that it has such an askQuestion
method, and replace the code in main that asks and grades the questions with two calls to askQuestion. Some things to keep
in mind:
The definition of askQuestion should be inside the MiniQuiz class but after the main method.
Since main is a static method, askQuestion must be static too. (A static method cannot call an instance method of the
same class.) Also, askQuestion is for use only by this class, so it should be declared private. So the header for
askQuestion should look like this:
• String possible, which is currently declared in main, will need to be defined in askQuestion instead.
• The Scanner object scan needs to be a static variable and moved outside of main (so it is available to askQuestion).
You do not need to make any changes to Question.java or Complexity .java.
//****************************************************************
// Question.java Author: Lewis/Loftus
//————————————————————–
// Sets up the question with a default complexity.
//————————————————————–
public Question (String query, String result)
{
question = query;
//————————————————————–
// Returns the complexity level for this question.
//————————————————————–
public int getComplexity()
Chapter 7: Object-Oriented Design 107
{
return complexityLevel;
}
//————————————————————–
// Returns the answer to this question.
//————————————————————–
public String getAnswer()
{
return answer;
}
return question + “\n” + answer;
}
}
//*****************************************************************
// Complexity.java Author: Lewis/Loftus
//
// Represents the interface for an object that can be assigned an
// explicit complexity.
//*****************************************************************
public interface Complexity
//*****************************************************************
//————————————————————–
// Presents a short quiz.
//————————————————————–
public static void main (String[] args)
{
Question q1, q2;
String possible;
Scanner scan = new Scanner(System.in);
q2.setComplexity (10);
System.out.print (q1.getQuestion());
System.out.println (” (Level: ” + q1.getComplexity() + “)”);
possible = scan.nextLine();
if (q1.answerCorrect(possible))
Chapter 7: Object-Oriented Design 109
A Flexible Account Class
File Account.java contains a definition for a simple bank account class with methods to withdraw, deposit, get the balance
and account number, and return a String representation. Note that the constructor for this class creates a random account
number. Save this class to your directory and study it to see how it works. Then modify it as follows:
1. Overload the constructor as follows:
public Account (double initBal, String owner, long number) – initializes the balance, owner, and account
2. Overload the withdraw method with one that also takes a fee and deducts that fee from the account.
File TestAccount.java contains a simple program that exercises these methods. Save it to your directory, study it to see what
it does, and use it to test your modified Account class.
//************************************************************
// Account.java
//
//————————————————-
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
}
// Adds deposit amount to balance.
//————————————————-
public void deposit(double amount)
{
balance += amount;
}
“\nAccount Number: ” + acctNum +
“\nBalance: ” + balance;
}
}
//************************************************************
// TestAccount.java
//
// A simple driver to test the overloaded methods of
System.out.println(“Enter account holder’s first name”);
name = scan.next();
acct = new Account(name);
System.out.println(“Account for ” + name + “:”);
System.out.println(acct);
System.out.println(acct);
System.out.print(“\nDepositing 100 into account, balance is now “);
acct.deposit(100);
System.out.println(acct.getBalance());
System.out.print(“\nWithdrawing $25, balance is now “);
Modifying the Coin Class
1. Create a new class named BiasedCoin that models a biased coin (heads and tails are not equally likely outcomes of a
flip). To do this modify the coin class from the Listing 5.4 of text (in the file Coin.java) as follows:
Add a private data member bias of type double. This data member will be a number between 0 and 1 (inclusive) that
2. Compile your class to make sure you have no syntax errors.
3. Write a program that uses three BiasedCoin objects. Instantiate one as a fair coin using the constructor with no
parameter. Read in the biases for the other two coins and instantiate those coins using the constructor with the bias as a