| | |
Applet Help
![]() |
•
•
Join Date: Sep 2004
Posts: 1
Reputation:
Solved Threads: 0
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
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
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 I inserted 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....
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
Java Syntax (Toggle Plain Text)
if ( number1 == number2 ) if ( number1 > number2 );
Java Syntax (Toggle Plain Text)
comparison = number1 >= number2;
Java Syntax (Toggle Plain Text)
// 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.
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.
-=CodeMasterFlex=-
Mastering Code One old school party at a time.
Mastering Code One old school party at a time.
![]() |
Similar Threads
- Java Applet viewer blinks everytime the init method is used. (Java)
- Java Game Applet Too Small (Java)
- Problem with Yahoo chat applet. (Windows NT / 2000 / XP)
- help!! My applet can't run... Urgent!! (Java)
- where to get the java applet? (Web Browsers)
- applet crashed (Windows NT / 2000 / XP)
- Applet or Application? (Java)
Other Threads in the Java Forum
- Previous Thread: repeating the whole program after pressing Yes button in the confirmation message box
- Next Thread: javax.comm write error
| Thread Tools | Search this Thread |
6 actuate android api applet application applications array arrays automation balls bank binary bluetooth bold business c++ chat class clear client code codesnippet collections component coordinates database defaultmethod development dice doctype dragging ebook eclipse educational error file formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide ideas image infinite ingres integer intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans openjavafx parameter php problem program programming project recursion repositories scanner scrollbar sell server set sms sort sorting sql sqlserver state storm string sun superclass swing swt threads tree websites windows





