By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,775 Members | 1,399 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,775 IT Pros & Developers. It's quick & easy.

Applet help

P: n/a
I have an applet that I am suppose to write for a java class. However, I am
stuck on one function of the applet. I need to keep a running total of
charges and display them in the status bar. I would appreciate any help
anyone can give. The applet works great as it is currently written, but the
totals do not reflect correctly. Please help.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class A6_8R extends JApplet implements ActionListener {
JLabel promptLabel;
JTextField inputField;

public void init()
{
Container container = getContentPane();
container.setLayout( new FlowLayout() );

promptLabel = new JLabel( "Enter number of hours: ");
inputField = new JTextField( 5 );
inputField.addActionListener( this );
container.add(promptLabel);
container.add(inputField);
}

public void actionPerformed( ActionEvent actionEvent)
{
double currentCharge = 0.00;

double hours = Double.parseDouble(actionEvent.getActionCommand() );
currentCharge = calculateCharges( hours );
showStatus( "Current charge: " + currentCharge + "; " + "Total Receipts: " +

calculateTotal( currentCharge ));
}

public double calculateCharges( double hours )
{
final double minimumCharge = 2.00;
final double maximumCharge = 10.00;
double finalCharge = 0.00;

if ( hours <= 3.0 )
{
finalCharge = minimumCharge;
}
else if ( hours > 3.0 && hours < 24.0)
{
finalCharge = minimumCharge + (0.50 * (hours - 3.0));
}
else if ( hours == 24.0)
{
finalCharge = maximumCharge;
}

return finalCharge;
}

public double calculateTotal ( double currentCharge )
{
double totalReceipts = 0.00;

totalReceipts += currentCharge;
return totalReceipts;
}

}
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"atptour" <at*****@insightbb.com> wrote in message
news:cD_bc.73645$w54.418944@attbi_s01...
I have an applet that I am suppose to write for a java class. However, I am stuck on one function of the applet. I need to keep a running total of
charges and display them in the status bar. I would appreciate any help
anyone can give. The applet works great as it is currently written, but the totals do not reflect correctly. Please help.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class A6_8R extends JApplet implements ActionListener {
JLabel promptLabel;
JTextField inputField;

public void init()
{
Container container = getContentPane();
container.setLayout( new FlowLayout() );

promptLabel = new JLabel( "Enter number of hours: ");
inputField = new JTextField( 5 );
inputField.addActionListener( this );
container.add(promptLabel);
container.add(inputField);
}

public void actionPerformed( ActionEvent actionEvent)
{
double currentCharge = 0.00;

double hours = Double.parseDouble(actionEvent.getActionCommand() );
currentCharge = calculateCharges( hours );
showStatus( "Current charge: " + currentCharge + "; " + "Total Receipts: " +
calculateTotal( currentCharge ));
}

public double calculateCharges( double hours )
{
final double minimumCharge = 2.00;
final double maximumCharge = 10.00;
double finalCharge = 0.00;

if ( hours <= 3.0 )
{
finalCharge = minimumCharge;
}
else if ( hours > 3.0 && hours < 24.0)
{
finalCharge = minimumCharge + (0.50 * (hours - 3.0));
}
else if ( hours == 24.0)
{
finalCharge = maximumCharge;
}

return finalCharge;
}

public double calculateTotal ( double currentCharge )
{
double totalReceipts = 0.00;

totalReceipts += currentCharge;
return totalReceipts;
}

}

Every time you call the calculateTotal method, it resets the totalReceipts
variable to 0. I think what you want to do is move the line:
double totalReceipts = 0.00;

to the class level with your label and text field declarations. That way you
only declare the variable the one time and you never reset it.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.