Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java calculation application

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java calculation application

    Hello I'm trying to create an application that will calculate an area based on width and height that a user enters and then use the area to calculate a cost based on a fixed rate. I get past compile errors but when I enter a height and weight it doesn't calculate and show an area. I tried to set a break point and debug but don't get anything useful with that. If someone could help me with this I would be greatful. Here is my code:

    /**

    *Calculates and displays commercial door estimate.
    *

    * @author (Shawn Newton)

    * @version (10/2/2012)

    */

    import java.awt.Image;
    import javax.imageio.ImageIO;
    import java.awt.*;

    import java.awt.event.*;

    import javax.swing.*;

    import java.text.NumberFormat;

    public class CalculatorPanel extends JPanel
    {
    double aRate = 20.00;
    double hRate = 15.00;

    private JButton push;
    private NumberFormat fmt;
    private JLabel heightLabel, widthLabel, areaLabel, costLabel;
    private JTextField height, width, area, cost;

    public CalculatorPanel()
    {
    heightLabel = new JLabel ("Height: \n\t");
    widthLabel = new
    JLabel ( "Width: \n\t");
    areaLabel = new
    JLabel ( "Area: \n\t");
    costLabel = new
    JLabel ( "Cost: \n\t");

    ButtonListener listener = new ButtonListener();
    fmt = NumberFormat.getCurrencyInstance();
    height = new JTextField (5);
    width = new
    JTextField (5);
    area = new JTextField (5);
    cost = new JTextField (5);
    //cost = new JTextField (5);

    push = new JButton("Calculate");
    push.addActionListener( new
    ButtonListener());
    // cost.addActionListener ( new PaymentListener());
    //num.addActionListener ( new PaymentListener());
    //rate.addActionListener ( new PaymentListener());

    add ( push );

    add (heightLabel);

    add (height);

    add (widthLabel);

    add (width);

    add (areaLabel);

    add (area);

    add (costLabel);

    add (cost);

    setPreferredSize ( new

    Dimension( 500, 250 ));

    setBackground ( Color.cyan );
    }

    private class ButtonListener implements ActionListener
    {
    //
    // Updates the monthly payment when the button is pushed.
    //

    public void actionPerformed (ActionEvent event)
    {

    double area = Integer.parseInt(height.toString()) * Integer.parseInt(width.toString());

    }
    }

    public static void main ()

    {

    JFrame frame = new JFrame ("Door Cost Estimation");

    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    CalculatorPanel panel = new CalculatorPanel();

    frame.getContentPane().add(panel);

    frame.pack();

    frame.setVisible(true);

    }

    public static void main (String[] args)

    {
    JFrame frame = new JFrame ( "CalculatorPanel");
    frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE);

    CalculatorPanel panel = new CalculatorPanel ();

    frame.getContentPane().add( panel );
    frame.pack();
    frame.setVisible( true );

    }
    }

    //Math.pow(base, exponent) /// 2^3
    // public String toString()
    //
    // {
    //
    // NumberFormat fmt = NumberFormat.getCurrencyInstance();
    //
    // return fmt.format(monthPayLabel);
    //
    // }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java calculation application

    Where does the code compute and display the value you want to see?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java calculation application

    Quote Originally Posted by Norm View Post
    Where does the code compute and display the value you want to see?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    This is the part of my code that should have calculated and displayed the area:

    double area = Integer.parseInt(height.toString()) * Integer.parseInt(width.toString());

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java calculation application

    This is the part of my code that should have calculated and displayed the area:
    What that statement could do is set the value of the variable named: area. It would NOT display its value anywhere.

    What errors do you get when you execute the program? Please copy the full text of the error messages and paste it here.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java array calculation
    By maple1100 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 1st, 2013, 01:05 PM
  2. Java Calculation Incorrect
    By cow23 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2011, 08:09 AM
  3. Calculation error...very New to Java..please be kind.
    By medicinaluser in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2011, 11:16 PM
  4. how to run any installed application through my java application??
    By sgsamanthjain in forum Java Theory & Questions
    Replies: 1
    Last Post: April 1st, 2011, 08:17 AM
  5. Replies: 0
    Last Post: December 3rd, 2009, 04:43 PM

Tags for this Thread