Question

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 the interfaces. User cannot withdraw more than current balance and cannot deposit negative money. Create at least two objects of BusinessAccount with hard-coded data of your choice in the driver code to test the class and all functions described above.

Use of meaningful names for variables, classes, and methods and meaningful comments.

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

Code for your program is provided below. Code is extensively explained in code comments. Output screenshot as well as screenshot of properly indented code in IDE is attached. If you need any further clarification please feel free to ask in comments. I would really appreciate if you would let me know if the explanation provided is to your satisfaction

##########################################################################

Accountable.java

//interface Accountable
public interface Accountable 
{
         public void withdraw(double amount);   //method declaration
         public double getBalance();   //method declaration
         
}

AccountReceivable.java

//interface declaration
public interface AccountReceivable 
{
        public void deposit(double amount);   //method declaration
}

BusinessAccount.java

//class implements both interfaces
public class BusinessAccount implements Accountable, AccountReceivable {

        //private data member to hold balance 
        private double balance;
        
        //default constructor
        public BusinessAccount() {
                
                balance=0;    //balance is initiaized to zero 
        }
        
        //paramaterized constructor
        public BusinessAccount(double balance) {
                
                this.balance=balance;   //initialize balance with given parameter
        }
        
        @Override    //method to deposit amount
        public void deposit(double amount) {
                
                if(amount>=0)   //if the amount is positive
                        balance+=amount;   //increment the balance
        }

        @Override      //method to withdraw amount
        public void withdraw(double amount) {
                
                //if amount to be withdrawn is smaller than balance then withdraw else dont do anything
                if(amount<=balance)   
                        balance-=amount;   //decrement the balance
        }

        @Override    //method to get balance
        public double getBalance() {
                
                return balance;   //return balance
        }
        
        //to string method override
        public String toString()
        {
                return "Amount: $"+getBalance();   //retrurn balance amount
        }

        public static void main(String[] args) {
                
                BusinessAccount b1=new BusinessAccount(1000);  //object creation
                BusinessAccount b2=new BusinessAccount(7000);   //second object created
                
                System.out.println("Account b1 Initial details: ");
                System.out.println(b1);  //display details of first object
                
                System.out.println("\n$750 Deposit in b1");
                b1.deposit(750);   //deposit money
                System.out.println("Account b1 details: ");
                System.out.println(b1);   //display details
                
                System.out.println("\nTrying to add -500 deposit in b1");
                b1.deposit(-500);   //try to deposit negative amount
                System.out.println("Account b1 details: ");
                System.out.println(b1);   //display details
                
                
                System.out.println("\nAccount b2 initial details: ");
                System.out.println(b2);   //initial balance of second object
                
                System.out.println("\n$300 withdraw from b2");
                b2.withdraw(300);   //withdraw money
                System.out.println("Account b1 details: ");
                System.out.println(b2);    //display details
                
                
                System.out.println("\nTrying to withdraw $50000 withdraw from b2");
                b2.withdraw(50000);   //try to withdraw money more than the balance
                System.out.println("Account b1 details: ");
                System.out.println(b2);   //display details
                
        }
}

###########################################################################

OUTPUT

Account b1 Initial details: Amount: $1000.0 $750 Deposit in b1 Account b1 details: Amount: $1750.0 Trying to add -500 deposit

#######################################################################

SCREENSHOT OF CODE IN IDE

Accountable.java

1 //interface Accountable 2 public interface Accountable 3 4 public void withdraw( double amount); //method declaration 5 pub

AccountReceivable.java

1 //interface declaration 2 public interface AccountReceivable 3 { 4 public void deposit(double amount); 5} 6 //method declar

BusinessAccount.java

1 //class implements both interfaces 2 public class BusinessAccount implements Accountable, AccountReceivable { 3 4 1/privatereturn Amount: $+getBalance(); //retrurn balance amount } public static void main(String[] args) { BusinessAccount b1=new B

Add a comment
Know the answer?
Add Answer to:
Java Code the following interfaces - An interface called Accountable with void withdraw(double) and double getBalance()...
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
  • 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...

  • 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...

  • 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...

  • 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...

  • 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...

  • 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

  • In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a...

    In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a basic employee schema within a company. The Employee class will be an abstract class that will contain methods applicable to all employees. You will then create 2 classes called SoftwareEngineer and ProductManager. Both are different employee types based on occupation. You will create an interface called Developer which will consist of specific methods that apply to only Developers (e.g. SoftwareEngineer class will implement this,...

  • [JAVA] < Instruction> You are given two classes, BankAccount and ManageAccounts ManageAccounts is called a Driver...

    [JAVA] < Instruction> You are given two classes, BankAccount and ManageAccounts ManageAccounts is called a Driver class that uses BankAccount. associate a member to BankAcount that shows the Date and Time that Accounts is created. You may use a class Date. To find API on class Date, search for Date.java. Make sure to include date information to the method toString(). you must follow the instruction and Upload two java files. BankAccount.java and ManageAccounts.java. -------------------------------------------------------------------------- A Bank Account Class File Account.java...

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