Lab 4 Solution: Creating Objects with Java Classes - Coin and Account, Lab Reports of Computer Science

Solutions to lab 4 exercises in java, focusing on creating objects using the coin and account classes. The coin class determines the maximum run of heads during 100 coin flips, while the account class manages bank accounts with methods for deposit, withdrawal, name change, and fee charging.

Typology: Lab Reports

Pre 2010

Uploaded on 04/12/2010

koofers-user-1vj-1
koofers-user-1vj-1 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Solution to “Part I: Using a Class to Create Objects” (Lab 4)
The Coin Class
Let’s call our Coin object “penny.” To create penny, we add the line:
Coin penny = new Coin();
The tasks that are required inside the loop are:
Flip the coin using the flip method,
Print the result of the flip using the toString method (implicitly), and
Update the values for the currentRun and maxRun variables.
The first task is done by calling the method flip on the penny object. The line that performs this task is:
penny.flip();
The second task is to print the results of each flip. Depending on the current value of the face variable, the
toString method will return either the String “Heads” or “Tails.” The return value of the toString method is the
legible representation of the penny object. The toString method is implicitly used in the following line to display
whether the flip was heads or tails:
System.out.println(penny);
The third task is to update the currentRun and maxRun variables. The main purpose of this program is to
determine the maximum run of heads during the 100 times the coin was flipped. To find this value, we need to
calculate all the runs of heads during the 100 times the coin was flipped and determine the longest (or
maximum) run. Since the flips are generated in a loop, we need to evaluate each time the loop is executed.
For each flip in the loop, we check whether it is heads or tails. If it is heads, we increment the currentRun
variable by one. The currentRun variable will contain the length of the current run of heads. If it is tails, we
reset the currentRun variable to zero, because the run of heads has ended. Before we reset the currentRun
variable, we compare the value of the currentRun variable with the value of the maxRun variable. If the
currentRun variable is greater than the maxRun variable, then we’ve found the longest run of heads up to this
point and we set the value of the maxRun variable to the value of the currentRun variable. Then we can reset the
currentRun variable to zero.
if (penny.toString() == "Heads")
{
currentRun = currentRun + 1;
}
else
{
if (currentRun > maxRun)
maxRun = currentRun;
currentRun = 0;
}
Finally at the end of the program, we print the value of the maxRun, which should contain the maximum run of
heads.
System.out.println("The maxmimum run is: " + maxRun);
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lab 4 Solution: Creating Objects with Java Classes - Coin and Account and more Lab Reports Computer Science in PDF only on Docsity!

Solution to “Part I: Using a Class to Create Objects” (Lab 4)

The Coin Class

Let’s call our Coin object “penny.” To create penny, we add the line:

Coin penny = new Coin();

The tasks that are required inside the loop are:

• Flip the coin using the flip method,

• Print the result of the flip using the toString method (implicitly), and

• Update the values for the currentRun and maxRun variables.

The first task is done by calling the method flip on the penny object. The line that performs this task is:

penny.flip();

The second task is to print the results of each flip. Depending on the current value of the face variable, the

toString method will return either the String “Heads” or “Tails.” The return value of the toString method is the

legible representation of the penny object. The toString method is implicitly used in the following line to display

whether the flip was heads or tails:

System.out.println(penny);

The third task is to update the currentRun and maxRun variables. The main purpose of this program is to

determine the maximum run of heads during the 100 times the coin was flipped. To find this value, we need to

calculate all the runs of heads during the 100 times the coin was flipped and determine the longest (or

maximum) run. Since the flips are generated in a loop, we need to evaluate each time the loop is executed.

For each flip in the loop, we check whether it is heads or tails. If it is heads, we increment the currentRun

variable by one. The currentRun variable will contain the length of the current run of heads. If it is tails, we

reset the currentRun variable to zero, because the run of heads has ended. Before we reset the currentRun

variable, we compare the value of the currentRun variable with the value of the maxRun variable. If the

currentRun variable is greater than the maxRun variable, then we’ve found the longest run of heads up to this

point and we set the value of the maxRun variable to the value of the currentRun variable. Then we can reset the

currentRun variable to zero.

if (penny.toString() == "Heads") { currentRun = currentRun + 1; } else { if (currentRun > maxRun) maxRun = currentRun; currentRun = 0; }

Finally at the end of the program, we print the value of the maxRun, which should contain the maximum run of

heads.

System.out.println("The maxmimum run is: " + maxRun);

// Runs.java // // Finds the length of the longest run of heads in 100 flips of a coin. // ******************************************************************** public class Runs { public static void main (String[] args) { final int FLIPS = 100; // number of coin flips int currentRun = 0; // length of the current run of HEADS int maxRun = 0; // length of the maximum run so far // Create a coin object Coin penny = new Coin(); // Flip the coin FLIPS times for (int i = 0; i < FLIPS; i++) { // Flip the coin & print the result penny.flip(); System.out.println(penny); // Update the run information if (penny.toString() == "Heads") { currentRun = currentRun + 1; } else { if (currentRun > maxRun) maxRun = currentRun; currentRun = 0; } } // Print the results System.out.println("The maximum run is: " + maxRun); } }

Third, we print Joe’s new balance by calling the getBalance method on acct2. The call to the getBalance method

returns a value of type double, which should be the balance in Joe’s account. We want to print this value that is

returned.

System.out.println("Joe's account balance: " + acct2.getBalance());

Fourth, we withdraw $50 from Sally’s account by calling the withdraw method on acct1. The withdraw method is

passed with the amount of funds to withdraw. The Account class will need this amount to determine how much

money to subtract from Sally’s account.

acct1.withdraw(50);

Fifth, we print Sally’s new balance by calling the getBalance method on acct1. This is similar to the third update

we did (printing Joe’s balance).

System.out.println("Sally's account balance: " + acct1.getBalance());

Sixth, we charge the fees for both accounts by calling the chargeFee methods on both acct1 and acct2.

acct1.chargeFee(); acct2.chargeFee();

Seventh, we change the name of the account holder of the account that originally had “Joe” as the name of the

account holder. This is done by calling the changeName method on acct2. The changeName method is passed

with the new name for the account.

acct2.changeName("Joseph");

Finally, we print the summary of both accounts by calling the printSummary methods on both acct1 and acct2.

acct1.printSummary(); acct2.printSummary();

In step 3, we need to change the chargeFee method so that it returns the updated balance of the account after the

fee has been charged. Initially the method has void as the return type, because it doesn’t return anything. It will

now return the balance of the account, which is of type double. The return type will need to be changed to

double.

Before After

public void chargeFee() { if (balance < 1000) { balance = balance – 10; } } public double chargeFee() { if (balance < 1000) { balance = balance – 10; } return balance; }

In step 4, we need to print the balances of both accounts by using the value that is returned by the chargeFee

method. This is done by calling the chargeFee methods on both acct1 and acct2. These method calls can be

done inside the call to the methods that print to the console.

System.out.println("Joe's account balance: " + acct2.chargeFee()); System.out.println("Sally's account balance: " + acct1. chargeFee());

// Deducts $10 service fee if balance is under $ //---------------------------------------------- /* // step 1 version: public void chargeFee() { if (balance < 1000) balance = balance - 10; } */ // step 4 version: public double chargeFee() { if (balance < 1000) balance = balance - 10; return balance; } //---------------------------------------------- // Changes the name on the account //---------------------------------------------- public void changeName(String newName) { name = newName; } }

// ManageAccounts.java // // Use Account class to create and manage Sally and Joe's // bank accounts // **************************************************************** public class ManageAccounts { public static void main(String[] args) { Account acct1, acct2; //create account1 for Sally with $ acct1 = new Account(1000, "Sally", 1111); //create account2 for Joe with $ acct2 = new Account(500, "Joe", 2222); //deposit $100 to Jo acct2.deposit(100); e's account //print Joe's new balance (use getBalance()) System.out.println("Joe's account balance: " + acct2.getBalance()); //withdraw $50 from acct1.withdraw(50); Sally's account //print Sally's new balance (use getBalance()) System.out.println("Sally's account balance: " + acct1.getBalance()); //charge fees to both accounts /* //step 1 version: acct1.chargeFee(); acct2.chargeFee(); */ //step 4 version: System.out.println("Sally's account balance: " + acct1.chargeFee()); System.out.println("Joe's account balance: " + acct2.chargeFee()); //change the name on Joe's account to Joseph acct2.changeName("Joseph"); //print summary for both accounts acct1.printSummary(); acct2.printSummary(); } }