?
Solved

Java How to Program Exercise Question

Posted on 2011-10-08
12
Medium Priority
?
1,238 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:bakerule22
  • 7
  • 5
12 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36936787
I think you should just change doulb to double and jOptionPane to JOptionPane
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36936791
And it makes a sum for me just after these two corrections
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36936793


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



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



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bakerule22
ID: 36936804
ok
0
 

Author Comment

by:bakerule22
ID: 36936817
// 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
 
LVL 47

Expert Comment

by:for_yan
ID: 36936822

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
 
LVL 47

Expert Comment

by:for_yan
ID: 36936827
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
 
LVL 47

Accepted Solution

by:
for_yan earned 200 total points
ID: 36936830
I meant

replace
sum = number1 + number2;

with the code in the box below
0
 

Author Comment

by:bakerule22
ID: 36936876
ok thanks
0
 

Author Comment

by:bakerule22
ID: 36940511
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36940536
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
 

Author Comment

by:bakerule22
ID: 36940575
ok thanks for help
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question