Writing Classes (1) - Chapter 4: Writing Classes (1) Lab...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 4: Writing Classes (1) Lab Exercises Topics Lab Exercises Classes and methods Prelab Exercises (in class) A Bank Account Class (submit) Tracking Grades (submit) A Band Booster Class (extra credit) Chapter 4: Writing Classes (1) 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Prelab Exercises 1. Constructors are special methods included in class definitions. a. What is a constructor used for? b. How do constructors differ from other methods in a class? 2. Both methods and variables in a class are declared as either private or public . Describe the difference between private and public and indicate how a programmer decides which parts of a class should be private and which public. 3. Consider a class that represents a bank account. a. Such a class might store information about the account balance, the name of the account holder, and an account number. What instance variables would you declare to hold this information? Give a type and name for each. b. There are a number of operations that would make sense for a bank account—withdraw money, deposit money, check the balance, and so on. Write a method header with return type, name, and parameter list, for each such operation described below. Don't write the whole method—just the header. They will all be public methods. The first one is done for you as an example. i. Withdraw a given amount from the account. This changes the account balance, but does not return a value. public void withdraw(double amount) ii. Deposit a given amount into the account. This changes the account balance, but does not return a value. iii. Get the balance from the account. This does not change anything in the account; it simply returns the balance. iv. Return a string containing the account information (name, account number, balance). This does not change anything in the account. v. Charge a $10 fee. This changes the account balance but does not return a value. vi. Create a new account given an initial balance, the name of the owner, and the account number. Note that this will be a constructor, and that a constructor does not have a return type. 2 Chapter 4: Writing Classes (1)
Background image of page 2
A Bank Account Class 1. File Account.java contains a partial definition for a class representing a bank account. Save it to your directory and study it to see what methods it contains. Then complete the Account class as described below. Note that you won't be able to test your methods until you write ManageAccounts in question #2. a. Fill in the code for method toString , which should return a string containing the name, account number, and balance for the account. b. Fill in the code for method chargeFee , which should deduct a service fee from the account. c. Modify chargeFee so that instead of returning void, it returns the new balance. Note that you will have to make changes in two places. d. Fill in the code for method
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/31/2010 for the course CPSC 1301 taught by Professor Khan,s during the Spring '08 term at Columbus State University.

Page1 / 9

Writing Classes (1) - Chapter 4: Writing Classes (1) Lab...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online