Question
2. A method that returns a special error code is usually better accomplished by throwing an exceptionn instead. The following class maintains an account balance.n
2. A method that returns a special error code is usually better accomplished by throwing an exceptionn
instead. The following class maintains an account balance.n
class Accountn
{n
private double balance;n
public Account()n
{n
balance = 0;n
}n
public Account( double initialDeposit)n
{n
balance = initialDeposit;n
}n
public double getBalance()n
{n
return balance;n
}n
// returns new balance or -1 if errorn
public double deposit( double amount)n
{n
if (amount > 0)n
balance += amount;n
elsen
return -1;// Code indicating errorn
return balance;n
}n
// returns new balance or -1 if invalid amountn
public double withdraw(double amount)n
{n
if ((amount > balance) || (amount < 0))n
return -1;n
elsen
balance -= amount;n
return balance;n
}n
}n
Rewrite the class so that it throws appropriate exceptions instead of returning −1 as an errorn
code. Write test code that attempts to withdraw and deposit invalid amounts and catches then
exceptions that are thrown.
Three classes are given belowwith names Date, Employee and SalariedEmployee. Analyze the above code and fulfil the following requirements. Define a class called Administrator, which is a derived class of the class SalariedEmployee. You are to supply the following additional instance variables and methods: a) An instance variable of type String that contains the administrator's title (such as "Director" or "Vice President"). b) An instance variable of type String that contains the administrator's area of responsibility (such as "Production", "Accounting" , or "Personnel" ). c) An instance variable of type String that contains the name of this administrator's immediate supervisor. d) Suitable constructors, and suitable accessor and mutator methods.
Step by Step Solution
3.56 Rating (163 Votes )
There are 3 Steps involved in it
Step: 1
Employeejava public class Employee private String name private Date hireDate public Employee nameNo name hireDatenew DateJanuary11000 public EmployeeString theNameDate theDate iftheNamenull theDatenul...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started