Applet Help

Discussion in 'Java' started by atptour, Apr 4, 2004.

  1. atptour

    atptour Guest

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

    }
    atptour, Apr 4, 2004
    #1
    1. Advertising

  2. atptour

    Rhino Guest

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

    Rhino

    "atptour" <> wrote in message
    news:BF_bc.75318$JO3.40539@attbi_s04...
    > 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.
    > 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;
    > }
    >
    > }
    >
    >
    Rhino, Apr 4, 2004
    #2
    1. Advertising

  3. atptour

    fox_fire Guest

    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.
    fox_fire, Apr 5, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Love

    applet vs. canvas in applet

    Rob Love, Jul 7, 2003, in forum: Java
    Replies:
    1
    Views:
    614
    Liu, Chunyen
    Jul 7, 2003
  2. webster
    Replies:
    0
    Views:
    848
    webster
    Jul 20, 2003
  3. Andrew Thompson
    Replies:
    0
    Views:
    409
    Andrew Thompson
    Aug 5, 2003
  4. Blah Blah
    Replies:
    0
    Views:
    1,426
    Blah Blah
    Aug 13, 2003
  5. Jade
    Replies:
    3
    Views:
    926
    Roedy Green
    Aug 14, 2003
Loading...

Share This Page