CS 1063  Week 6:  An Introduction to Objects and Classes

Designing and Implementing a Class

Objectives

Assignments



Variable Types

You have seen 3 different types of variables so far:
  1. Instance fields, such as the myBalance variable of the BankAccount class.
  2. Local variables, such as the newBalance variable of the deposit method below.
    public void deposit(double amount) {
       double newBalance = myBalance + amount;
       myBalance = newBalance;
    }
    
  3. Parameter variables, such as the amount variable of the deposit method.

Instance fields belong to an object.  Each object has its own copy of each instance field.

Parameter variables and local variables belong to the method - they die when the method exits.

Forgetting to Initialize

Activity 1:  The SavingsAccount Class

Write a class named SavingsAccount that is similar to the BankAccount class from last week, except that it has an added instance field myInterest.  Supply a constructor that sets both the initial balance and the interest rate.  Supply a method addInterest (with no explicit parameter) that adds interest to the account.

Step 1:  Consider the kind of operations you want to carry out.  These are actions that will be furnished by your class and should be verbs.  This will give you the methods in the class

Step 2:  Determine the instance fields (data) for your class.  Determine what information an object needs to store to do its job.  List beside each method from step 1 the information that is necessary to complete the task.

Step 3:  Consider how you will need to open a new account.  This will give you the constructor(s).

Step 4:  Write the public interface by listing your constructors and methods below.  Be sure and document what each method should do.

Step 5:  Create your project and begin implementing the class using method stubs (methods without the code) and comments describing what the methods will do.

Setup:

Step 6:  Implement the constructors and methods using the comments you wrote in the stubs.  Run your project to check for any errors.

Step 7:  Test your class.  Write the main method of SavingsAccountTest.java.

Write a program that constructs a savings account with an initial balance of $1000.00 and interest rate of 10%.  Then apply the addInterest method six times and print the resulting balance.

Activity 2:  The Student Class

Implement a class named Student.  For the purpose of this exercise, a student has a name, number of quizzes taken, and a total quiz score.  Supply an appropriate constructor and methods getName(), getTotal(), getNumTests(), addQuiz(int score), getAverageScore() and toString().

Step 1:  Consider the kind of operations you want to carry out.  These are actions that will be furnished by your class and should be verbs.  This will give you the methods in the class.

Step 2:  Determine the instance fields (data) for your class.  Determine what information an object needs to store to do its job.  List beside each method from step 1 the information that is necessary to complete the task.

Step 3:  Consider how you will need to initialize a new student.  This will give you the constructor(s).

Step 4:  Write the public interface by listing your constructors and methods below.  Be sure and document what each method should do.

Step 5:  Create your project and implement the class using methods stubs and comments describing what the method will do.

Setup:

Step 6:  Implement the constructors and methods using the comments you wrote in the stubs.  Compile your project to check for any errors.

Step 7:  Test your class.  Write the main method of StudentTest.java.

Instantiate 2 students and add 4 quizzes to each student.  Print the average, total score, and number of quizzes after each addition.  Be sure you test ALL of your methods.