Please Simple JAVA I/O exception question Please follow the bolded instruction o
ID: 3727826 • Letter: P
Question
Please Simple JAVA I/O exception question
Please follow the bolded instruction of # of lines required etc TY
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
This version throws an exception in the constructor.
*/
class BankAccount
{
//instance variables
private double balance; //the money in the account
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0;
}
/**
Constructs a bank account with a given balance.
@param aBalance the initial balance
*/
public BankAccount(double aBalance)
{
//-----------Start below here. To do: approximate lines of code = 2
// 1. Throw an IllegalArgumentException if the initial balance is negative
//2. otherwise do the usual thing
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
/**
Deposits money into the bank account.
@param amount the amount to deposit, which cannot be negative
*/
public void deposit(double amount)
{
//-----------Start below here. To do: approximate lines of code = 1
// 3. Throw an IllegalArgumentException if the amount is negative
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
double newBalance = balance + amount;
balance = newBalance;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
//-----------Start below here. To do: approximate lines of code = 1
// 4. Throw an IllegalArgumentException if the amount is too much or if amount is negative
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
balance -= amount ;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
/**
Gives a string representation of the bankaccount,
but in this case the string is just the balance.
@return the balance as a string
*/
public String toString()
{
return "" + balance ;
}
}
Explanation / Answer
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
This version throws an exception in the constructor.
*/
public class BankAccount
{
//instance variables
private double balance; //the money in the account
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0;
}
/**
Constructs a bank account with a given balance.
@param aBalance the initial balance
*/
public BankAccount(double aBalance)
{
//-----------Start below here. To do: approximate lines of code = 2
//1. Throw an IllegalArgumentException if the initial balance is negative
if(aBalance < 0) {
throw new IllegalArgumentException();
}
//2. otherwise do the usual thing
balance = aBalance;
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
/**
Deposits money into the bank account.
@param amount the amount to deposit, which cannot be negative
*/
public void deposit(double amount)
{
//-----------Start below here. To do: approximate lines of code = 1
//3. Throw an IllegalArgumentException if the amount is negative
if(amount < 0) {
throw new IllegalArgumentException();
}
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
double newBalance = balance + amount;
balance = newBalance;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
//-----------Start below here. To do: approximate lines of code = 1
//4. Throw an IllegalArgumentException if the amount is too much or if amount is negative
if(amount < 0 || amount > balance) {
throw new IllegalArgumentException();
}
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
balance -= amount ;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
/**
Gives a string representation of the bankaccount,
but in this case the string is just the balance.
@return the balance as a string
*/
public String toString()
{
return "" + balance ;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.