Rank the following Account class identifiers by thier scope, from broadest (can
ID: 641555 • Letter: R
Question
Rank the following Account class identifiers by thier scope, from broadest (can be seen by the most lines of code) to the narrowest (can be seen by the fewest).
1. Account class (definded in line 3 of Listing 1).
2. totalCredits (defind in line 8 of listing 1).
3. amount (defined in line 37 fo listing 1).
4. count(definded in line 79 of listing 1).
Listing 1 below:
1
2
3 public class Account
4 {
5 private double balance;
6 private int credits;
7 private int debits;
8 private double totalCredits;
9 private double totalDebits;
10 public static final double CURRENT_RATE = 0.045;
11
12 public Account(double initialBalance)
13 {
14 if( initialBalance >= 0.0 )
15 balance = initialBalance;
16 else
17 showMessage("? Incorrect initial balance: $%.2f ", initialBalance);
18 }
19
20 public Account(Account fromAccount)
21 {
22 balance = fromAccount.getBalance();
23 }
24
25 public void credit(double amount)
26 {
27 if( amount > 0.0 )
28 {
29 balance += amount;
30 totalCredits += amount;
31 credits++;
32 }
33 else
34 showMessage("? Invalid amount for a Credit: $%.2f ", amount);
35 }
36
37 public void debit(double amount)
38 {
39 if( amount > 0.0 )
40 if( balance >= amount )
41 {
42 balance = balance - amount;
43 totalDebits = totalDebits + amount;
44 debits++;
45 }
46 else
47 showMessage("? Debit of $%.2f exceeds balance of $%.2f", amount,
48 balance);
49 else
50 showMessage("? Invalid amount for a Debit: $%.2f ", amount);
51 }
52
CSC-240 Java Programming
Explanation / Answer
Its in the order
1 Account (as it is a class which has larger scope)
2 totalCredits (as it is a member it can be seen any where with in the class it has equal scope as of Account with in a class)
3 Amount (is a method level variable accessible with in the method debit)
4 count (it is visible with in the for loop)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.