PreLab 9: Classes

  1. In class we talked about the bank account class described below:
    //*****************************************************
    // Account.java
    //
    // A class representing a bank account; allows deposits,
    // withdrawals, balance inquiries, etc.
    //*****************************************************
    public class Account
    {
      private double balance;
      private String name;
      private long acctNum;
    
      //---------------------------------------------------
      // Constructor -- initializes instance data
      //---------------------------------------------------
      public Account(double initBal, String owner, long number)
      {
        balance = initBal;
        name = owner;
        acctNum = number;
      }
    
      //---------------------------------------------------
      // Simple withdrawal -- deducts amount from balance
      // If balance doesn't cover withdrawal, prints message
      // and does not change balance.
      //---------------------------------------------------
      public void withdraw(double amount)
      {
        if (balance >= amount)
           balance -= amount;
        else
           System.out.println("Insufficient funds");
      }
    
      //---------------------------------------------------
      // Simple deposit -- adds amount to balance.
      //---------------------------------------------------
      public void deposit(double amount)
      {
        balance += amount;
      }
    
      //---------------------------------------------------
      // Returns balance.
      //---------------------------------------------------
      public double getBalance()
      {
        return balance;
      }
    
      //---------------------------------------------------
      // Prints name, account number, and balance
      //---------------------------------------------------
      public void printSummary()
    
      {
        //print name
    
        _____________________________
    
        //print acct number
    
        _____________________________
    
        //print balance
    
        _____________________________
      }
    
      //---------------------------------------------------
      // Deducts $10 service fee if balance is under $1000
      //---------------------------------------------------
      public void chargeFee()
      {
        
    
    
    
    
      }
    
      //---------------------------------------------------
      // Changes the name on the account to newName.
      //---------------------------------------------------
      public void changeName(String newName)
      {
        
    
      }
    
    }
    
    1. Fill in the code for method printSummary, which should print the name, account number, and balance for the account.
    2. Fill in the code for method chargeFee, which should deduct a service fee from the account. If the balance is $1000 or more, no fee should be charged, but if it is less than $1000, $10 should be deducted from the balance.
    3. Modify chargeFee so that instead of returning no value (type void), it returns the new balance. Note that you will have to make changes in two places -- add a return statement and change the return type.
    4. In the last space, fill in the body for method changeName 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 #1. Complete the program as indicated by the comments.
    //*****************************************************
    // ManageAccounts.java
    //
    // A program that uses the Account class to create and
    // manipulate bank accounts for Sally and Joe.
    //*****************************************************
    public class ManageAccounts
    {
      public static void main(String[] args)
      {
        Account account1, account2;
     
        //create account1 for Sally for $1000
        account1 = new Account(1000, "Sally", 12345);
    
        //create account2 for Joe with $500
    
        ______________________________________________
    
    
        //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 method headers as indicated (you don't have to write the bodies of the methods!).
    //*****************************************************
    // Student.java
    //
    // A class representing a student; holds info about 
    // the student's name and test grades.
    //*****************************************************
    public class Student
    {
      //declare instance data
    
      __________________________  // name
                                   
      __________________________  // grade for test #1
    
      __________________________  // grade for test #2
    
    
      //---------------------------------------------------
      // Constructor -- creates a student object whose name 
      // is the value passed in.
      //---------------------------------------------------
      public Student(String studentName)
      {
    
    
      }
    
    
      //---------------------------------------------------
      // inputGrades: Prompts for and reads in grades for 
      // the student's two tests.  No parameters or return value.
      //---------------------------------------------------
      public void inputGrades()
      {
        ... body of inputGrades ...(don't fill in)
      }
    
      //---------------------------------------------------
      // printName: Prints the student's name. No parameters
      // or return value.
      //---------------------------------------------------
    
      ____________________________________  //fill in header
      {
        ... body of printName ...(don't fill in)
      }
    
    
      //---------------------------------------------------
      // getAverage: Computes and return the average of the 
      // student's test grades.  No parameters.
      //---------------------------------------------------
    
      ____________________________________  //fill in header
      {
        ... body of getAverage ... (don't fill in)
      }
    
    }