Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Interactive Applet Troubles"

Status
Not open for further replies.

JavaNovice484

Programmer
Jul 29, 2006
2
0
0
US
Greetings,

I'm trying to create a simple applet...I'm have problems getting my application to perform the correct calculations any assistance would be greatly appreciated.

I'm trying to get the cost per Kilowatt-hour in cents * the kilowatt-hours consumed.

Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class KilowattApplet extends Applet implements ActionListener
{
	//Declare Varaibles
	int cents,hours;

	//Construct Components
	Label welcome = new Label("Welcome to the Appliance Energry Calculator");
	Label costKwhrLabel = new Label("Please enter the cost per kilowatt-hour in cents:");
	TextField costKwhrField = new TextField(5);
	Label hoursPerYearLabel = new Label("Please enter the kilowatt-hours consumed:");
	TextField hoursPerYearField = new TextField(5);
	Button calcButton = new Button("Calculate");
	Label outputLabel = new Label("Click the Calculate button to display the average energy cost.");

public void init()
{
	setForeground(Color.magenta);
	setBackground(Color.darkGray);
	add(welcome);
	add(costKwhrLabel);
	add(costKwhrField);
	add(hoursPerYearLabel);
	add(hoursPerYearField);
	add(calcButton);
	calcButton.addActionListener(this);
	add(outputLabel);
}

public void actionPerformed(ActionEvent e)
{
	//Declare Varaibles
	int average;
	double costKwhr = Double.parseDouble(costKwhrField.getText());
	double cosumedKwhr = Double.parseDouble(hoursPerYearField.getText());
	costKwhr = cents;
	cosumedKwhr = hours
	average = cents * hours;
	outputLabel.setText("The average annual cost to operate this appliance is $" +Math.round(average* 100)/100D);

}

}
 
Code:
// costKwhrField == "34.5"
// costKwhr == 34.5
   double costKwhr = Double.parseDouble(costKwhrField.getText());
    double cosumedKwhr = Double.parseDouble(hoursPerYearField.getText());
// costKwhr = 0.0 -- ooops!
    costKwhr = cents;
// missing semicolon here:
    cosumedKwhr = hours
    average = cents * hours;
the other way round:
Code:
   double costKwhr = Double.parseDouble(costKwhrField.getText());
    double cosumedKwhr = Double.parseDouble(hoursPerYearField.getText());
// need a cast here:
    cents = (int) costKwhr;
    hours = (int) cosumedKwhr;
    average = cents * hours;

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top