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

bakerule22Asked:
Who is Participating?
 
for_yanCommented:
I meant

replace
sum = number1 + number2;

with the code in the box below
0
 
for_yanCommented:
I think you should just change doulb to double and jOptionPane to JOptionPane
0
 
for_yanCommented:
And it makes a sum for me just after these two corrections
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
for_yanCommented:


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



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



0
 
bakerule22Author Commented:
ok
0
 
bakerule22Author Commented:
// 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
0
 
for_yanCommented:

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

0
 
for_yanCommented:
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
0
 
bakerule22Author Commented:
ok thanks
0
 
bakerule22Author Commented:
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

0
 
for_yanCommented:
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



0
 
bakerule22Author Commented:
ok thanks for help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.