




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Coin penny = new Coin();
penny.flip();
System.out.println(penny);
if (penny.toString() == "Heads") { currentRun = currentRun + 1; } else { if (currentRun > maxRun) maxRun = currentRun; currentRun = 0; }
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); } }
System.out.println("Joe's account balance: " + acct2.getBalance());
acct1.withdraw(50);
System.out.println("Sally's account balance: " + acct1.getBalance());
acct1.chargeFee(); acct2.chargeFee();
acct2.changeName("Joseph");
acct1.printSummary(); acct2.printSummary();
public void chargeFee() { if (balance < 1000) { balance = balance – 10; } } public double chargeFee() { if (balance < 1000) { balance = balance – 10; } return balance; }
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(); } }