Question

Code the following interfaces - An interface called Accountable with void withdraw(double) and double getPayment() methods,...

Code the following interfaces - An interface called Accountable with void withdraw(double) and double getPayment() methods, and an interface called AccountReceivable with a void deposit(double) method. Save these two interfaces in separate Java files. Then code a class, BusinessAccount, that implements the two interfaces defined above. Define and code the necessary instance variables and constructors. Override toString() so it will return the amount of a business account. Apply the particular operations, such as withdraw and deposit, required in implementing the interfaces. Create at least two objects of BusinessAccount with hard-coded data of your choice in the driver code to test the class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question. Please do rate the answer if it helped. Thank you.


AccountReceivable.java
------
public interface AccountReceivable {
   public void deposit(double amt);
}

Accountable.java
-----
public interface Accountable{
   public void withdraw(double amt);
   public double getPayment();
}

BusinessAccount.java
-----

public class BusinessAccount implements Accountable, AccountReceivable
{
   private String name;
   private double balance;
  
   public BusinessAccount(String n, double bal){
       name = n;
       balance = bal;
   }
  
   public void withdraw(double amt){
       if(amt <= balance)
           balance -= amt;
   }
  
   public void deposit(double amt){
       if(amt > 0)
           balance += amt;
   }
  
   public double getPayment(){
       return balance;
   }
  
   public String toString(){
       return "Name: " + name + ", Balance: " + balance ;
   }
  
   public static void main(String[] args){
       BusinessAccount a1 = new BusinessAccount("John", 5000);
       BusinessAccount a2 = new BusinessAccount("Alice", 3000);
       a1.deposit(200);
       a2.withdraw(300);
       System.out.println("Balance in a1 = " + a1.getPayment());
       System.out.println(a1.toString());
       System.out.println(a2.toString());
   }
}

output
----
Balance in a1 = 5200.0
Name: John, Balance: 5200.0
Name: Alice, Balance: 2700.0


Add a comment
Know the answer?
Add Answer to:
Code the following interfaces - An interface called Accountable with void withdraw(double) and double getPayment() methods,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Code the following interfaces - An interface called Accountable with void withdraw(double) and double getBalance()...

    Java Code the following interfaces - An interface called Accountable with void withdraw(double) and double getBalance() methods, and an interface called AccountReceivable with a void deposit(double) method. Save these two interfaces in separate Java files. Then code a class, BusinessAccount, that implements the two interfaces defined above. Define and code the necessary instance variables and constructors. Override toString() so it will return the amount of a business account. Apply the particular operations, such as withdraw and deposit, required in implementing...

  • Java program. Code the following interfaces, implement them, and use the interface as a parameter, respectively:...

    Java program. Code the following interfaces, implement them, and use the interface as a parameter, respectively: a. An interface called Printable with a void print() method. b. An interface called EmployeeType with FACULTY and CLASSIFIED integer data. The value of FACULTY is 1 and CLASSIFIED is 2. a. A class called Employee to implement the two interfaces. Its constructor will initialize three instance data as name, employeeType, and salary. Implementation of method print() will display name, employeeType, and salary; salary...

  • Write a Java interface called Salience that includes two methods: setSalience and getSalience. The interface should...

    Write a Java interface called Salience that includes two methods: setSalience and getSalience. The interface should define a way to establish numeric salience among a set of objects. Design and implement a class called Task that represents a task (such as on a to-do list) that implements the Salience interface. Create a driver class to exercise some Task objects.

  • Code using Java What is an interface? Complete the following code: interface IExample{ public void print();...

    Code using Java What is an interface? Complete the following code: interface IExample{ public void print(); } class Example1 implements IExample{ . . . } // Driver class public class Questions { public static void main(String[] args) { . . . } } To have this message when you execute it: Implementation of an interface

  • Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An...

    Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An abstract method    static double average(Measurable[] objects) { // A static method    double sum = 0;    for (Measurable obj : objects) {    sum = sum + obj.getMeasure();    }    if (objects.length > 0) { return sum / objects.length; }    else { return 0; }    } } Write a class, called Person, that has two instance variables, name (as...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • in java • Create an interface called Person. In this interface create a method called printData()...

    in java • Create an interface called Person. In this interface create a method called printData() • Implement interface Person by classes Student, Teacher, Admin. You need to think the relevant data attributes for each class. Define atleast 4 attributes for each class. • In each class override a method toString() and create a string which holds all data • In the method printData in each class print the data using toString() method. Design the UML and write a code...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Consider the following Java 8 code, and select true statements: public interface Functionlnterfac...

    Consider the following Java 8 code, and select true statements: public interface Functionlnterface1 void abstFun1 (int x) public interface Functioninterface2( void abstFun2(int x); public class Main implements Functioninterface2( @Override public void abstFun2(int x) System.out.printin(3'x); public static void main(Stringl args) ( Functioninterface1 fobj (int x)->System.out.printin(2"x) fobj.abstFun1(5); Main me - new Main; me.abstFun2(5); Functioninterface2 fobj2-(int x)->System.out.printin(4%; fobj2.abstFun2(5) 2 in main will not run because abstFun2 was previoudly implemented. O The fobj instance of Functionlnterface1 is created using the lambda at runtime without...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT