# Java Help



## Rhino24 (Feb 8, 2008)

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


----------



## Chicon (Jul 29, 2004)

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 :


```
[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.


----------



## Rhino24 (Feb 8, 2008)

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


----------



## Chicon (Jul 29, 2004)

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 !


----------



## Rhino24 (Feb 8, 2008)

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


----------



## Chicon (Jul 29, 2004)

I've readapted your coding this way :

```
[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 :


----------



## Chicon (Jul 29, 2004)

Rhino24 said:


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


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


```
[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]
```


----------

