Consider this following Diagram
1.Impelements the classes. Assume Taxable is an interface and Product is an abstract class.
2. Assume the Test Product class is the test program, describe what would be the output.
public interface Taxable {
double GasTaxRate = 0.75;
double MotorVehiclTaxRate = 0.5;
public double calcTax();
}
public abstract class Product {
private String description;
private double price;
public Product(){
}
public abstract void display();
public double getPrice(){
return price;
}
public void setPrice(double val){
price = val;
}
public String getDescription(){
return description;
}
public void setDescription(String val){
description = val;
}
}
public class Food extends Product{
private int calories;
public Food(String desc,double p,int c){
setDescription(desc);
setPrice(p);
calories = c;
}
public int getCalory(){
return calories;
}
public void display(){
System.out.println("Food: "+this.getDescription()+
"\nPrice: RM"+this.getPrice()+"\nCalories: "+calories);
}
}
public class MotorVehicle extends Product implements Taxable{
private String MotorType;
public MotorVehicle(String desc,String type,double p){
setDescription(desc);
MotorType = type;
setPrice(p);
}
public String getMotorType(){
return MotorType;
}
public void display(){
System.out.println("MotorVehicle: "+this.getDescription()+
"\nType: "+MotorType+"\nPrice: RM"+this.getPrice());
System.out.println("Tax: RM"+calcTax());
}
public double calcTax(){
return this.getPrice() * MotorVehiclTaxRate / 100;
}
}
public class Gasoline extends Product implements Taxable{
private String type;
public Gasoline(String desc,String type,double p){
setDescription(desc);
this.type = type;
setPrice(p);
}
public String getType(){
return type;
}
public void display(){
System.out.println("Gasoline: "+this.getDescription()+
"\nType: "+type+"\nPrice: RM"+this.getPrice());
System.out.println("Tax: RM"+calcTax());
}
public double calcTax(){
return this.getPrice() * GasTaxRate / 100;
}
}
public class TestProduct {
public static void main(String []args){
Product[] inventory = new Product[4];
inventory[0] = new Food("Meat",14.40,2000);
inventory[1] = new Food("Banana",1.5,500);
inventory[2] = new MotorVehicle("Proton","Car",56000.0);
inventory[3] = new Gasoline("Unleaded","Diesel",2.5);
for (int i = 0; i < inventory.length; i++) {
inventory[i].display();
System.out.println();
}
}
}
1. Implements all the classes. Assume that Convertible is an interface and the Food is the abstract class.
2. The convertible interface converts the following.
a. weight in pound to gram. poundtoGram= 435.59
b.US Currency to Malaysian Ringgit UStoMalaysian= 3.45
3. The food class contains abstract method (named Display Info()) whose purpose to display a brief information about the food such as
Food Description: Celery
Weight in pound : 1 pound
Weight in gram : 453.6 grams
Price: US3.00
Price converted to Malaysian = RM10.35
Food description: Rambutan in Syrup
Canned Food Type: fruit
Expired Date: 12/12/12
Price: US5.00
Price converted to Malaysian = RM17.25
4. Write Application Program to test the classes named FoodApp. The program must apply a polymorphism conceptand the sample output of the application must be shown like above.
Answer
public interface Convertible {
double poundToGram = 435.59;
double UStoMalaysian = 3.45;
}
public abstract class Food {
private String description;
private double price;
public Food(){
}
public Food(String desc,double p){
description = desc;
price = p;
}
public String getDesciption(){
return description;
}
public double getPrice(){
return price;
}
public void setDescription(String val){
description = val;
}
public void setPrice(double val){
price = val;
}
public double calcPriceInRinggit(){
return 0;
}
public abstract String displayInfo();
}
public class CannedFood extends Food implements Convertible{
private String type;
private String expiredDate;
public CannedFood(String desc,double p,String t,String e){
setDescription(desc);
setPrice(p);
type = t;
expiredDate = e;
}
public String displayInfo(){
String str = "";
str = "Food description: "+this.getDesciption()+
"\nCanned Food Type: "+type+"\nExpired date: "
+ expiredDate+"\nPrice: US"+this.getPrice()+
"\nPrice converted to Malaysian = RM"+this.getPrice() * UStoMalaysian;;
return str;
}
}
public class Vegetable extends Food implements Convertible{
private int weight;
public Vegetable(String desc,double p,int w){
setDescription(desc);
setPrice(p);
weight = w;
}
public double calcWeightInGram(){
return weight * poundToGram;
}
public String displayInfo(){
String str = "";
str = "Food description: "+this.getDesciption()+
"\nWeight in pound: "+weight+" pound\nWeight in gram: "
+ calcWeightInGram()+" grams\nPrice: US"+this.getPrice()+
"\nPrice converted to Malaysian = RM"+this.getPrice() * UStoMalaysian;
return str;
}
}
public class FoodApp {
public static void main(String []args){
Food f1 = new Vegetable("Celery", 3.00, 1);
Food f2 = new CannedFood("Rambutan in syrup", 5.00, "friut","12/12/12");
System.out.println(f1.displayInfo());
System.out.println();
System.out.println(f2.displayInfo());
}
}
0 Comments