473,378 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Applet help

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
1 1635

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ...
8
by: DKM | last post by:
Here are the source code files to a Java applet that utilizes LiveConnect to communicate with Javascript, and the HTML file. The thing works both in IE 6.0 and FireFox 1.4. but with some...
0
by: ankur | last post by:
WHEN I RUN THIS WEB APPLICATION ON Tomcat5.5.9 SERVER MY HttpChatApplet sccessfully Loaded from ChatDispatch but running on some another PC HttpChatApplet not loaded my Coad ...
0
by: speedcoder | last post by:
hi all, i'm stumped. my applet used to load images over the network. (it was actually designed by someone else.) yes, the applet used to load each image file independently over the network and...
1
by: brickled | last post by:
hello all, i have used the browser objects (using vb6 and vb.net) in the past to do many things "automatically" on the web. now i'm trying to "interact" with a java applet on a website. I am new...
0
by: shanmukhi | last post by:
Hi All I got a problem in running java programs. i am not able to run java applet while running i got a problem as Loading Java Applet Failed java.lang.NoClassDefFoundError: App (wrong...
0
by: shanmukhi | last post by:
Hi All I got a problem in running java Applet i am not able to run java applet while running i got a problem as Loading Java Applet Failed java.lang.NoClassDefFoundError: App (wrong name:...
4
by: tudyfruity18 | last post by:
I'm suppose to write an applet that contains two buttons Investment calculator and Loan Calculator. When the Investment Calculator button is clicked, a frame appears in a new window for calculating...
1
by: sheephead86 | last post by:
Hi, I'm pretty new to java, and I have a small problem involving drawing a rectangle on a java applet.Firstly this is not a plea for someone to help me with this peice of work, I just need pointing...
2
by: ManidipSengupta | last post by:
Hi, a few (3) questions for the Java experts, and let me know if this is the right forum. It deals with 100% java code (reason for posting here) but manages a Web browser with Javascript. Thanks in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.