Hello All,
This is my first time taking Java, so I might need quite a bit of help form ya all.
Anyways, I have this program that has to be in Applet, and it is comparing two floating numbers. I have to have the program decide which one is larger, then say that number is bigger, and if the two numbers are equal, I have to have it specify that these numbers are equal. So I understand what I have done so far, but dont know how to make the program read those instructions. So here is what I have so far, can anyone please help me?

// Java packages
import java.awt.Graphics;   // import class Graphics
import javax.swing.*;      // import package javax.swing


public class ComparisonApplet extends JApplet {
float comparison;    // comparison 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 number entered by user


float number1;        // first number to be compared
float number2;        // second number to be compared


// 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 float
number1 = Float.parseFloat( firstNumber );
number2 = Float.parseFloat( secondNumber );


// compare numbers
if ( number1 == number2 )
if ( number1 > number2 );


} // end method init


// draw rectangle on applets background
public void paint( Graphics g)
{
// call superclass version of method paint
super.paint( g );


// 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( "Is larger" + comparison, 25, 25 );
}  // end method paint


}  // end class Comparison Applet

Recommended Answers

All 3 Replies

Hello Firsttimer,
I think I have the program working the way you needed it too. I usually don't complete homework projects, but you was pretty close. I changed your variable float comparison to boolean and instead of

if ( number1 == number2 ) 
if ( number1 > number2 );

I inserted

comparison = number1 >= number2;

since it is a better programming practice and less prone to error. I also inserted if, else statements for the output. I guess you want it now, right? Ok, here it is....

// Java packages
import java.awt.Graphics; // import class Graphics
import javax.swing.*; // import package javax.swing


public class ComparisonApplet extends JApplet {
boolean comparison;  
String firstNumber; // first string entered by user
String secondNumber; // second number entered by user

float number1; // first number to be compared
float number2; // second number to be compared
// initialize applet by obtaining values from user
public void init()
{


// 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 float

number1 = Float.parseFloat(firstNumber);
number2 = Float.parseFloat(secondNumber);
comparison = number1 >= number2; // comparison of values entered by user

} // end method init

// draw rectangle on applets background
public void paint( Graphics g) 
{
// call superclass version of method paint
super.paint( g );

// draw rectangle starting from (15, 10) that is 270
// pixels wide and 20 pixels tall
g.drawRect( 15, 10, 270, 20 );

if(number1 == number2)

{
	g.drawString("The Numbers are equal", 25, 25);	
}

else if(comparison == true)
{
// draw results as a String at (25,25)
g.drawString( firstNumber + " is larger", 25, 25);
}
else
{
		
g.drawString( secondNumber + " is larger", 25, 25);	
}

}// end method paint
} // end class Comparison Applet

yea your comparison is badly coded.

Your code:

if ( number1 == number2 )
if ( number1 > number2 );

this won't work.

if(number1 == number2)
{
string comparison = "your string here";
}
elseif(number1 > number2)
{
string comparison = "your string here";
}

that code should work alot better although I don't guarantee correctness as I don't have a java capable machine on hand to try it with.

you should probably add another else to that statement incase number1 is less than number2 because in that case you'll need another string statement to paint.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.