Applet Help

A

atptour

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;
}

}
 
R

Rhino

I have written some comments within your code. I hope they help ;-)

Rhino

atptour said:
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() );

I'm not sure what the previous line is trying to do but I'm pretty darned
sure that you aren't going about it the right way!

In fact, I'm surprised that this code even compiles because
actionEvent.getActionCommand() should be returning a String and that won't
work will in the Double.parseDouble() method. The String that you get from
getActionCommand() *won't* include the number of hours either;
getActionCommand() returns whatever actionCommand you set for a GUI control
like a JButton.

You need to rethink that line completely. Where is the program supposed to
be getting the number of hours? I'm guessing that it should be getting this
information from the JTextField, inputField, that you set up in the init()
method. If that is the case, you need to read the contents of inputField,
which will also be in String format, and convert that input to a number.
That should be done something like this:

/* Read contents of inputField; discard any leading/trailing spaces. */
String strHours = inputField.getText().trim();

/* Convert input to a number. Assuming that the number of hours could be a
decimal, e.g. 3.5, rather than an integer, and that the user might enter
something that isn't numeric at all, like "foo" (without the quotes), you'll
need code something like this. */
Double DblHours = 0.0; //initialize variable
try {
DblHours = new Double(strHours); //try to convert String to Double
}
catch (NumberFormatException nf_excp) {
System.out.println("The input supplied in the inputField, " + strHours +
", is not numeric. Details: " nf_excp);
return;
}

/* Convert the hours from a Double (wrapper class) to a double (primitive)
which can be used in
calculations. */
double hours = DblHours.doubleValue();

It would also be a good idea to verify that the number of hours supplied by
the user is reasonable. A negative number of hours or a number of hours
greater than 168 for a week (there are 168 hours in a week) or 24 hours for
a day, is not very realistic. If the number of hours is too high or too low,
you could display a message to the user to make them give you a more
realistic value.

That's all I have the time to look at right now but I think that fixing this
logic will go a long way to making your program work.
 
F

fox_fire

Well, since you say the totals are not working correctly, I assume the rest
of it works.

The totals won't display correctly because the variable that keeps track
of the total charges is local; it dies at the end of the calculateTotal
method. What you should do is add a instance field like so:

private double totalReceipts;

right under your declarations of the JLabel and input field. Then modify
_THAT_ variable in the calculateTotal method. I don't know about
getActionCommand either, and I don't know if that works for you or what.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Applet 1
Assigning User input to an array 17
problem in java question 13
Applet not inited 7
newbie: GUI help 1
Run Error Need Help! 0
Java socket programming 1
Running Total in Array 1

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top