RU beehive logo ITEC dept promo banner
ITEC 120
2009spring
ibarland
nokie
jmdymacek

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs

hw09
static; nested loops

Due May.01 (Fri) 17:00, under your professor's door.

  1. (3pts) Consider the following class:
    class Foo {
      private int n = 77;
      private static double r = 2.3;
      public static final String MOTTO = "Do unto others, before they do unto you.";
    }
        
    When a program starts running, and the Foo constructor has not been called at all:
    1. How many fields n are in existence?                 
    2. How many fields r are in existence?                 
    3. How many fields MOTTO are in existence?                 
    Now, assume the program has been running a while, and the Foo constructor has been called ten times.
    1. How many fields n are in existence?                 
    2. How many fields r are in existence?                 
    3. How many fields MOTTO are in existence?                 
  2. (1pt) T or F: Constructors are never static methods.
  3. (1pt) T or F: A static field should be initialized in a constructor.
  4. (2pts) The syntax for calling static method is called not object.methodName(args…), but instead it is                                         .
  5. (2pts) A method should be declared static if its answer or effect does not depend on                                         .
  6. (5pts) Run through the following code fragment by hand, to determine what it prints out.
          System.out.println( "Start:" );
          for (int i=0;  i<3;  ++i) {
              for (int j=0;  j<5;  ++j) {
                  System.out.println( "i=" + i + ", j=" + j );
              }
              System.out.println("");  // A blank line.
          }
          System.out.println( "Done." );
          
    (Hint: your should have 17 non-blank lines, plus a few blank lines.)
  7. (5pts) The BankAccount class below uses the static variable totalAccounts in an interesting, useful way.
    Run through the BankAccountTester code below by hand. When Location A is reached, what is the value of the static field BankAccount.totalAccts?                 What is the value of b3.acctNum?                
  8. public class BankAccount {
        private static int totalAccts = 0;
        private int acctNum;
        private double balance;
    
        BankAccount( double initBal ) {
            this.balance = initBal;
            this.acctNum = 100 + totalAccts;
            ++totalAccts;
        }
    
        void deposit( double amt ) {
            this.balance += amt;
            }
    
        // Other methods omitted...
        
    }
    
    public class BankAccountTester {
        public static void main( String[] args ) {
            BankAccount b1 = new BankAccount( 500 );
            BankAccount b2 = new BankAccount( 600 );
            BankAccount b3 = new BankAccount( 700 );
            // Location A.
            
        }
    }
    
  9. (5pts) As a promotional offer, the bank has decided: if a customer happens to deposit an amount equal to their account number, then the bank will add an additional $n, where n is how many customers they have — that is, n is the number of BankAccounts objects that have been constructed.1
    (For example, at Location A, if $100 is deposited into b0, then its resulting balance would be $603: $500 + $100 + the bonus $3.)

    Your task: Modify deposit so that it uses this promotional scheme.

  10. (3pts) The promo offer was a success, and the bank decides they want to run their offer a few days each year. They ask you to add a boolean field “currentlyRunningPromo”, so that by setting this field they can control whether all deposits are made normally, or with the promo offer.
    1. Should currentlyRunningPromo be declared final?                 
    2. Should currentlyRunningPromo be declared static?                 
    3. Why can't currentlyRunningPromo be a local variable inside deposit?                 
  11. (3pts) Finally, re-write deposit so that it makes normal deposits or the promo scheme, depending on the value of BankAccount.currentlyRunningPromo.
  12. (0pts) Self-challenge: Write a method which, given an array of BankAccounts would compute the total balance of all accounts in the array.

1 Note that this scheme scales well: it encourages large deposits, and it only costs the bank much money if they already have lots of customers.
… Though there is one loophole that could be costly: a customer might repeatedly deposit-then-withdraw money over and over again, to repeatedly get the bonus. Hopefully the bank has other safeguards to discourage people from this (e.g. by charging a fee for excessive transactions, or allowing only one promo-bonus per customer per day, etc.). That's not part of this problem, though.      

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs


©2009, Ian Barland, Radford University
Last modified 2009.May.01 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme