Applet Help

Reply

Join Date: Sep 2004
Posts: 1
Reputation: Firsttimer is an unknown quantity at this point 
Solved Threads: 0
Firsttimer Firsttimer is offline Offline
Newbie Poster

Applet Help

 
0
  #1
Sep 10th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 47
Reputation: Banderson is an unknown quantity at this point 
Solved Threads: 4
Banderson's Avatar
Banderson Banderson is offline Offline
Light Poster

Re: Applet Help

 
0
  #2
Sep 12th, 2004
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
  1. if ( number1 == number2 )
  2. if ( number1 > number2 );
I inserted
  1. 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....

  1.  
  2. // Java packages
  3. import java.awt.Graphics; // import class Graphics
  4. import javax.swing.*; // import package javax.swing
  5.  
  6.  
  7. public class ComparisonApplet extends JApplet {
  8. boolean comparison;
  9. String firstNumber; // first string entered by user
  10. String secondNumber; // second number entered by user
  11.  
  12. float number1; // first number to be compared
  13. float number2; // second number to be compared
  14. // initialize applet by obtaining values from user
  15. public void init()
  16. {
  17.  
  18.  
  19. // obtain first number from user
  20. firstNumber = JOptionPane.showInputDialog( "Enter first floating-point value" );
  21.  
  22. // obtain second number from user
  23. secondNumber = JOptionPane.showInputDialog( "Enter second floating-point value" );
  24.  
  25. // convert numbers from type String to type float
  26.  
  27. number1 = Float.parseFloat(firstNumber);
  28. number2 = Float.parseFloat(secondNumber);
  29. comparison = number1 >= number2; // comparison of values entered by user
  30.  
  31. } // end method init
  32.  
  33. // draw rectangle on applets background
  34. public void paint( Graphics g)
  35. {
  36. // call superclass version of method paint
  37. super.paint( g );
  38.  
  39. // draw rectangle starting from (15, 10) that is 270
  40. // pixels wide and 20 pixels tall
  41. g.drawRect( 15, 10, 270, 20 );
  42.  
  43. if(number1 == number2)
  44.  
  45. {
  46. g.drawString("The Numbers are equal", 25, 25);
  47. }
  48.  
  49. else if(comparison == true)
  50. {
  51. // draw results as a String at (25,25)
  52. g.drawString( firstNumber + " is larger", 25, 25);
  53. }
  54. else
  55. {
  56.  
  57. g.drawString( secondNumber + " is larger", 25, 25);
  58. }
  59.  
  60. }// end method paint
  61. } // end class Comparison Applet
Reply With Quote Quick reply to this message  
Join Date: Feb 2004
Posts: 16
Reputation: CodeMasterFlex is an unknown quantity at this point 
Solved Threads: 1
CodeMasterFlex's Avatar
CodeMasterFlex CodeMasterFlex is offline Offline
Newbie Poster

Re: Applet Help

 
0
  #3
Sep 13th, 2004
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.
-=CodeMasterFlex=-

Mastering Code One old school party at a time.
Reply With Quote Quick reply to this message  
Join Date: Feb 2004
Posts: 16
Reputation: CodeMasterFlex is an unknown quantity at this point 
Solved Threads: 1
CodeMasterFlex's Avatar
CodeMasterFlex CodeMasterFlex is offline Offline
Newbie Poster

Re: Applet Help

 
0
  #4
Sep 13th, 2004
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.
-=CodeMasterFlex=-

Mastering Code One old school party at a time.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC