JAVA, error help: Given an existing class , BankAccount, containing: a construct
ID: 3825640 • Letter: J
Question
JAVA, error help:
Given an existing class , BankAccount, containing:
a constructor accepting a String corresponding to the name of the account holder.
a method , getBalance, that returns a double corresponding to the account balance.
a method withdraw that accepts a double , and deducts the amount from the account balance.
Write a class definition for a subclass, CheckingAccount, that contains:
a boolean instance variable , overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance).
a constructor that accepts a String and a boolean . The String parameter is used in the invocation of the superclass (BankAccount) constructor , while the boolean is used to initialize the overdraft instance variable .
a method , hasOverdraft, that returns a boolean . hasOverdraft returns true if the account supports overdraft.
a method , clearCheck, that accepts a double and returns a boolean . clearCheck will determine if the amount (of the check) can be cashed-- this will be the case if the amount is less than the balance in the account, or if the account allows overdraft. If the check can be cashed, clearCheck returns true , and also calls the withdraw method to update the account balance; otherwise, clearCheck returns false .
My code:
class CheckingAccount extends BankAccount{
boolean overdraft;
CheckingAccount(String accountHolder, boolean overdraftAvailable){
super(accountHolder);
overdraft = overdraftAvailable;
}
// returns whether or not the check can be cashed, and properly withdraws money if it can.
public boolean clearCheck(double amt){
if (hasOverdraft() || amt < super.getBalance()){
super.withdraw(x);
return true;
}
return false;
}
// returns whether or not this account can withdraw more money than it has
public boolean hasOverdraft(){
return overdraft;
}
}
Error: • We think you might want to consider using: else
You almost certainly should be using: private
We think you might want to consider using: this
Explanation / Answer
BankAccount.java
public class BankAccount {
//Declaring instance variables
private String name;
private double balance;
//Parameterized constructor
public BankAccount(String name) {
super();
this.name = name;
}
//Setters and getters
public double getBalance()
{
return balance;
}
public void setBalance(double bal)
{
this.balance=bal;
}
//This method will deduct the amount from the balance
public void withdraw(double amount)
{
balance=balance-amount;
}
//Displaying the Account holder name and balance
@Override
public String toString() {
return name + " is having balance in the Account :" + balance;
}
}
_________________
CheckingAccount.java
public class CheckingAccount extends BankAccount {
// Declaring instance variables
private boolean overdraft;
// parameterized constructor
public CheckingAccount(String name, boolean overdraft) {
super(name);
this.overdraft = overdraft;
}
// This method will check whether there is overdraft or not
public boolean hasOverdraft() {
if (overdraft)
return true;
else
return false;
}
public boolean clearCheck(double amount) {
if (amount < getBalance() || overdraft == true) {
withdraw(amount);
return true;
} else
return false;
}
}
______________________
Test.java
public class Test {
public static void main(String[] args) {
// creating the CheckingAccount class Object by passing account holder
// name and boolean value(Over draft or not)
CheckingAccount ca = new CheckingAccount("Williams", true);
// calling the setter method to set the account balance
ca.setBalance(50000);
// displaying the Account holder name and balance
System.out.println(ca.toString());
// calling the Withdraw method
ca.withdraw(30000);
// displaying the Account holder name and balance
System.out.println(ca.toString());
// calling the clear check method
boolean bool = ca.clearCheck(27000);
if (bool)
System.out.println("Check is Cleared.");
else
System.out.println("Check not Cleared.");
System.out.println(ca.toString());
}
}
______________________
Output:
Williams is having balance in the Account :50000.0
Williams is having balance in the Account :20000.0
Check is Cleared.
Williams is having balance in the Account :-7000.0
_____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.