Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do I override this method in java? Please help?

Hi

I have created an Applet that calculates the shipping costs of items purchased by customers. This is the code: import java.applet.*;

import java.awt.*;

import java.awt.event.ItemListener;

import java.text.DecimalFormat;

public class CandleLine extends Applet implements ItemListener

{

//Declare Variables

double dollars, answer;

int shippingCode;

//Construct Components for applet

Label welcome = new Label("Welcome to CandleLine online");

Label OrderEntryLabel = new Label("Please Enter the total amount of you order");

TextField OrderEntryField = new TextField(7);

Label codeLabel = new Label("Select the appropriate shipping method");

CheckboxGroup codeGroup = new CheckboxGroup();

Checkbox PriorityBox = new Checkbox("Priority Delivery; overnight",false,codeGroup);

Checkbox ExpressBox = new Checkbox("Express Delivery; 2 business days",false,codeGroup);

Checkbox StandardBox = new Checkbox("Standard Delivery; 3-7 business days",false,codeGroup);

Checkbox hiddenBox = new Checkbox("",true,codeGroup);

Label Guarantee = new Label("We will guarantee on-time delivery, or your money back ");

Label outputLabel = new Label("");

public void init()

{

setForeground(Color.black);

setBackground(Color.cyan);

add(welcome);

add(OrderEntryField);

add(OrderEntryLabel);

add(PriorityBox);

PriorityBox.addItemListener(this);

add(ExpressBox);

ExpressBox.addItemListener(this);

add(StandardBox);

StandardBox.addItemListener(this);

add(outputLabel);

add(Guarantee);

}

public void ItemStateChanged(ItemListener choice)

{

try

{

dollars = getSales();

shippingCode = getCode();

answer = getShipping(dollars,shippingCode);

output(answer, dollars);

}

catch(NumberFormatException e)

{

outputLabel.setText("You must enter a dollar amount greater than zero");

hiddenBox.setState(true);

OrderEntryField.setText("");

OrderEntryField.requestFocus();

}

}

public double getSales()

{

double total = Double.parseDouble(OrderEntryField.getText());

if(total <= 0) throw new NumberFormatException();

return total;

}

public int getCode()

{

int code = 0;

if (PriorityBox.getState()) code = 1;

else

if (ExpressBox.getState()) code = 2;

else

if (StandardBox.getState()) code = 3;

return code;

}

public double getShipping(double Total, int code)

{

double shipping = 0.0;

switch(code)

{

case 1:

shipping = 16.95;

break;

case 2:

shipping = 13.95;

break;

case 3:

if(Total>100) shipping = 0.0;

else if(Total<=100) shipping = 7.95;

break;

}

return shipping;

}

public void output(double shipping, double total)

{

DecimalFormat twoDigits = new DecimalFormat("$#,0000.00");

outputLabel.setText("Your shipping on sales of " + twoDigits.format(total) + " is "+twoDigits.format(shipping));

}

}

This is the Error: C:\Users\user\Desktop\Assignment 2\CandleLine.java:17: CandleLine is not abstract and does not override abstract method itemStateChanged(java.awt.event.ItemEvent) in java.awt.event.ItemListener

public class CandleLine extends Applet implements ItemListener

^

1 error

I need to know what code to use to override this method. Could someone please assist me?

Thank You :-)

1 Answer

Relevance
  • 9 years ago
    Favorite Answer

    The error message describes what's wrong in your error :

    "CandleLine is not abstract and does not override abstract method itemStateChanged(java.awt.event.ItemEven… in java.awt.event.ItemListener"

    There's a method called itemStateChanged, which takes as parameter an ItemEvent reference as parameter. In your code, there's a method called itemStateChanged, yes, but it does not take an ItemEvent reference parameter. You need to implement "public void ItemStateChanged(ItemEvent evt)".

    Have a look at what overloading and overwriting is ;-)

Still have questions? Get your answers by asking now.