Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Starting Out with Java: From Control Structures through Objects 5th edition By Tony Gaddis Source Code: Chapter 6 Code Listing 6-1 (ObjectDemo.java) 1 import java.util.Scanner; // Needed for the Scanner class 2 import java.util.Random; // Needed for the Random class 3 import java.io.*; // Needed for file I/O classes 4 5 /* 6 This program writes random numbers to a file. Objects created are from 7 classes that are part of the Java API. 7 */ 8 9 public class ObjectDemo 10 { 11 public static void main(String[] args) throws IOException 12 { 13 int maxNumbers; // Max number of random numbers 14 int number; // To hold a random number 15 16 17 Scanner keyboard = new Scanner(System.in); 18 19 // Create a Random object to generate random numbers. 20 Random rand = new Random(); 21 22 25 26 27 28 29 30 31 32 33 34 35 System.out.print("How many random numbers should I write? "); maxNumbers = keyboard.nextInt(); for (int count = 0; count < maxNumbers; count++) { number = rand.nextInt(); // Write the random integer to the file. outputFile.println(number); 36 37 39 40 41 42 } 43 } } outputFile.close(); System.out.println("Done"); Program Output with Example Input Shown in Bold How many random numbers should I write? 10 [ Enter] Done Code Listing 6-2 (Rectangle.java) 1 /** 2 Rectangle class, phase 1 3 Under construction! 4 */ 5 6 public class Rectangle 7 { 8 private double length; 9 private double width; 10 11 /** 12 The setLength method stores a value in the 13 length field. 14 @param len The value to store in length. 15 */ 16 17 public void setLength( double len ) 18 { 19 length = len; 20 } 21 } Code Listing 6-3 (LengthDemo.java) 1 2 3 4 5 6 7 8 9 /** This program demonstrates the Rectangle class's setLength method. */ public class LengthDemo { public static void main(String[] args) { 12 13 14 15 16 17 Rectangle box = new Rectangle(); 19 box.setLength(10.0); // Constructor method? System.out.println("Sending the value 10.0 " + "to the setLength method."); (Continued) 22 System.out.println("Done."); 23 } // end main 24 } // end class Program Output Sending the value 10.0 to the setLength method. Done. Code Listing 6-4 (Rectangle.java) 1 2 3 4 5 6 7 8 /** Rectangle class, phase 2 Under construction! */ public class Rectangle { private double length; private double width; 9 10 11 /** 12 The setLength method stores a value in the 13 length field. 14 @param len The value to store in length. 15 */ 16 17 public void setLength( 18 { 19 length = len; 20 } 21 double len ) (Continued) 22 23 24 25 26 27 28 29 30 31 32 } /** The setWidth method stores a value in the width field. @param w The value to store in width. */ public void setWidth(double w) { width = w; } Code Listing 6-5 (Rectangle.java) 1 /** 2 Rectangle class, phase 3 3 Under construction ! 4 */ 5 6 public class Rectangle 7 { 8 private double length; 9 private double width; 10 11 /** 12 The setLength method stores a value in the 13 length field. 14 @param len The value to store in length. 15 */ 16 17 public void setLength(double len) 18 { 19 length = len; 20 } 21 (Continued) 22 /** 23 The setWidth method stores a value in the 24 width field. 25 @param w The value to store in width. 26 */ 27 public void setWidth(double w) 29 { 30 width = w; 31 } 28 32 33 /** 34 The getLength method returns a Rectangle 35 object's length. 36 @return The value in the length field. 37 */ 38 public double getLength() 40 { 41 return length; 42 } 39 43 (Continued) 44 45 46 47 48 49 /** 50 51 public double getWidth() 52 53 54 } The getWidth method returns a Rectangle object's width. @return The value in the width field. */ { return width; } Code Listing 6-6 (LengthWidthDemo.java) 1 /** 2 This program demonstrates the Rectangle class 3 4 */ 5 6 public 7 { 8 class LengthWidthDemo public static void main(String[] args) 9 { 10 11 12 15 Rectangle box = new Rectangle(); box.setLength(10.0); 16 17 19 box.setWidth(20.0); 20 (Continued) 21 // Display the object's length and width. 22 System.out.println("The box's length is " + box.getLength()); 23 24 System.out.println("The box's width is " + 25 26 } 27 } Program Output The box's length is 10.0 The box's width is 20.0 box.getWidth()); Code Listing 6-7 (Rectangle.java) 1 /** 2 Rectangle class, phase 4 3 Under construction! 4 */ 5 6 public class Rectangle 7 { 8 private double length; 9 private double width; 10 11 /** 12 The setLength method stores a value in the 13 length field. 14 @param len The value to store in length. 15 */ 16 17 public void setLength( 18 { 19 length = len; 20 } 21 double len ) (Continued) 22 23 24 25 26 27 /** The setWidth method stores a value in the width field. @param w The value to store in width. */ 28 public void setWidth( double w ) 29 { 30 31 32 33 34 35 36 37 38 width = w; } /** The getLength method returns a Rectangle object's length. @return The value in the length field. */ 39 public double getLength() 40 { 41 42 43 return length; } 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 /** 61 62 public double getArea() 63 64 65 } The getWidth method returns a Rectangle object's width. @return The value in the width field. */ public double getWidth() { return width; } /** The getArea method returns a Rectangle object's area. @return The product of length times width. */ { return length } * width; // What is Stale data? Code Listing 6-8 (RectangleDemo.java) 1 /** 2 This program demonstrates the 3 5 */ 6 7 public class RectangleDemo 8 { 9 public static void main(String[] args) 10 { Rectangle class 12 13 Rectangle box = new Rectangle(); 15 box.setLength(10.0); box.setWidth(20.0); 16 17 19 20 System.out.println("The box's length is " + box. getLength() ); (Continued) 21 23 System.out.println("The box's width is " + box.getWidth()); 24 25 27 System.out.println("The box's area is " + 28 29 } 30 } box.getArea()); Program Output The box's length is 10.0 The box's width is 20.0 The box's area is 200.0 Code Listing 6-9 (RoomAreas.java) 1 import javax.swing.JOptionPane; 2 3 /** 4 This program creates three instances of the 5 Rectangle class. 6 */ 7 8 public class RoomAreas 9 { 10 public static void main(String[] args) 11 { 12 double number; // To hold a number 13 double totalArea; // The total area 14 String input; // To hold user input 15 16 // Create three Rectangle objects. 17 Rectangle kitchen = new Rectangle(); Rectangle bedroom = new Rectangle(); Rectangle den = new Rectangle(); 18 19 20 22 23 24 25 input = JOptionPane.showInputDialog( "What is the " + "kitchen's length?"); number = Double.parseDouble(input); kitchen.setLength(number); 26 27 28 29 input = JOptionPane.showInputDialog("What is the " + "kitchen's width?"); number = Double.parseDouble(input); kitchen.setWidth(number); 30 32 33 34 35 input = JOptionPane.showInputDialog("What is the " + "bedroom's length?"); number = Double.parseDouble(input); 36 37 38 39 input = JOptionPane.showInputDialog("What is the " + "bedroom's width?"); number = Double.parseDouble(input); 40 bedroom.setLength(number); bedroom.setWidth(number); 42 43 44 input = JOptionPane.showInputDialog("What is the " + "den's length?"); number = Double.parseDouble(input); 45 den.setLength(number); 46 47 48 input = JOptionPane.showInputDialog("What is the " + "den's width?"); number = Double.parseDouble(input); 49 den.setWidth(number); 50 //__________________________________________________________ 52 53 54 totalArea = kitchen.getArea() + bedroom.getArea() + den.getArea(); 56 JOptionPane.showMessageDialog(null, " + "The total area” + 57 "of the rooms is " + totalArea); 59 System.exit(0); 60 } 61 } Code Listing 6-10 (Rectangle.java) 1 /** 2 Rectangle class, 3 */ 4 phase 5 5 public class Rectangle 6 { 7 private double length; 8 private double width; 9 10 /** 11 12 13 14 15 16 17 18 19 20 Constructor @param len The length of the rectangle. @param w The width of the rectangle. */ public Rectangle( double len, double w ) { length = len; width = w; } . . . The remainder of the class has not changed, and is not shown. Code Listing 6-11 (ConstructorDemo.java) 1 /** 2 This program demonstrates the Rectangle class's 3 constructor. 4 */ 5 6 public class ConstructorDemo 7 { 8 public static void main(String[] args) 9 { 10 12 13 14 15 16 17 18 19 20 Rectangle box = new Rectangle( 5.0, 15.0 ); System.out.println("The box's length is " + box.getLength()); System.out.println("The box's width is " + box.getWidth()); 21 (Continued) 22 23 System.out.println("The box's area is " + 24 25 } 26 } Program Output The box's length is 5.0 The box's width is 15.0 The box's area is 75.0 box.getArea()); Code Listing 6-12 (CellPhone.java) 1 /** 2 The 3 */ 4 CellPhone class holds data about a cell phone. 5 public class 6 { 7 // Fields 8 9 CellPhone private String manufact; private String model; private double retailPrice; 10 11 12 /** 13 Constructor 14 @param man The phone's manufacturer. 15 @param mod The phone's model number. 16 @param price The phone's retail price. 17 */ 18 19 public 20 { 21 22 23 // Manufacturer // Model // Retail price CellPhone( String man, String mod, double price ) manufact = man; model = mod; retailPrice = price; (Continued) 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 } /** The setManufact method sets the phone's manufacturer name. @param man The phone's manufacturer. */ public void setManufact( String man ) { manufact = man; } /** The setMod method sets the phone's model number. @param mod The phone's model number. */ public void setMod( String mod ) { model = mod; } (Continued) 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 /** The setRetailPrice method sets the phone's retail price. @param price The phone's retail price. */ public void setRetailPrice( double price ) { retailPrice = price; } /** getManufact method @return The name of the phone's manufacturer. */ public String getManufact() { return manufact; } 68 (Continued) 69 /** 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 } getModel method @return The phone's model number. */ public String getModel() { return model; } /** getretailPrice method @return The phone's retail price. */ public double getRetailPrice() { return retailPrice; } Code Listing 6-13 (CellPhoneTest.java) 1 import java.util.Scanner; 2 3 /** 4 This program runs a simple test 5 of the CellPhone class. 6 */ 7 8 public class CellPhoneTest 9 { 10 public static void main(String[] args) 11 { 12 String testMan; // To hold a manufacturer 13 14 15 16 17 18 19 20 21 22 testMod; double testPrice; String // To hold a model number // To hold a price Scanner keyboard = new Scanner(System.in); System.out.print("Enter the manufacturer: "); testMan = keyboard.nextLine(); (Continued) 23 24 25 26 27 28 29 System.out.print("Enter the model number: "); testMod = keyboard.nextLine(); System.out.print("Enter the retail price: "); testPrice = keyboard.nextDouble(); 30 31 32 34 35 36 37 38 39 // Create an instance of the CellPhone class, CellPhone phone = new CellPhone( testMan, testMod, testPrice ); System.out.println(); System.out.println("Here is the data that you provided:"); System.out.println("Manufacturer: " + phone getManufact()); . 40 System.out.println("Model number: " + phone.getModel()); 41 42 43 } System.out.println("Retail price: " + phone.getRetailPrice()); } (Continued) Program Output with Example Input Shown in Bold Enter the manufacturer: Acme Electronics [ Enter ] Enter the model number: M1000 [ Enter ] Enter the retail price: 199.99 [ Enter ] Here is the data that you provided: Manufacturer: Acme Electronics Model number: M1000 Retail price: $199.99 Code Listing 6-14 (Die.java) 1 import java.util.Random; 2 3 /** 4 The Die class simulates 5 */ 6 7 public class Die 8 { 9 private int sides; 10 private int value; a die of varying sizes. // Number of sides // The die's value 11 12 /** 13 The constructor performs an initial 14 roll of the die. 15 @param numSides The number of sides for this die. 16 */ 17 18 19 20 21 22 public Die( int numSides ) { sides = numSides; roll(); // What are we calling? } 23 (Continued) 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 /** The roll method simulates the rolling of the die. */ public void roll() { // Class or Instance method? Random rand = new Random(); value = rand.nextInt(sides) + 1; // What is value? } /** getSides method @return The number of sides for this die. */ public int getSides() { return sides; // What is sides? } (Continued) 47 48 /** 49 getValue method 50 @return The value of the die. 51 */ 52 53 public int getValue() 54 { 55 return value; 56 } 57 } End Class Code Listing 6-15 (DiceDemo.java) 1 /** 2 This 3 */ 4 program simulates the rolling of dice. 5 public 6 { 7 8 9 10 11 13 14 15 16 18 19 20 21 22 23 class DiceDemo public static void main(String[] args) { final int DIE1_SIDES = 6; final int DIE2_SIDES = 12; final int MAX_ROLLS = 5; // Number of sides for die #1 // Number of sides for die #2 // Number of times to roll Die die1 = new Die(DIE1_SIDES); Die die2 = new Die(DIE2_SIDES); System.out.println("This simulates the rolling of a " + DIE1_SIDES + " sided die and a " + DIE2_SIDES + " sided die."); System.out.println("Initial value of the dice:"); // ? System.out.println( die1.getValue() + " " + die2.getValue() ); (Continued) 26 27 28 29 31 32 33 35 System.out.println("Rolling the dice " + MAX_ROLLS + " times."); for (int i = 0; i < MAX_ROLLS; i++) { die1.roll(); die2.roll(); System.out.println( die1.getValue() + " " + die2.getValue() ); 36 } 37 } // End main 38 } // End Class Program Output This simulates the rolling of a 6 sided die and a 12 sided die. Initial value of the dice: 2 7 Rolling the dice 5 times. 3 5 5 2 2 1 4 1 5 9 Code Listing 6-16 (DieArgument.java) 1 /** 2 This program rolls a 6-sided die and 3 a 20-sided die. Client of Die Class. 4 */ 5 6 public 7 { class DieArgument 8 public static void 9 10 { main(String[] args) final int SIX_SIDES = 6; 11 12 13 final int TWENTY_SIDES = 20; 14 15 Die sixDie = new Die(SIX_SIDES); 17 18 Die twentyDie = new Die(TWENTY_SIDES); 19 rollDie(sixDie); rollDie(twentyDie); 20 21 22 // What type of method? // Passing a Reference as an Argument } (Continued) 23 24 /** 25 This method simulates a die roll, displaying 26 the die's number of sides and value. 27 @param d The Die object to roll. 28 */ 29 30 31 public static void rollDie(Die d) { 33 34 37 38 40 // Receiving the Reference Argument .getSides() + System.out.println("Rolling a " + d " sided die.") d.roll(); .getValue()); System.out.println("The die's value: " + d 41 } 42 } Program Output Rolling a 6 sided die. The die's value: 3 Rolling a 20 sided die. The die's value: 19 Code Listing 6-17 (Dealer.java) 1 /** 2 Dealer 3 */ 4 5 public 6 { 7 class for the game of Cho-Han( Uses Die class ) class Dealer private int die1Value; private int die2Value; 8 9 10 /** 11 Constructor 12 */ 13 14 // The value of die #1 // The value of die #2 public Dealer() 15 { 16 17 die1Value = 0; die2Value = 0; 18 } 20 /** 21 22 23 */ The rollDice their values. method rolls the dice and saves (Continued) 24 25 26 27 28 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 public void rollDice() { final int SIDES = 6; // Number of sides for the dice Die die1 = new Die(SIDES); Die die2 = new Die(SIDES); die1Value = die1.getValue(); // Where is memory for die1Value die2Value = die2.getValue(); // and die2Value allocated? } /** The getChoOrHan method returns the result of the dice roll, Cho or Han. @return Either "Cho (even)" or "Han (odd)" */ public String getChoOrHan() { String result; // To hold the result (Continued) 47 48 49 50 51 52 int sum = die1Value + die2Value; if (sum % 2 == 0) result = "Cho (even)"; else result = "Han (odd)"; 53 54 55 56 57 58 59 60 61 62 63 64 65 66 return result; } /** The getDie1Value method returns the value of die #1. @return The die1Value field */ 67 public int getDie1Value() 68 { 69 return die1Value; 70 } 71 72 /** 73 The getDie2Value method returns the value of 74 die #2. 75 @return The die2Value field 76 */ 77 78 public int getDie2Value() 79 { 80 return die2Value; 81 } 82 } Code Listing 6-18 (Player.java) 1 import java.util.Random; 2 3 /** 4 Player class for the game of Cho-Han 5 */ 6 7 public class Player 8 { 9 private String name; // The player's 10 private String guess; // The player's 11 private int points; // The player's 12 13 /** 14 Constructor 15 @param playerName The player's name. 16 */ 17 18 public Player(String playerName) 19 { 20 name = playerName; 21 guess = ""; 22 points = 0; 23 } name guess points (Continued) 24 25 26 27 28 29 30 31 33 34 36 37 /** The makeGuess method causes the player to guess either "Cho (even)" or "Han (odd)". */ public void makeGuess() { Random rand = new Random(); int guessNumber = rand.nextInt(2); 40 if (guessNumber == 0) 41 guess = "Cho (even)"; 42 else 43 guess = "Han (odd)"; 44 } 45 (Continued) 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 /** The addPoints method adds a specified number of points to the player's current balance. @newPoints The points to add. */ public void addPoints(int newPoints) { points += newPoints; } /** The getName method returns the player's name. @return The value of the name field. */ public String getName() { return name; } (Continued) 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 } /** The getGuess method returns the player's guess. @return The value of the guess field. */ public String getGuess() { return guess; } /** The getPoints method returns the player's points @return The value of the points field. */ public int getPoints() { return points; } Code Listing 6-19 (ChoHan.java) 1 import java.util.Scanner; 2 3 public class ChoHan 4 { 5 public static void main(String[] args) 6 { 7 final int MAX_ROUNDS = 5; // Number of rounds 8 String player1Name; // First player's name 9 String player2Name; // Second player's name 10 11 12 Scanner keyboard = new Scanner(System.in); 13 15 System.out.print("Enter the first player's name: "); 16 player1Name = keyboard.nextLine(); 17 18 19 21 22 System.out.print("Enter the second player's name: "); player2Name = keyboard.nextLine(); Dealer dealer = new Dealer(); (Continued) 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 // Create the two players. Player player1 = new Player(player1Name); Player player2 = new Player(player2Name); // Play the rounds. for (int round = 0; round < MAX_ROUNDS; round++) { System.out.println("----------------------------"); System.out.printf("Now playing round %d.\n", round + 1); dealer.rollDice(); player1.makeGuess(); player2.makeGuess(); 38 39 40 roundResults( dealer, player1, player2 ); 41 42 43 } 44 // Display the grand winner. 45 displayGrandWinner( player1, player2 ); (Continued) 46 } // End Main 47 48 /** 49 The roundResults method determines the results of 50 the current round. 51 @param dealer The Dealer object 52 @param player1 Player #1 object 53 @param player2 Player #2 object 54 */ 55 56 public static void roundResults( Dealer dealer, Player player1, 57 Player player2 ) 58 { 59 // Show the dice values. 60 System.out.printf("The dealer rolled %d and %d.\n", 61 62 63 64 65 66 67 } dealer.getDie1Value(), dealer.getDie2Value()); System.out.printf("Result: %s\n", dealer.getChoOrHan() ); // Check each player's guess and award points. checkGuess( player1, dealer ); checkGuess( player2, dealer ); (Continued) 68 69 70 71 72 73 74 75 76 77 78 /** The checkGuess method checks a player's guess against the dealer's result. @param player The Player object to check. @param dealer The Dealer object. */ public static void checkGuess( Player player, Dealer dealer ) { final int POINTS_TO_ADD = 1; // Points to award winner 79 String guess = player.getGuess(); // Player's guess 80 81 82 83 84 String choHanResult = dealer.getChoOrHan(); // Cho or Han // Display the player's guess. System.out.printf("The player %s guessed %s.\n", player.getName(), player.getGuess()); 85 86 // Award points if the player guessed correctly. 87 if (guess.equalsIgnoreCase( choHanResult) ) (Continued) 88 { . 89 player addPoints(POINTS_TO_ADD); 90 System.out.printf("Awarding %d point(s) to %s.\n", 91 POINTS_TO_ADD, player.getName() ); 92 } 93 } // End checkGuess 94 95 /** 96 The displayGrandWinner method displays the game's grand winner. 97 @param player1 Player #1 98 @param player2 Player #2 99 */ 100 101 public static void displayGrandWinner( Player player1, Player player2 ) 102 { 103 System.out.println("----------------------------"); 104 System.out.println("Game over. Here are the results:"); 105 System.out.printf("%s: %d points.\n", player1.getName(), 106 player1.getPoints()); 107 System.out.printf("%s: %d points.\n", player2.getName(), 108 player2.getPoints()); 109 (Continued) 110 111 112 113 114 115 116 } 117 } if ( player1.getPoints() > player2.getPoints() ) System.out.println( player1.getName() + " is the grand winner!"); else if ( player2.getPoints() > player1.getPoints() ) System.out.println( player2.getName() + " is the grand winner!"); else System.out.println("Both players are tied!"); Program Output Enter the first player's name: Chelsea [ Enter ] Enter the second player's name: Chris [ Enter ] ---------------------------Now playing round 1. The dealer rolled 3 and 6. Result: Han (odd) The player Chelsea guessed Han (odd). Awarding 1 point(s) to Chelsea. The player Chris guessed Han (odd). Awarding 1 point(s) to Chris. ---------------------------Now playing round 2. The dealer rolled 4 and 5. Result: Han (odd) The player Chelsea guessed Cho (even). The player Chris guessed Cho (even). ---------------------------Now playing round 3. The dealer rolled 5 and 6. Result: Han (odd) The player Chelsea guessed Cho (even). The player Chris guessed Han (odd). Awarding 1 point(s) to Chris. ---------------------------Now playing round 4. The dealer rolled 1 and 6. Result: Han (odd) The player Chelsea guessed Cho (even). The player Chris guessed Cho (even). ---------------------------(Continued) Now playing round 5. The dealer rolled 6 and 6. Result: Cho (even) The player Chelsea guessed Han (odd). The player Chris guessed Cho (even). Awarding 1 point(s) to Chris. ---------------------------Game over. Here are the results: Chelsea: 1 points. Chris: 3 points. Chris is the grand winner! Code Listing 6-20 (BankAccount.java) 1 /** 2 The 3 */ 4 BankAccount class simulates a bank account. Overloaded constructors. 5 public 6 { 7 8 9 class BankAccount private double balance; // Account balance Data field( Instance Variable ) /** 10 11 12 */ 13 This constructor - 1 sets the starting balance at 0.0. 14 public BankAccount() 15 { 16 17 } 18 19 /** balance = 0.0; 20 21 22 23 */ This constructor - 2 sets the starting balance to the value passed as an argument. @param startBalance The starting balance. (Continued) 24 25 public BankAccount( 26 { double startBalance ) 27 28 } 29 30 /** balance = startBalance; 31 32 33 34 */ 35 This constructor – 3 sets the starting balance to the value in the String argument. @param str The starting balance, as a String. 36 public BankAccount( 37 { String str ) 38 39 } 40 41 /** balance = Double.parseDouble(str); 42 43 44 45 46 */ The deposit method – 1 makes a deposit into the account. @param amount The amount to add to the balance field. (Continued) 47 48 49 50 51 52 53 54 55 56 57 58 59 public void deposit( double amount ) { balance += amount; } /** The deposit method – 2 makes a deposit into the account. @param str The amount to add to the balance field, as a String. */ 60 public void deposit(String str) 61 { 62 balance += Double.parseDouble(str); 63 } 64 65 /** 66 The withdraw method - 1 withdraws an amount 67 from the account. 68 @param amount The amount to subtract from (Continued) 69 70 */ 71 the balance field. 72 public void withdraw( 73 { double amount ) 74 75 } 76 77 /** balance -= amount; 78 79 80 81 82 */ 83 The withdraw method – 2 withdraws an amount from the account. @param str The amount to subtract from the balance field, as a String. 84 public void withdraw( 85 { String str ) 86 87 } 88 89 /** balance -= Double.parseDouble(str); 90 The setBalance method - 1 sets the account balance. 91 92 */ 93 @param b The value to store in the balance field. 94 public void setBalance(double 95 { b) 96 balance = b; 97 } 98 99 /** 100 The setBalance method – 2 sets the account balance. 101 @param str The value, as a String, to store in 102 the balance field. 103 */ 104 105 106 107 108 109 public void setBalance(String str) { balance = Double.parseDouble(str); } (Continued) 110 111 112 113 114 115 116 117 118 119 120 } /** The getBalance method returns the account balance. @return The value in the balance field. */ public double getBalance() { return balance; } Code Listing 6-21 (AccountTest.java) 1 import javax.swing.JOptionPane; // For the JOptionPane class 2 import java.text.DecimalFormat; // For the DecimalFormat class 3 4 /** 5 This program demonstrates the BankAccount class. 6 */ 7 8 public class AccountTest 9 { 10 public static void main(String[] args) 11 { 12 String input; 13 14 15 DecimalFormat dollar = new DecimalFormat("#,###.00"); 16 17 18 input = JOptionPane.showInputDialog("What is your " + 19 "account's starting balance?"); 20 22 BankAccount account = new BankAccount(input); (Continued) 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 input = JOptionPane.showInputDialog("How much were " + "you paid this month? "); account.deposit(input); JOptionPane.showMessageDialog(null, "Your pay has been deposited.\n" + "Your current balance is $ " + . . dollar format( account getBalance()) ); input = JOptionPane.showInputDialog("How much would " + "you like to withdraw? "); account.withdraw(input); 41 42 43 44 45 JOptionPane.showMessageDialog(null, "Now your balance is $" + . . dollar format(account getBalance()) ); (Continued) (Continued) Code Listing 6-21 (AccountTest.java) 46 47 System.exit(0); } end main 49 } end class 48