Question

Using Java language.... Polymorphism of Interfaces : Take the Account class from the last chapter and...

Using Java language....

Polymorphism of Interfaces :

Take the Account class from the last chapter and make a java program for your bank. However, there is another type of customer that may not have money in the bank and may not in fact have a back account with this bank at all, but may have a loan here.

Make another class in the application you are making called CarLoan. Carloan's should have a name, amount owed, a rate, and a monthly payment.

Make an Interface class called iMailable to print the monthly statements for all Accounts and all Carloans in the same print run. It should have one method in it called printStatement()

Finally, demonstrate polymorphism with interfaces by having both Account and Carloan implement iMailable and add code to each to have the printStatement() that prints out their data. Make an array of at least 2 iMailable and have at least one Account and at least one CarLoan in it, and use a loop to call printStatement() to show all the statements of the customers accounts and car loans in this array of the interface.

Below is a copy of the account class...

public class Account {
private String name; // instance variable
private double balance; // instance variable

// Account constructor that receives two parameters
public Account(String name, double balance) {
this.name = name; // assign name to instance variable name

// validate that the balance is greater than 0.0; if it isn't
// instance variable balance keeps its default initial value of 0.0
if (balance > 0.0) { // if the balance is valid
this.balance = balance; // assign it to instance variable balance
}
}

// method that deposits only a valid amount to the balance
public void withdrawal(double withdrawalAmount) {
if (withdrawalAmount <= balance) { // if the withdrawal amount is valid  subtract it from the balance
balance = balance - withdrawalAmount;
}
  
else {
System.out.printf("Withdrawal amount exceeded account balance.");
}
}

// method returns the account balance
public double getBalance() {
return balance;
}

// method that sets the name
public void setName(String name) {
this.name = name;
}

// method that returns the name
public String getName() {
return name;
}
}

Homework Answers

Answer #1

JAVA PROGRAM:

Account class (defined in my own term, as it was not in the question):

package test.polymorphsm;

//Account class
//It is generated with assumption of having 3 fields : name,accountBalance and accountNumber
//It has a parametrized constructor
//The class implements the iMailable interface
public class Account implements iMailable{
private String name;
private Double accountBalance;
private Long accountNumber;

//Parameterized constructor: instance can be created through this only
public Account(String name, Double accountBalance, Long accountNumber) {
this.name = name;
this.accountBalance = accountBalance;
this.accountNumber = accountNumber;
}

//implements the method of the interface in its on way
@Override
public void printStatement() {
System.out.println("Given below is the print statement for an ACCOUNT::");
System.out.println(this);//toString method will be used to print the object's data
}

//overrides the toString method for the object
@Override
public String toString() {
return "Account [Name=" + name + ", accountBalance(in $)=" + accountBalance + ", accountNumber=" + accountNumber
+ "]";
}
}

CarLoan class (defined as given in the question):

package test.polymorphsm;

//CarLoan class
//It is generated with having 4 fields : name,amountOwed ,rate and monthlyPayment
//It has a parametrized constructor
//The class implements the iMailable interface
public class CarLoan implements iMailable{

private String Name;
private Double amountOwed;
private Double rate;
private Double monthlyPayment;

//Parameterized constructor: instance can be created through this only
public CarLoan(String name, Double amountOwed, Double rate, Double monthlyPayment) {
super();
Name = name;
this.amountOwed = amountOwed;
this.rate = rate;
this.monthlyPayment = monthlyPayment;
}


//implements the method of the interface in its on way
@Override
public void printStatement() {
System.out.println("Given below is the print statement for a CARLOAN::");
System.out.println(this);//toString method will be used to print the object's data
}


//overrides the toString method for the object
@Override
public String toString() {
return "CarLoan [Name=" + Name + ", amountOwed (in $)=" + amountOwed + ", rate (in %)=" + rate + ", monthlyPayment(in $)="
+ monthlyPayment + "]";
}


}

iMailable interface

package test.polymorphsm;

//This is an interface: it cannot be instantiated
//but other classes can implement it otr other interface can extend it
public interface iMailable {

void printStatement(); //method is declared only: implementing classes has to define it within themselves

}

TestPolymorphism class (to test the polymorphism):

package test.polymorphsm;

public class TestPolymorphism {

public static void main(String[] args){

//2 Account instances are created and assigned to iMailable type
//as Account class implements iMailable interface
iMailable acc1 = new Account("Thomas Clark",1500.50,12054568L);
iMailable acc2 = new Account("Bobby Simpson",579.86,12050303L);

//2 CarLoan instances are created and assigned to iMailable type
//as CarLoan class implements iMailable interface
iMailable cl1 = new CarLoan("Harry George",10000.00,3.8,200.00);
iMailable cl2 = new CarLoan("Larry Gomes",5500.00,3.05,115.40);

//All of the above are added in a iMailable array
iMailable[] printArr = {acc1,cl1,cl2,acc2};

//traverse each element of the array and print the corresponding types's printStatement.
//Depending on whether it is Account instance or CarLoan instance, the printStatement() implementation
//of corresponding type will be called during runtime
for(iMailable recToPrint: printArr){
recToPrint.printStatement();
}
}

}

Output:

Given below is the print statement for an ACCOUNT::
Account [Name=Thomas Clark, accountBalance(in $)=1500.5, accountNumber=12054568]
Given below is the print statement for a CARLOAN::
CarLoan [Name=Harry George, amountOwed (in $)=10000.0, rate (in %)=3.8, monthlyPayment(in $)=200.0]
Given below is the print statement for a CARLOAN::
CarLoan [Name=Larry Gomes, amountOwed (in $)=5500.0, rate (in %)=3.05, monthlyPayment(in $)=115.4]
Given below is the print statement for an ACCOUNT::
Account [Name=Bobby Simpson, accountBalance(in $)=579.86, accountNumber=12050303]

Explanation:

Polymmorphism is defined as object's ability to take various firms. Here also, iMailable can be of type Account or of type CarLoan , and accordingly it has different implementation for its printStatement() method, which can be seen as we iterate over array elements. Whenever Account object is found , it' s defined printtatement() is printed, ans so as for CarLoan object.

Know the answer?
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
Similar Questions
Write a pseudocode for the following java programs: public class Item {    private String name;...
Write a pseudocode for the following java programs: public class Item {    private String name;    private double cost;    public Item(String name, double cost) {        this.name = name;        this.cost = cost;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getCost() {        return cost;    }    public void setCost(double cost) {...
IN JAVA PLEASE Design and implement an ADT CreditCard that represents a credit card. The data...
IN JAVA PLEASE Design and implement an ADT CreditCard that represents a credit card. The data of the ADT should include Java variables for the customer name, the account number, the next due date, the reward points, and the account balance. The initialization operation should set the data to client-supplied values. Include operations for a credit card charge, a cash advance, a payment, the addition of interest to the balance, and the display of the statistics of the account. Be...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also define...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial capacity of the ArrayDeque. * * DO NOT MODIFY THIS VARIABLE. */ public static final int INITIAL_CAPACITY = 11; // Do not add new instance variables or modify existing ones. private T[] backingArray; private int front; private int size; Q1: write a method that constructs a new arrayDeque called "public ArrayDeque()" Q2: write a method called "public void addFirst(T data)" that adds an element...
in java Write a class Battery that models a rechargeable battery. A battery has a constructor...
in java Write a class Battery that models a rechargeable battery. A battery has a constructor public Battery(double capacity) where capacity is a value measured in milliampere hours. A typical AA battery has a capacity of 2000 to 3000 mAh. The method public void drain(double amount) drains the capacity of the battery by the given amount. The method public void charge() charges the battery to its original capacity. The method public double getRemainingCapacity() gets the remaining capacity of the battery....
java 1) Create a mutator method called setPosition that accepts one integer parameter. Update the position...
java 1) Create a mutator method called setPosition that accepts one integer parameter. Update the position variable by adding the position to the parameter variable. 2)debug the code public class food { public static void main(String[] args) { Fruits apple = new Fruits(20); // Write the statement to call the method that will increase the instance variable position by 6. Fruits.setPosition(6); apple.getPosition(); } }
Casting class objects 1.2 Compile and execute the code listed below as it is written. Run...
Casting class objects 1.2 Compile and execute the code listed below as it is written. Run it a second time after uncommenting the line obj.speak();. public class AnimalRunner {    public static void main(String[] args)    {       Dog d1 = new Dog("Fred");       d1.speak();       Object obj = new Dog("Connie");       // obj.speak();    } } The uncommented line causes a compile error because obj is an Object reference variable and class Object doesn’t contain a speak() method. This...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Create a field with the boolean value of goodBoi. In the constructor, set your good boi...
Create a field with the boolean value of goodBoi. In the constructor, set your good boi to being false, as all dogs has deserve being called a good boiii. Make a method makeGoodBoi, which sets the good boi value to being true. Make a method isGoodBoi which returns the value the good boi field (hint: it returns a boolean value). Now we need a method called whosAGoodBoi. If the doggo is indeed a good boi, then print: "[doggo] is such...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT