CPSC 220 Chapter 3 Answers
From MWCSWiki
Sample questions from Chapter 3 and student-provided answers--waiting on instructor to proof-read and correct if necessary.
Give an example of each:
1. A Method definition containws an access specifier (usually public), a return type, a method name, parameters and the method body.
- public void deposit(double amount)
2. Constructors contain instructions to initialize objects. The constructor name is always the same as the class name.
- CashRegister = new CashRegister();
3. An object uses instance fields to store its state--the data that it needs to execute its methods.
- private double bounds;
4. Each object of a class has its own set of instance fields.
- YourSavings {...} MomsSavings has its own balance.
5. You should declare all instance fields as private.
- Private double payment;
6. Constructors contain instructions to initialize the instance fields of an object.
- public BankAccount
- Balance = 0;
7. Use the return statement to specify the value that a method returns to its caller.
8. Instance fields are initialized to a default value, but you must initialize local variables belonging to a method--they die when the method exits.
- Ur Savings --> Balance
- getInterest(doubleDeposit);
9. Instance fields are initialized to a default value, but you must initialize local variables.
- double new Balance = balance + amount;
10. The implicit parameter of a method is the object on which the method is invoked. The this reference denotes the implicit parameter.
- this.Balance = newBalance;
11. Use of an instance field name in a method denotes the instance field of the implicit parameter.
- public void deposit(double amount)
{
- double newBalance = this.balance amount;
- this.balance = newBalance;
}

