Question

In: Computer Science

Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...

Create a class named 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.
  • Java Language/ must be a superclass, subclass and main class

Solutions

Expert Solution

//BankAccount.java


public class BankAccount {

   private String name;
   private double balance;
   public BankAccount(String name) {
       this.name = name;
   }
  
   public void setBalance(double balance) {
       this.balance = balance;
   }

   public double getBalance() {
       return balance;
   }
  
   public void withdraw(double amount)
   {
       this.balance-=amount;
   }
  
  
}

//CheckingAccount.java


public class CheckingAccount extends BankAccount {
  
   private boolean overdraft;

   public CheckingAccount(String name,boolean od) {
       super(name);
       this.overdraft=od;
   }
   public boolean hasOverdraft()
   {
       return this.overdraft;
   }
  
   public boolean clearCheck(double amount) {
       if (amount<this.getBalance())
       {
           this.withdraw(amount);
           return true;
       }
       else if(this.hasOverdraft())
       {
           this.withdraw(amount);
           return true;
       }
       else
           return false;
   }
  

}

//Tester.java (maiin class)


public class Tester {

   public static void main(String[] args) {
       CheckingAccount acc1 = new CheckingAccount("David", true);//overdraft
       CheckingAccount acc2 = new CheckingAccount("Pete", false);//no overdraft
      
       acc1.setBalance(100);
       acc2.setBalance(100);
      
       System.out.println("Balance in acc1 before trying to clear check: "+acc1.getBalance());
       System.out.println("Balance in acc2 before trying to clear check: "+acc2.getBalance());
      
       System.out.println("Trying to cash check more than balance for acc1:"+acc1.clearCheck(101));
       System.out.println("Trying to cash check more than balance for acc2:"+acc2.clearCheck(101));;
      
      
       System.out.println("Balance in acc1 after trying to clear check: "+acc1.getBalance());
       System.out.println("Balance in acc2 after trying to clear check: "+acc2.getBalance());

   }

}


Related Solutions

using C++ Please create the class named BankAccount with the following properties below: // The BankAccount...
using C++ Please create the class named BankAccount with the following properties below: // The BankAccount class sets up a clients account (using name & id), and - keeps track of a user's available balance. - how many transactions (deposits and/or withdrawals) are made. public class BankAccount { private int id; private String name private double balance; private int numTransactions; // ** Please define method definitions for Accessors and Mutators functions below for the class private member variables string getName();...
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .2. Fill in the blanks so that the statement will instantiate a JButton object and assigns it to variable of JButton type:JButton btn= _______ ("OK");3. Fill in the blanks to complete the description of the constructor in the corresponding UML class diagram:+<>______( _____ : ______)
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting). Implement this class (if you wish you...
A constructor, with a String parameter representing the name of the item.
USE JAVA Item classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.Inventory classA constructor which takes a String parameter indicating the item type, an int parameter indicating the initial...
Java program Create a constructor for a class named Signal that will hold digitized acceleration data....
Java program Create a constructor for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp data The constructor...
Create an IceCreamConeException class whose constructor recetves a String that consists of an ice creams cone’s...
Create an IceCreamConeException class whose constructor recetves a String that consists of an ice creams cone’s flavour and the number of scoops Create an IceCreamCone class with two fields - flavor and scoops The IceCreamCone constructor calls two data entry methods — getFlavor() and getScoops() The getScoops() method throws an IceCreamConeException when the scoop quantity exceeds 3 Write a program that establish three TceCreamCone objects and handles the Exception
Create an ApartmentException class whose constructor receives a string that holds a street address, an apartment...
Create an ApartmentException class whose constructor receives a string that holds a street address, an apartment number, a number of bedrooms, and a rent value for an apartment. Upon construction, throw an ApartmentException if any of the following occur: • The apartment number does not consist of 3 digits • The number of bedrooms is less than 1 or more than 4 • The rent is less than $500.00 or over $2500 Write a driver class that demonstrates creating valid...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT