I 2 3 4 public static void displayAccount (Account accountToDisplay) { // place the statement that displays // accountToDisplay's name and balance here }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
I // Fig. 7.9: AccountTest.java
2 // Inputting and outputting floating-point numbers with Account objects.
3 import java.util.Scanner;
4
Fig. 7.9 Inputting and outputting floating-point numbers with Account objects. (Part 1 of 2.)
314
5
6
7
8
9
10
II
12
13
14
15
16
17
18
19
20
NNNN-HANSE
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class AccountTest {
public static void main(String[] args) {
Account account1 = new Account ("Jane Green", 50.00);
Account account2 = new Account ("John Blue", -7.53);
41
42
43 }
Chapter 7 Introduction to Classes and Objects
}
// display initial balance of each object
System.out.printf("%s balance: $ %.2f%n",
account1.getName(), account1.getBalance());
System.out.printf("%s balance: $ %.2f%n%n",
account2.getName(), account2.getBalance());
// create a Scanner to obtain input from the command window
Scanner input = new Scanner(System.in);
System.out.print("Enter deposit amount for accountl: "); // prompt
double depositAmount = input.nextDouble(); // obtain user input
System.out.printf("%nadding %.2f to accountl balance%n%n",
depositAmount);
account1.deposit (depositAmount); // add to account1's balance
// display balances
System.out.printf("%s balance: $%.2f%n",
account1.getName(), account1.getBalance());
System.out.printf("%s balance: $ %.2f%n%n",
account2.getName(), account2.getBalance());
System.out.print("Enter deposit amount for account2: "); // prompt
depositAmount = input.nextDouble(); // obtain user input
System.out.printf("%nadding %.2f to account2 balance%n%n",
depositAmount);
account2.deposit (depositAmount); // add to account2 balance
// display balances
System.out.printf("%s balance: $%.2f%n",
account1.getName(), account1.getBalance());
System.out.printf("%s balance: $ %.2f%n%n",
account2.getName(), account2.getBalance());
Jane Green balance: $50.00
John Blue balance: $0.00
Enter deposit amount for account1: 25.53
adding 25.53 to account1 balance
Jane Green balance: $75.53
John Blue balance: $0.00
Enter deposit amount for account2: 123.45
adding 123.45 to account2 balance
Jane Green balance: $75.53
John Blue balance: $123.45
Fig. 7.9 Inputting and outputting floating-point numbers with Account objects. (Part 2 of 2.)
Transcribed Image Text:I // Fig. 7.9: AccountTest.java 2 // Inputting and outputting floating-point numbers with Account objects. 3 import java.util.Scanner; 4 Fig. 7.9 Inputting and outputting floating-point numbers with Account objects. (Part 1 of 2.) 314 5 6 7 8 9 10 II 12 13 14 15 16 17 18 19 20 NNNN-HANSE 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public class AccountTest { public static void main(String[] args) { Account account1 = new Account ("Jane Green", 50.00); Account account2 = new Account ("John Blue", -7.53); 41 42 43 } Chapter 7 Introduction to Classes and Objects } // display initial balance of each object System.out.printf("%s balance: $ %.2f%n", account1.getName(), account1.getBalance()); System.out.printf("%s balance: $ %.2f%n%n", account2.getName(), account2.getBalance()); // create a Scanner to obtain input from the command window Scanner input = new Scanner(System.in); System.out.print("Enter deposit amount for accountl: "); // prompt double depositAmount = input.nextDouble(); // obtain user input System.out.printf("%nadding %.2f to accountl balance%n%n", depositAmount); account1.deposit (depositAmount); // add to account1's balance // display balances System.out.printf("%s balance: $%.2f%n", account1.getName(), account1.getBalance()); System.out.printf("%s balance: $ %.2f%n%n", account2.getName(), account2.getBalance()); System.out.print("Enter deposit amount for account2: "); // prompt depositAmount = input.nextDouble(); // obtain user input System.out.printf("%nadding %.2f to account2 balance%n%n", depositAmount); account2.deposit (depositAmount); // add to account2 balance // display balances System.out.printf("%s balance: $%.2f%n", account1.getName(), account1.getBalance()); System.out.printf("%s balance: $ %.2f%n%n", account2.getName(), account2.getBalance()); Jane Green balance: $50.00 John Blue balance: $0.00 Enter deposit amount for account1: 25.53 adding 25.53 to account1 balance Jane Green balance: $75.53 John Blue balance: $0.00 Enter deposit amount for account2: 123.45 adding 123.45 to account2 balance Jane Green balance: $75.53 John Blue balance: $123.45 Fig. 7.9 Inputting and outputting floating-point numbers with Account objects. (Part 2 of 2.)
7.14 (Removing Duplicated Code in Method main) In the AccountTest class of Fig. 7.9, method
main contains six statements (lines 11–12, 13–14, 26–27, 28–29, 38–39 and 40–41) that each dis-
play an Account object's name and balance. Study these statements and you'll notice that they differ
only in the Account object being manipulated—account1 or account2. In this exercise, you'll define
a new display Account method that contains one copy of that output statement. The method's pa-
rameter will be an Account object and the method will output the object's name and balance. You'll
then replace the six duplicated statements in main with calls to displayAccount, passing as an argu-
ment the specific Account object to output.
Modify class AccountTest of Fig. 7.9 to declare method displayAccount (Fig. 7.18) after the
closing right brace of main and before the closing right brace of class AccountTest. Replace the com-
ment in the method's body with a statement that displays accountToDisplay's name and balance.
I
2
3
4
public static void displayAccount (Account accountToDisplay) {
// place the statement that displays
// accountToDisplay's name and balance here
}
Fig. 7.18 | Method display Account to add to class Account.
Recall that main is a static method, so it can be called without first creating an object of the
class in which main is declared. We also declared method display Account as a static method.
When main needs to call another method in the same class without first creating an object of that
class, the other method also must be declared static.
Once you've completed displayAccount's declaration, modify main to replace the statements
that display each Account's name and balance with calls to displayAccount-each receiving as its
argument the account1 or account2 object, as appropriate. Then, test the updated Account Test
class to ensure that it produces the same output as shown in Fig. 7.9.
Transcribed Image Text:7.14 (Removing Duplicated Code in Method main) In the AccountTest class of Fig. 7.9, method main contains six statements (lines 11–12, 13–14, 26–27, 28–29, 38–39 and 40–41) that each dis- play an Account object's name and balance. Study these statements and you'll notice that they differ only in the Account object being manipulated—account1 or account2. In this exercise, you'll define a new display Account method that contains one copy of that output statement. The method's pa- rameter will be an Account object and the method will output the object's name and balance. You'll then replace the six duplicated statements in main with calls to displayAccount, passing as an argu- ment the specific Account object to output. Modify class AccountTest of Fig. 7.9 to declare method displayAccount (Fig. 7.18) after the closing right brace of main and before the closing right brace of class AccountTest. Replace the com- ment in the method's body with a statement that displays accountToDisplay's name and balance. I 2 3 4 public static void displayAccount (Account accountToDisplay) { // place the statement that displays // accountToDisplay's name and balance here } Fig. 7.18 | Method display Account to add to class Account. Recall that main is a static method, so it can be called without first creating an object of the class in which main is declared. We also declared method display Account as a static method. When main needs to call another method in the same class without first creating an object of that class, the other method also must be declared static. Once you've completed displayAccount's declaration, modify main to replace the statements that display each Account's name and balance with calls to displayAccount-each receiving as its argument the account1 or account2 object, as appropriate. Then, test the updated Account Test class to ensure that it produces the same output as shown in Fig. 7.9.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Returning value from Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education