Java Student Project         


Account
Ability Level: Intermediate  
Estimated Time: 50 minutes
Objectives:
  • To learn about the this reference 
  • To learn how to use constants
  • To learn how to use prewritten classes and methods that are automatically imported
  • To learn how to use prewritten methods that you import
Materials & Resources:
  • Java Software
  • Textbook
Overview: 1. Create a class called Account1 that stores information about a bank account. It should have a data member to store the account balance named balance, and the account number, named accountNumber. The class should also have a static data member called nextAccountNumber initialized to zero. Create a constructor for the class that takes a balance amount as an argument, and sets the accountNumber variable to the nextAccountNumber plus 1. Save this class in a file named Account1.java.

2. Add a method called display that will display the account number and balance. Create another class called OpenAccount that will create three Account1 objects with balances of 4000, 5000, and 2000. The constructor should assign consecutive account numbers. Save this class in a file called OpenAccount.java.

Add another constructor that will allow Account1 objects to be instantiated without a balance argument, so that the balance is initialized to zero, but the account number receives the next available number. Use the this reference in the constructor to call the constructor that takes the double argument.

3. Add a static variable called interestRate and initialize it to .08 (8% interest). Add a method called calcInterest that will calculate the interest, display the interest amount, add the interest to the balance and display the new balance.

4. Add a method that will report what the last account number assigned was, and also what the current interest rate is. Call this method reportStatus.

Instructions:  See the notes in overview above.
Hints: Some important points to keep in mind in doing this project:
  1. Methods associated with individual objects are instance methods

  2. Mathematical constants are good candidates for receiving final status.  A constant never takes on any other value than the one first assigned.

  3. You can use the keyword final with methods or classes. When used in this manner, final indicates limitations placed on inheritance.

  4. A static variable can be either public or private. If the variable is private, then you must write a method in your program to access it.

  5. You will begin to import optional classes explicitly.

  6. Another useful constant is E, which represents the base of natural logarithms. Its definition is public final static double E = 2.7182818284590452354;.

  7. Because all constants and methods in the Math class are classwide, there is no need to create an instance. You cannot instantiate objects of type Math because the constructor for the Math class is private and your programs cannot access the constructor.

  8. The expression -val means "negative val". The minus sign (-) used in this manner is a unary or single-argument operator.

  9. The import statement ends with a semicolon.

  10. Date is not a reserved word; it is a class you are importing. If you do not want to import the java utility's Date class, you are free to write your own Date class.

  11. The Java wildcard works only with specific packages such as import java.util.*; or import java.lang.*;.

  12. The import statement does not move the entire imported class or package into your program as its name implies. It simply notifies the program that you will be using the data and method names that are part of the imported class or package.

  13. The compiler will interpret an incorrect date, such as March 32, as being April 1.

  14. For information about time, including how leap years and leap seconds are calculated, go to the U.S. Naval Observatory Web site at http://tycho.usno.navy.mil.