0% found this document useful (0 votes)
51 views

Tutorial 10 Polymorphism

The document discusses polymorphism concepts in Java including abstract classes, interfaces, and inheritance. It provides code examples of: 1. An abstract DiscountPolicy class with derived BulkDiscount and OtherDiscount classes implementing a computeDiscount method differently. A combineDiscount class returns the maximum discount from two policies. 2. An Interest interface implemented by SavingAccount and FixedAccount classes to compute monthly interest differently based on account type. 3. A Person class that implements Comparable to sort an array of Person objects by name in ascending order.

Uploaded by

shu min
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Tutorial 10 Polymorphism

The document discusses polymorphism concepts in Java including abstract classes, interfaces, and inheritance. It provides code examples of: 1. An abstract DiscountPolicy class with derived BulkDiscount and OtherDiscount classes implementing a computeDiscount method differently. A combineDiscount class returns the maximum discount from two policies. 2. An Interest interface implemented by SavingAccount and FixedAccount classes to compute monthly interest differently based on account type. 3. A Person class that implements Comparable to sort an array of Person objects by name in ascending order.

Uploaded by

shu min
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Tutorial 10 Polymorphism

1. Create an abstract class DiscountPolicy. It has a single abstract method


computeDiscount that return the discount for the purchase of a given number of item.
The method has two parameters count and itemCost. Derived a class BulkDiscount
from DiscountPolicy. It has a constructor that has two parameters minimum and
discount rate. It has a method computeDiscount that compute the discount base on
discount rate if the number of item more than minimum. Otherwise, no discount given.
Derived a class OtherDiscount that compute the discount base on the table below
N (number of Item) 1 – 2 3 – 5 6 – 8 >8
Discount 0 10% 20% 30%

Derived a class combineDiscount from DiscountPolicy. It has two parameters of type


DiscountPolicy It has a method computeDiscount that return the maximum value
returned by the computeDiscount for the two discount policies. Create a Tester class
to test the program.
package TutoQ1;

public abstract class DiscountPolicy {

protected double discount;

public abstract double computeDiscount(int count, double itemCost);


}

package TutoQ1;

public class BulkDiscount extends DiscountPolicy {

private int minimum;


private double discountRate;

public BulkDiscount(int minimum, double discountRate) {


this.minimum = minimum;
this.discountRate = discountRate;
}

public double computeDiscount(int count, double itemCost) {


if (count > minimum) {
discount = discountRate * count * itemCost;
} else {
discount = 0;
}
return discount;
}
}

package TutoQ1;

public class OtherDiscount extends DiscountPolicy {

public double computeDiscount(int count, double itemCost) {

if (count <= 2) {
discount = 0;
} else if (count <= 5) {
discount = count * itemCost * 0.1;
} else if (count <= 8) {
discount = count * itemCost * 0.2;
} else {
discount = count * itemCost * 0.3;
}
return discount;
}
}

package TutoQ1;

public class combineDiscount extends DiscountPolicy {

public double computeDiscount(DiscountPolicy d1, DiscountPolicy d2) {


if (d1.discount > d2.discount) {
return d1.discount;
} else if (d2.discount > d1.discount) {
return d2.discount;
} else {
return 0;
}
}

public double computeDiscount(int count, double itemCost) {


return 0;
}
}

package TutoQ1;

public class Tester {

public static void main(String[] args) {


OtherDiscount a = new OtherDiscount();
discount(a);
BulkDiscount b = new BulkDiscount(4, 1.4);
discount(b);
combineDiscount c = new combineDiscount();
System.out.println("The maximum dsicount is RM " + c.computeDiscount(a, b));
}

public static void discount(DiscountPolicy v) {


System.out.println("RM: " + v.computeDiscount(9, 6.7));
}
}
2. Create an interface Interest that has a single method computeInterest that return
the monthly interest based on the balance in the account. Create the SavingAccount
that implement the interface, the class has an instance variable called balance. Define
the method to compute interest. The interest rate for saving account is 0.5% per year.
Create the FixedAccount that implement the interface. The class has an instance
variable called balance. Define the method to compute interest. The interest rate for
saving account is 3% per year. Create a Tester class to test the program.
package TutoQ2;

public interface Interest {

public double computeInterest();


}

package TutoQ2;

public class SavingAcc implements Interest {

private double balance;

public SavingAcc(double balance) {


this.balance = balance;
}

public double computeInterest() {


return balance * (0.5 / 100);
}
}

package TutoQ2;

public class FixedAcc implements Interest {

private double balance;

public FixedAcc(double balance) {


this.balance = balance;
}
public double computeInterest() {
return balance * ((double) 3 / 100);
}
}

package TutoQ2;

public class Tester {

public static void main(String[] args) {


SavingAcc a = new SavingAcc(798);
FixedAcc b = new FixedAcc(244);
displayInterest(a);
displayInterest(b);
}

public static void displayInterest(Interest a) {


System.out.println(String.format("The interest is RM%.2f.",
a.computeInterest()));
}
}
3. Create a class Person that implements the comparable interface. The class has an
instance variable name. The class has the constructor that initializes the name. The
class also has the accessor method and a display method to display the name. Create
an array for multiple Person objects. Sort the person in ascending order. Create a
Tester class to test the program.
package TutoQ3;

public class Person implements Comparable {

private String name;

public Person(String name) {


this.name = name;
}

public String getName() {


return name;
}

@Override
public String toString() {
return name;
}
public int compareTo(Object other) {
return (name.compareToIgnoreCase(((Person) (other)).getName()));
}
}
package TutoQ3;

import java.util.Scanner;

public class Tester {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("How many people? -> ");
int num = sc.nextInt();
sc.nextLine();
Person obj[] = new Person[num];

for (int i = 0; i < num; i++) {


System.out.print("Name of Person" + (i + 1) + ": ");
String name = sc.nextLine();
//Constructor of object
obj[i] = new Person(name);
}

//Bubble sort
String hold;
for (int i = 1; i < obj.length; i++) {
for (int j = 0; j < obj.length - 1; j++) {
if (obj[j].compareTo(obj[j + 1]) > 0) {
hold = obj[j].getName();
obj[j] = new Person(obj[j + 1].getName());
obj[j + 1] = new Person(hold);
}
}
}

System.out.println("Name in ascending order:");


for (int i = 0; i < num; i++) {
System.out.println(obj[i].toString());
}
}
}

You might also like