1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Java Help

Discussion in 'Software Development' started by Rhino24, Apr 14, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. Rhino24

    Rhino24 Thread Starter

    Joined:
    Feb 8, 2008
    Messages:
    41
    Here is my program so far...I have to write an applet that asks the user to enter two floating-point numbers, obtains the two numbers from the user and draws their sum, product, difference and quotient. I not sure what draws means, but I am assuming some kind of shape so I put it into a rectangle.


    import java.awt.Graphics; // program uses class Graphics
    import javax.swing.JApplet; // program uses class JApplet
    import javax.swing.JOptionPane; // program uses class JOptionPane

    public class AdditionApplet extends JApplet
    {
    private double sum; // sum of values entered by user

    // initialize applet by obtaining values from user
    public void init()
    {
    String firstNumber; // first string entered by user
    String secondNumber; // second string entered by user

    double number1; // first number to add
    double number2; // second number to add

    // obtain first number from user
    firstNumber = JOptionPane.showInputDialog(
    "Enter first floating-point value" );

    // obtain second number from user
    secondNumber = JOptionPane.showInputDialog(
    "Enter second floating-point value" );

    // convert numbers from type String to type double
    number1 = Double.parseDouble( firstNumber );
    number2 = Double.parseDouble( secondNumber );

    sum = number1 + number2; // add numbers
    product = number1 * number2; // multiply numbers
    difference = number1 - number2; // subtract numbers
    quotient = number1 / number2; // divide numbers

    } // end method init

    // draw results in a rectangle on applet’s background
    public void paint( Graphics g )
    {
    super.paint( g ); // call superclass version of method paint

    // draw rectangle starting from (15, 10) that is 270
    // pixels wide and 20 pixels tall
    g.drawRect( 15, 10, 270, 20 );

    // draw results as a String at (25, 25)
    g.drawString( "The sum is " + sum, 25, 25 );
    g.drawString( "The product is " + sum, 25, 25 );
    g.drawString( "The difference is " + sum, 25, 25 );
    g.drawString( "The quotient is " + sum, 25, 25 );
    } // end method paint
    } // end class AdditionApplet
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Hi Rhino24,

    I've had a look to your coding and I have some remarks :

    - when writing an applet, it may be useful to know its size.
    So, at the start of the init() part, you can use the getWidth() and getHeight() methods to get the size parameters passed by the browser;

    - the variables product, difference, quotient are not declared;

    - the drawing of the rectangle is not necessary;

    - all your strings are drawn at the same location 25, 25 and are showing the same variable sum, you may draw the second string at location 25, 45, the third at location 25, 65 and so on ...

    - if the user enters whatever data in the input dialog boxes, your applet will crash.
    To prevent the user entering wrong data, you may do something like this :

    Code:
    [SIZE=2]
    ...
    boolean noData = true;
    while (noData) {
       firstNumber = JOptionPane.showInputDialog("Enter first floating-point value" );
       secondNumber = JOptionPane.showInputDialog("Enter second floating-point value" );
       try {
          number1 = Double.parseDouble( firstNumber );
          number2 = Double.parseDouble( secondNumber );
          noData = false;
       } catch(NumberFormatException nfe) {
          new JOptionPane.showMessageDialog(this, "At least one of the entered values is not numeric", "Error", JOptionPane.ERROR_MESSAGE);
       }
    }
    ...
    [/SIZE]
    BTW, your choice of the drawString method is correct.
     
  3. Rhino24

    Rhino24 Thread Starter

    Joined:
    Feb 8, 2008
    Messages:
    41
    I am getting an error system for the Error Message. Do have any idea why. It says it cannot find the symbol.

    import java.awt.Graphics; // program uses class Graphics
    import javax.swing.JApplet; // program uses class JApplet
    import javax.swing.JOptionPane; // program uses class JOptionPane

    public class MathApplet extends JApplet
    {
    private double sum; // sum of values entered by user
    private double product;
    private double difference;
    private double quotient;

    // initialize applet by obtaining values from user
    public void init()
    {
    String firstNumber; // first string entered by user
    String secondNumber; // second string entered by user

    double number1; // first number to add
    double number2; // second number to add

    // obtain first number from user
    firstNumber = JOptionPane.showInputDialog(
    "Enter first floating-point value" );

    // obtain second number from user
    secondNumber = JOptionPane.showInputDialog(
    "Enter second floating-point value" );

    // convert numbers from type String to type double
    number1 = Double.parseDouble( firstNumber );
    number2 = Double.parseDouble( secondNumber );

    boolean noData = true;
    while (noData) {
    firstNumber = JOptionPane.showInputDialog("Enter first floating-point value" );
    secondNumber = JOptionPane.showInputDialog("Enter second floating-point value" );
    try {
    number1 = Double.parseDouble( firstNumber );
    number2 = Double.parseDouble( secondNumber );
    noData = false;
    } catch(NumberFormatException nfe) {
    new JOptionPane.showMessageDialog(this, "At least one of the entered values is not numeric", "Error", JOptionPane.ERROR_MESSAGE); // ERROR MESSAGE HERE
    }
    }


    sum = number1 + number2; // add numbers
    product = number1 * number2; // multiply numbers
    difference = number1 - number2; // subtract numbers
    quotient = number1 / number2; // divide numbers

    } // end method init

    // draw results in a rectangle on applet’s background
    public void paint( Graphics g )
    {
    super.paint( g ); // call superclass version of method paint


    // draw results as a String at (25, 25)
    g.drawString( "The sum is " + sum, 25, 45 );
    g.drawString( "The product is " + sum, 25, 65 );
    g.drawString( "The difference is " + sum, 25, 85 );
    g.drawString( "The quotient is " + sum, 25, 105 );
    } // end method paint
    } // end class MathApplet
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Just replace the word this by null in the statement :
    new JOptionPane.showMessageDialog(this, "At least one of the entered values is not numeric", "Error", JOptionPane.ERROR_MESSAGE);

    Edit : Sorry, the word new must be removed !
     
  5. Rhino24

    Rhino24 Thread Starter

    Joined:
    Feb 8, 2008
    Messages:
    41
    When I compile my program in javac it compiles, but then when I got turn run it it gives me an error message that says Exception in thread "main" java.lang.NoSuchMethodError:main

    I am typing javac MathApplet.java // to compile
    then: java MathApplet // to run
     
  6. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    I've readapted your coding this way :
    Code:
    [SIZE=2]
    import java.awt.Graphics;
    import javax.swing.JApplet;
    import javax.swing.JOptionPane;
    
    public class MathApplet extends JApplet {
    
        private double sum; // sum of values entered by user
        private double product;
        private double difference;
        private double quotient;
    
    // initialize applet by obtaining values from user
        @Override
        public void init() {
            String firstNumber; // first string entered by user
            String secondNumber; // second string entered by user
    
            double number1 = 0.0; // first number to add
            double number2 = 1.0; // second number to add
    
            boolean noData = true;
            while (noData) {
                firstNumber = JOptionPane.showInputDialog("Enter first floating-point value");
                secondNumber = JOptionPane.showInputDialog("Enter second floating-point value");
                try {
                    number1 = Double.parseDouble(firstNumber);
                    number2 = Double.parseDouble(secondNumber);
                    noData=false;
                } catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(this, "At least one of the entered values is not numeric", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
            sum = number1 + number2; // add numbers
            product = number1 * number2; // multiply numbers
            difference = number1 - number2; // subtract numbers
            quotient = number1 / number2; // divide numbers     
    
        } // end method init
    
    // draw results in a rectangle on applet’s background
        @Override
        public void paint(Graphics g) {
            super.paint(g); // call superclass version of method paint
    
    
    // draw results as a String at (25, 25)
            g.drawString("The sum is " + sum, 25, 45);
            g.drawString("The product is " + product, 25, 65);
            g.drawString("The difference is " + difference, 25, 85);
            g.drawString("The quotient is " + quotient, 25, 105);
        } // end method paint
    } // end class MathApplet[/SIZE]
    And it works :
     

    Attached Files:

  7. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    It is not the way to run an applet.
    An applet is launched from a web browser :

    - copy paste the following content into a file and name the file TestApplet.html;
    - have the file stored in the same directory than your MathApplet.class;
    - open the .html file with your favourite Internet browser

    Code:
    [SIZE=2]
    <html>
        <head>
            <title>Test Applet</title>
    	</head>
    	<body>
    	<p align=center><applet code=MathApplet.class alt="(Applet 'MathApplet' should be displayed here.)" width=250 height=150>
    		<font color="#E70000">(Applet "MathApplet" would be displayed here<br>if Java were available.)</font>
    	</applet></p>
    	</body>
    </html>[/SIZE]
     
  8. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/703808

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice