Java Reference
In-Depth Information
to the
BankAccount
class that adds interest at the given rate. For
example, after the statements
BankAccount momsSavings = new BankAccount(1000);
momsSavings.addInterest(10); // 10% interest
the balance in
momsSavings
is $1,100. Also supply a
BankAccountTester
class that prints the actual and expected balance.
125
126
Exercise P3.3. Write a class
SavingsAccount
that is similar to the
BankAccount
class, except that it has an added instance field
interest
. 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. Write a
SavingsAccountTester
class that constructs a savings account with
an initial balance of $1,000 and an interest rate of 10%. Then apply the
addInterest
method and print the resulting balance. Also compute the
expected result by hand and print it.
Exercise P3.4. Implement a class
Employee
. An employee has a name (a
string) and a salary (a
double
). Provide a constructor with two parameters
public Employee(String employeeName, double
currentSalary)
and methods
public String getName()
public double getSalary()
public void raiseSalary(double byPercent)
These methods return the name and salary, and raise the employee's salary
by a certain percentage. Sample usage:
Employee harry = new Employee("Hacker, Harry",
50000);
harry.raiseSalary(10); // Harry gets a 10% raise
Supply an
EmployeeTester
class that tests all methods.