Purchase Solution

Java Applets - Floating-Point Numbers

Not what you're looking for?

Ask Custom Question

Write an applet that asks the user to enter two floating-point numbers, obtains the numbers from the user and displays the two numbers first and then the larger number followed by the words "is larger" as a string on the applet. If the numbers are equal, the applet should print the message "These numbers are equal." Use the techniques shown in Fig. 20.10.

--------------------------------------------

// Fig. 20.10: AdditionApplet.java
// Adding two floating-point numbers.
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
} // 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 );
} // end method paint
} // end class AdditionApplet

Purchase this Solution

Solution Summary

Write an applet that asks the user to enter two floating-point numbers, obtains the numbers from the user and displays the two numbers first and then the larger number followed by the words "is larger" as a string on the applet.

Purchase this Solution


Free BrainMass Quizzes
Javscript Basics

Quiz on basics of javascript programming language.

C++ Operators

This quiz tests a student's knowledge about C++ operators.

C# variables and classes

This quiz contains questions about C# classes and variables.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Basic Networking Questions

This quiz consists of some basic networking questions.