Pre-Lab Assignment: Lab 9     Name __________________________________________

  1. In class we talked about the bank account class on pages 184 and 185 in the text (with minor changes - see the text for the complete class). Part of it is below:
    import java.text.NumberFormat;
    
    //************************************************************************
    // Represents a bank account; allows deposits, withdrawals, balance 
    // inquiries, etc.
    //************************************************************************
    public class Account
    {
       public static final double RATE = 0.035; // interest rate of 3.5%
    
       ___________________________________________________________; // $10 fee
    
       private long acctNumber;
       private double balance;
       private String name;
    
       //************************************************************************
       // Sets up the account object by defining its owner, account number, and
       // initial balance.  These values are specified in parameters to the
       // constructor.
       //************************************************************************
       public Account(String owner, long account, double initial)
       {
          name = owner;
          acctNumber = account;
          balance = initial;
       }
    
       //**********************************************************************
       // Deposits the specified amount into the account and returns 
       // the new balance.
       //**********************************************************************
       public double deposit (double amount)
       {
          balance += amount;
          return balance;
       }
    
       //*********************************************************************
       // Simple withdrawal -- deducts the specified amount from balance
       //*********************************************************************
       public double withdraw(double amount)
       {
          balance -= amount;
          return balance;
       }
    
       //*********************************************************************
       // Adds interest to the account balance and returns the new balance
       //*********************************************************************
       public double addInterest ()
       {
          balance = balance + balance * RATE;
          return balance;
       }
    
       //****************************************
       // Returns the current balance
       //****************************************
       public double getBalance()
       {
          return balance;
       }
    
       //******************************************************************
       // Deducts the service fee and returns the new balance.
       //******************************************************************
       public double chargeFee()
       {
          // deduct the fee
    
          ____________________________________________________
    
    
          // return the updated balance
    
          ____________________________________________________
       }
    
       //******************************************************************
       // Changes the name on the account to newName.
       //******************************************************************
       public void setName(String newName)
       {
      
          ___________________________________________________ 
       }
    
       //******************************************************************
       // Puts the account data into a string and returns the string
       //******************************************************************
       public String toString ()
       {
          NumberFormat fmt = NumberFormat.getCurrencyInstance();
          return "Account number: " + acctNumber + "\nOwner: "
               + name + "\nCurrent balance: " + fmt.format(balance);
       }
    }
    
    
    1. Add a statement that declares a constant for a service fee of $10. Add this on the blank line after the declaration of the constant RATE. Choose the name of the constant using Java naming conventions.
    2. Fill in the code for method chargeFee, which should deduct a service fee of $10 (use the constant!) from the account, and then return the balance.
    3. In the last space, fill in the body for method setName that takes a string as a parameter and changes the name on the account to be that string.

  2. The Java program below uses the Account class defined in above. Complete the program, using method calls, as indicated by the comments.
    //***************************************************
    // This program uses the Account class to create and
    // manipulate bank accounts for Sally and Joe.
    //
    //***************************************************
    public class ManageAccounts
    {
      //*********************************************
      // Uses the Account class to create and
      // manipulate bank accounts for Sally and Joe.
      //*********************************************
      public static void main(String[] args)
      {
        Account account1, account2;
     
        //create account1 for Sally for $1000
        account1 = new Account("Sally", 12345, 1000);
    
        //create account2 for Joe with $500 and account number 11229
    
        ______________________________________________
    
    
        //deposit $100 to Joe's account
    
        ______________________________________________
    
        //withdraw $50 from Sally's account
    
        ______________________________________________
    
        //charge fees to both accounts
    
        ______________________________________________
    
        ______________________________________________
    
    
        //change name on Joe's account to Joseph
    
        ______________________________________________
    
        //print summary for both accounts
    
        ______________________________________________
    
        ______________________________________________
       
      }
    }
    
  3. Suppose a teacher wants a program to keep track of grades for students and decides to create a student class for her program as follows:

    The following is an incomplete class declaration. Fill in the data declarations and methods as indicated in the comments and the above description of the class.

    //************************************************************************
    // Defines a class that represents a student with a name, score on test 1, and
    // score on test 2. Methods set test grades, compute the average, determine the
    // higest test grade, and determine the letter grade corresponding to the
    // average.
    //*************************************************************************
    public class Student
    {
      //declare instance data
    
      ____________________________________________  // name
                                   
      ____________________________________________  // grade for test #1
    
      ____________________________________________  // grade for test #2
    
    
      //*********************************************************
      // Initializes a student object using the name passed in.
      //*********************************************************
      public Student(String studentName)
      {
          // Assign the instance variable name the value passed in
          // and initialize the two test grades to 0
    
    
    
    
    
    
    
      }
    
      //*************************************************************
      // Assigns the score passed in to the test indicated by the
      // second parameter (whichTest is integer indicating which test
      // the score is for (1 for test #1, 2 for test #2)) 
      //*************************************************************
      public void setTestScore(int score, int whichTest)
      {
         // write an if statement to set the correct test grade 
    
    
    
    
    
    
    
    
    
      }
    
    
      //******************************************
      // Returns the name of a student
      //******************************************
    
    
      _________________________________________________  //fill in header
      {
          // fill in the body of the method (just one line!)
    
          _____________________________________________________;
    
      }
    
    
      //*********************************************************
      // Computes the average of the student's test grades and
      // returns the result.
      //*********************************************************
    
    
      _____________________________________________________  //fill in header
      {
    
         // Compute and return the average (it can be done in one statement -
         // be sure to use floating point arithmetic)
    
    
         _______________________________________________________ ;
      }
    
    
      //***********************************************************
      // Determines the highest test grade and returns the result.
      //***********************************************************
    
      
      ______________________________________________________  // fill in header
      {
     
         // Fill in the blank to return the highest test grade - this
         // can be done in one statement using a method from the Math class.
    
    
         ______________________________________________________________ ;
    
       }
    
    
       //*******************************************************************
       // Finds the letter grade (type char) corresponding to the student's 
       // average assuming an average greater than or equal to 90 is an A, 
       // from 80 up to 90 is a B, and so on.  Below 60 is an F.
       //
       // The letter grade is returned.
       //*******************************************************************
    
       ______________________________________________________ (fill in the header)
    
       {
          double average = averageScore();  // call the method to compute
                                            // the average
          char letter;
    
          // Use a cascading if statement to assign the correct
          // letter grade to the variable letter
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        
    
    
    
          // Return the letter grade
          return letter;
       }
    }
    
  4. The following is part of a class that uses the Student class. Fill in the the blanks and two if statements requested in the commments.
    import java.util.Scanner;
    
    //************************************************************************
    // Uses the Student class to get test grades for two students and compute
    // averages, letter grade, etc. for each.
    //************************************************************************ 
    public class StudentGrades
    {
     
       public static void main (String[] args)
       {
          Scanner scan = new Scanner (System.in);
          String name;
          int score;
    
          System.out.print ("Enter the first student's name: ");
          name = scan.nextLine();            
          Student student1 = new Student (name);
    
          System.out.print ("Enter the second student's name: ");
          name = scan.nextLine();
          Student student2 = new Student (name);
    
          System.out.print ("Enter " + student1.getName() + "'s test 1 grade: ");
          score = scan.nextInt();
    
          // Invoke setTestScore to set test 1 grade for student1
    
          ____________________________________________________________________;
    
    
          System.out.print ("Enter " + student1.getName() + "'s test 2 grade: ");
          score = scan.nextInt();
    
          // Invoke setTestScore to set test 2 grade for student1
    
          ____________________________________________________________________;
    
    
          // Invoke setTestScore to set student2's first test score to 78
          // and second test score to 83.
    
          ____________________________________________________________________;
    
          ____________________________________________________________________;
    
          // Determine each student's test average.
          double average1 = student1.averageScore();
          double average2 = student2.averageScore();
      
          // Write an if ...else... statement that prints the message
          // "Great work!" if student1's average is greater than 84; "Doing
          // fine" if the average is between 70 and 84 (inclusive);
          // "Work harder" otherwise
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
          // Write an if ... else that determines who has the highest
          // average - use the getName method to print the name of the person
          // Be sure to also test for a tie.
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
          }
    }