Question: The following is the last statement of this assignment. Create one object using the default constructor of your sub-class. Call the set methods to set

The following is the last statement of this assignment.

Create one object using the default constructor of your sub-class.

Call the set methods to set all attribute data assoicated to that object.

Create one object using the parameterized constructor of your sub-class.

Put both of your objects in an ArrayList, the loop through the Array List and call display for each object.

These are what I have so far. I am having problem with the display() method, which is supposed to override that of base class. When running the main, this always results in Exception in thread "main" java.util.IllegalFormatConversionException.

VendingMachine.java

public class VendingMachine {

//Declaring instance variables

private String itemName;

private int itemNumber;

private double itemPrice;

//private double insertMoney;

//Default argument constructor

public VendingMachine()

{

this.itemName="Unknown";

this. itemNumber=0;

this. itemPrice=0.00;

//this. insertMoney=0.00;

}

//Parameterized constructor

public VendingMachine(String item, int number, double price)

{

itemName=item;

itemNumber=number;

itemPrice=price;

/*if (money>0) double money

insertMoney=money;

else if(money<0 || money==0)

insertMoney=0;*/

}

//setters and getters

/*

* This method will set itemName of instance variable

*/

public void setItemName(String item)

{

itemName=item;

}

/*

* This method will set itemNumber of instance variable

*/

public void setItemNumber(int number)

{

itemNumber=number;

}

/*

* This method will set itemPrice of instance variable

*/

public void setItemPrice(double price)

{

itemPrice=price;

}

/*

* This method will get the itemName of instance variable

*/

public String getItemName()

{

return itemName;

}

/*

* This method will get the itemNumber of instance variable

*/

public int getItemNumber()

{

return itemNumber;

}

/*

* This method will get the itemPrice of instance variable

*/

public double getItemPrice()

{

return itemPrice;

}

/*

* This method will use the getters to display the instance variables

*/

public String display()

{

return String.format("The vending machine has item %s "

+ "Its code number is %d "

+ "Its price is $%.2f " , getItemName(), getItemNumber(), getItemPrice());

}

}

VendingInterface.java

public interface VendingInterface { boolean isEnough(VendingMachine s); boolean isNotEnough(VendingMachine s); double theChange(VendingMachine s); }

VendingSnacks.java

public class VendingSnacks extends VendingMachine implements VendingInterface{

private double insertMoney;

public VendingSnacks()

{

super();

this. insertMoney=0.00;

}

public VendingSnacks(String item, int number, double price, double money)

{

super(item, number, price);

setInsertMoney(money);

}

public void setInsertMoney(double money)

{

if (money>0.00)

insertMoney=money;

else if(money<0.00 || money==0.00)

insertMoney=0.00;

}

public double getInsertMoney()

{

return insertMoney;

}

public boolean isEnough(VendingMachine s)

{

boolean enough;

if (this.getInsertMoney() >= s.getItemPrice()

&& this.getInsertMoney() > 0.00

&& s.getItemPrice() > 0.00)

enough = true;

else

enough = false;

return enough;

}

public boolean isNotEnough(VendingMachine s)

{

boolean notEnough;

if(this.getInsertMoney() < s.getItemPrice())

notEnough=true;

else

notEnough=false;

return notEnough;

}

public double theChange(VendingMachine s)

{

return this.getInsertMoney()-s.getItemPrice();

}

@Override

public String display()

{

return String.format("The vending machine has item %s "

+ "Its code number is %d "

+ "Its price is $%.2f " ,super.display(),

"The money inserted is $%.2f ", getInsertMoney());

}

}

Demo.java

import java.util.*;

public class Demo {

public static void main(String[] args) {

VendingSnacks healthy1 = new VendingSnacks();

VendingSnacks healthy2 = new VendingSnacks("Soy Chips",1,3.00,5.00);

if (healthy1.isEnough(healthy1))

System.out.printf("The money inserted is enough. The change is %.2f",

healthy1.theChange(healthy1));

else if (healthy1.isNotEnough(healthy1))

System.out.println("The money inserted is not enough.");

else

System.out.println("The item is unknown");

if (healthy2.isEnough(healthy2))

System.out.printf("The money inserted is enough. The change is %.2f",

healthy2.theChange(healthy2));

else if (healthy2.isNotEnough(healthy2))

System.out.println("The money inserted is not enough.");

else

System.out.println("The item is unknown");

ArrayList healthyList = new ArrayList();

healthyList.add(healthy1);

healthyList.add(healthy2);

Iterator itr=healthyList.iterator();

while(itr.hasNext())

{

VendingSnacks each = (VendingSnacks)itr.next();

each.display();

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!