//******************************************************************** // CountFlips.java Author: Lewis and Loftus // // Demonstrates the use of a programmer-defined class. //******************************************************************** //Class coin needs to be in the same folder or imported public class CountFlips { //----------------------------------------------------------------- // Flips a coin multiple times and counts the number of heads // and tails that result. //----------------------------------------------------------------- public static void main (String[] args) { final int NUM_FLIPS = 1000; //local variable int heads = 0, tails = 0; Coin myCoin = new Coin(); // instantiate the Coin object for (int count=1; count <= NUM_FLIPS; count++) { myCoin.flip(); if (myCoin.getFace() == myCoin.HEADS) heads++; else tails++; } System.out.println ("The number flips: " + NUM_FLIPS); System.out.println ("The number of heads: " + heads); System.out.println ("The number of tails: " + tails); } } //******************************************************************** // Coin.java Author: Lewis and Loftus // // Represents a coin with two sides that can be flipped. //******************************************************************** public class Coin { public final int HEADS = 0; //member variable public final int TAILS = 1; private int face; //----------------------------------------------------------------- // Sets up the coin by flipping it initially. //----------------------------------------------------------------- public Coin () { flip(); } //----------------------------------------------------------------- // Flips the coin by randomly choosing a face. //----------------------------------------------------------------- public void flip () { face = (int) (Math.random() * 2); } //----------------------------------------------------------------- // Returns the current face of the coin as an integer. //----------------------------------------------------------------- public int getFace () { return face; } //----------------------------------------------------------------- // Returns the current face of the coin as a string. //----------------------------------------------------------------- public String toString() { String faceName; if (face == HEADS) faceName = "Heads"; else faceName = "Tails"; return faceName; } } //******************************************************************** // Cat.java //******************************************************************** public class Cat { private float weight; private int age; private boolean isFriendly; public Cat() { weight = 3.8f; age = 2; moodSwing(); } public void moodSwing() { isFriendly = ((int)(Math.random()*2) == 0); } public String toString() { String sWeight = "I weight " + weight + " kg.\n"; String sAge = "I'm " + age + " years old.\n"; String sFriendly = (isFriendly)? "I'm the nicest cat in the world" : "One more step and I'll attack."; return (sWeight+sAge+sFriendly); } public float eat(float food) { weight += food; System.out.println("it wasn't Fancy Feast's seafood fillet..."); wail(); return weight; } public void wail() { System.out.println("Miiiiaaawwwwwww!"); moodSwing(); } } //******************************************************************** // FeedTheCat.java //******************************************************************** public class FeedTheCat { public static void main(String args[]) { float fishBonesInKg = 1.2f; Cat Frisky = new Cat(); System.out.println(Frisky.toString()); System.out.println("We are about to feed the cat..."); float newWeight = Frisky.eat(fishBonesInKg); System.out.println("The cat should weight " + newWeight + " kg."); System.out.println(Frisky.toString()); } } //******************************************************************** // FlipRace.java Author: Lewis and Loftus // // Demonstrates the existence of separate data space in multiple // instantiations of a programmer-defined class. //******************************************************************** public class FlipRace { //----------------------------------------------------------------- // Flips two coins until one of them comes up heads a set number // of times in a row. //----------------------------------------------------------------- public static void main (String[] args) { final int GOAL = 3; int count1 = 0, count2 = 0; // Create two separate coin objects Coin coin1 = new Coin(); Coin coin2 = new Coin(); while (count1 < GOAL && count2 < GOAL) { coin1.flip(); coin2.flip(); // Print the flip results (uses Coin's toString method) System.out.print ("Coin 1: " + coin1); System.out.println (" Coin 2: " + coin2); // Increment or reset the counters count1 = (coin1.getFace() == coin1.HEADS) ? count1+1 : 0; count2 = (coin2.getFace() == coin2.HEADS) ? count2+1 : 0; } // Determine the winner if (count1 < GOAL) System.out.println ("Coin 2 Wins!"); else if (count2 < GOAL) System.out.println ("Coin 1 Wins!"); else System.out.println ("It's a TIE!"); } } //*********************************************************************** // SuperCat.java // Exact same class as Cat except the constructor takes input parameters //*********************************************************************** public class SuperCat { private float weight; private int age; private boolean isFriendly; public SuperCat(int catAge, float catWeight, boolean friendlyCat) { weight = catWeight; age = catAge; isFriendly = friendlyCat; } public void moodSwing() { isFriendly = ((int)(Math.random()*2) == 0); } public String toString() { String sWeight = "I weight " + weight + " kg.\n"; String sAge = "I'm " + age + " years old.\n"; String sFriendly = (isFriendly)? "I'm the nicest cat in the world." : "One more step and I'll attack."; return (sWeight+sAge+sFriendly); } public float eat(float food) { weight += food; System.out.println("it wasn't Fancy Feast's seafood fillet..."); wail(); return weight; } public void wail() { System.out.println("Miiiiaaawwwwwww!"); moodSwing(); } } //******************************************************************** // FeedTheCats.java // Tests the SuperCat constructor and creates two SuperCats //******************************************************************** public class FeedTheCats { public static void main(String args[]) { float fishBonesInKg = 1.2f; SuperCat frisky = new SuperCat(3,4.1f,true); SuperCat tiger = new SuperCat(2,12.4f,false); System.out.println("\nLet's check out Frisky's stats:"); System.out.println(frisky.toString()); System.out.println("\nLet's check out Tiger's stats:"); System.out.println(tiger.toString()); System.out.println("\nWe are about to feed Frisky..."); float newFriskyWeight = frisky.eat(fishBonesInKg); System.out.println("\nFrisky should now weight " + newFriskyWeight + " kg:"); System.out.println(frisky.toString()); System.out.println("\nWe are about to feed Frisky...to Tiger:"); float newTigerWeight = tiger.eat(newFriskyWeight); System.out.println("\nTiger should now weight " + newTigerWeight + " kg:"); System.out.println(tiger.toString()); } } //******************************************************************** // BankAccounts.java Author: Lewis and Loftus // // Driver to exercise the use of multiple Account objects. //******************************************************************** public class BankAccounts { //----------------------------------------------------------------- // Creates some bank accounts and requests various services. //----------------------------------------------------------------- public static void main (String[] args) { Account acct1 = new Account ("Ted Murphy", 72354, 102.56); Account acct2 = new Account ("Jane Smith", 69713, 40.00); Account acct3 = new Account ("Edward Demsey", 93757, 759.32); acct1.deposit (25.85); double smithBalance = acct2.deposit (500.00); System.out.println ("Smith balance after deposit: " + smithBalance); System.out.println ("Smith balance after withdrawal: " + acct2.withdraw (430.75, 1.50)); acct3.withdraw (800.00, 0.0); // exceeds balance acct1.addInterest(); acct2.addInterest(); acct3.addInterest(); System.out.println (); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); } } //******************************************************************** // Account.java Author: Lewis and Loftus // // Represents a bank account with basic services such as deposit // and withdraw. //******************************************************************** import java.text.NumberFormat; public class Account { private NumberFormat fmt = NumberFormat.getCurrencyInstance(); private final double RATE = 0.045; // interest rate of 4.5% private long acctNumber; private double balance; private String name; //----------------------------------------------------------------- // Sets up the account by defining its owner, account number, // and initial balance. //----------------------------------------------------------------- public Account (String owner, long account, double initial) { name = owner; acctNumber = account; balance = initial; } //----------------------------------------------------------------- // Validates the transaction, then deposits the specified amount // into the account. Returns the new balance. //----------------------------------------------------------------- public double deposit (double amount) { if (amount < 0) // deposit value is negative { System.out.println (); System.out.println ("Error: Deposit amount is invalid."); System.out.println (acctNumber + " " + fmt.format(amount)); } else balance = balance + amount; return balance; } //----------------------------------------------------------------- // Validates the transaction, then withdraws the specified amount // from the account. Returns the new balance. //----------------------------------------------------------------- public double withdraw (double amount, double fee) { amount += fee; if (amount < 0) // withdraw value is negative { System.out.println (); System.out.println ("Error: Withdraw amount is invalid."); System.out.println ("Account: " + acctNumber); System.out.println ("Requested: " + fmt.format(amount)); } else if (amount > balance) // withdraw value exceeds balance { System.out.println (); System.out.println ("Error: Insufficient funds."); System.out.println ("Account: " + acctNumber); System.out.println ("Requested: " + fmt.format(amount)); System.out.println ("Available: " + fmt.format(balance)); } else balance = balance - amount; return balance; } //----------------------------------------------------------------- // Adds interest to the account and returns the new balance. //----------------------------------------------------------------- public double addInterest () { balance += (balance * RATE); return balance; } //----------------------------------------------------------------- // Returns the current balance of the account. //----------------------------------------------------------------- public double getBalance () { return balance; } //----------------------------------------------------------------- // Returns the account number. //----------------------------------------------------------------- public long getAccountNumber () { return acctNumber; } //----------------------------------------------------------------- // Returns a one-line description of the account as a string. //----------------------------------------------------------------- public String toString () { return (acctNumber + "\t" + name + "\t" + fmt.format(balance)); } }