Link to home
Start Free TrialLog in
Avatar of bakerule22
bakerule22

asked on

Java How to Program Exercise Question

I been trying figure out this question but i can't figure it out I need help asap!!

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 (Multiplication), difference and quotient (division).

So far I got
//Fig. 23.9: AdditionApplet.java
// Applet that adds two doubl values entered via input dialogs. 
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 doulb sum; // sum of values entered by user
	 
	 // initialize applet by obtaining values from user
	 public void init()
	 {
		 // obtain first number from user 
		 String firstNumber = JOptionPane.showInputDialog(
			 "Enter first floating-point value" ); 
		 
		 // obtain second number from user 
		 String secondNumber = JOptionPane.showInputDialog(
			 "Enter second floating-point value" ); 
			 
		 // convert numbers from type String to type double
		 double number1 = Double.parseDouble( firstNumber ); 
		 double number2 = Double.parseDouble( secondNumber ); 
		 
		 sum = number1 + number2; // add 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 pain 
		 
		 // draw rectangle starting from (11, 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 ); 
	 } // end method paint 
} // end class AdditionApplet

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image

I think you should just change doulb to double and jOptionPane to JOptionPane
And it makes a sum for me just after these two corrections


import javax.swing.jOptionPane; -->import javax.swing.JOptionPane;  



private doulb sum;  ---> private double sum;



Avatar of bakerule22
bakerule22

ASKER

ok
// Fig. 23.9: AdditionApplet.java
// Applet that adds two double values entered via input dialogs. 
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()
	 {
		 // obtain first number from user 
		 String firstNumber = JOptionPane.showInputDialog(
			 "Enter first floating-point value" ); 
		 
		 // obtain second number from user 
		 String secondNumber = JOptionPane.showInputDialog(
			 "Enter second floating-point value" ); 
			 
		 // convert numbers from type String to type double
		 double number1 = Double.parseDouble( firstNumber ); 
		 double number2 = Double.parseDouble( secondNumber ); 
		 
		 sum = number1 + number2; // add 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 pain 
		 
		 // draw rectangle starting from (11, 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 ); 
	 } // end method paint 
} // end class AdditionApplet

Open in new window


I fix it

Replace
sum = number1 + number2;

also add declaration
private double result;
immediately after the sum declasruion

and also change sum to result in the paint(g) method

and you'll get any operrtaion nit just sum


          String operator = JOptionPane.showInputDialog(
                 "Enter operator (+,-,*,/):");

            if(operator.equals("+")){
                  result = number1 + number2;
            }   else if

            (operator.equals("-")){
                      result = number1 - number2;
            }   else if
             (operator.equals("*")){
                       result = number1 * number2;
             }  else
              if(operator.equals("/")){
                       result = number1 / number2;
              }

Open in new window

So your code in ID:36936817 should be working for addition as it worked for me

Look into my previous posting if you want to add other operations
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok thanks
I just want check one more time if this what look likes

// Fig. 23.9: AdditionApplet.java
// Applet that adds two double values entered via input dialogs. 
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()
	 {
		 // obtain first number from user 
		 String firstNumber = JOptionPane.showInputDialog(
			 "Enter first floating-point value" ); 
		 
		 // obtain second number from user 
		 String secondNumber = JOptionPane.showInputDialog(
			 "Enter second floating-point value" ); 
			 
		 // convert numbers from type String to type double
		 double number1 = Double.parseDouble( firstNumber ); 
		 double number2 = Double.parseDouble( secondNumber ); 
		 
		 String operator = JOptionPane.showInputDialog(
			 "Enter operator (+,-,*,/):");

             if(operator.equals("+")){
                  result = number1 + number2;
            }   else if

            (operator.equals("-")){
                      result = number1 - number2;
            }   else if
             (operator.equals("*")){
                       result = number1 * number2;
             }  else
              if(operator.equals("/")){
                       result = number1 / number2;
              }
	 } // 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 pain 
		 
		 // draw rectangle starting from (11, 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 ); 
	 } // end method paint 
} // end class AdditionApplet

Open in new window

You should add (or even replace)
 private double sum;

with

private double result;

and replace:
g.drawString( "The sum is " + sum, 25, 25 );

with

g.drawString( "The result  is " + result, 25, 25 );


The rest I think should be OK.

This works:

// Fig. 23.9: AdditionApplet.java
// Applet that adds two double values entered via input dialogs.
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 result; // sum of values entered by user

	 // initialize applet by obtaining values from user
	 public void init()
	 {
		 // obtain first number from user
		 String firstNumber = JOptionPane.showInputDialog(
			 "Enter first floating-point value" );

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

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

		 String operator = JOptionPane.showInputDialog(
			 "Enter operator (+,-,*,/):");

             if(operator.equals("+")){
                  result = number1 + number2;
            }   else if

            (operator.equals("-")){
                      result = number1 - number2;
            }   else if
             (operator.equals("*")){
                       result = number1 * number2;
             }  else
              if(operator.equals("/")){
                       result = number1 / number2;
              }
	 } // 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 pain

		 // draw rectangle starting from (11, 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 result is " + result, 25, 25 ); 
	 } // end method paint
} // end class AdditionApplet

Open in new window


Hope you are studying Java all by yourselef, not in a class so that we are not violating EE rules



ok thanks for help