a for loop problem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2006
Posts: 11
Reputation: lamees is an unknown quantity at this point 
Solved Threads: 0
lamees lamees is offline Offline
Newbie Poster

Re: a for loop problem

 
0
  #11
Aug 5th, 2007
Thanks for your advice and interest.
I can't teach anybody anything, I only can tell them how to think (Socrattes).
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 71
Reputation: indienick is an unknown quantity at this point 
Solved Threads: 2
indienick's Avatar
indienick indienick is offline Offline
Junior Poster in Training

Re: a for loop problem

 
0
  #12
Aug 5th, 2007
What you've got is a good start. Here's how I would do it:
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class GuessGame extends JFrame {
  6.  
  7. private static JButton button;
  8. private static JLabel label1, label2;
  9. private static JTextField textfield;
  10. private static Color backgroundColor;
  11. private static Container content;
  12.  
  13. private int valueToGuess; // Equivalent to "x" in your code.
  14. private int numberOfTrials = 0;
  15.  
  16. public static void main(String[] args) {
  17.  
  18. initializeComponents();
  19.  
  20. valueToGuess = randomNumber(); // Initialize valueToGuess.
  21.  
  22. finalizeComponents();
  23.  
  24. }
  25.  
  26. private static void resetTrialCounter() {
  27.  
  28. numberOfTrials = 0;
  29.  
  30. }
  31.  
  32. private static int randomNumber(int min, int max) {
  33.  
  34. return (min + (int)(Math.random() * b));
  35.  
  36. }
  37.  
  38. private static void compare(int a, int b) {
  39.  
  40. if (numberOfTrials >= 5) return;
  41.  
  42. if (a == b) {
  43. label2.setText("Astonishing! The number is actually " + a + "!");
  44. textfield.setEditable(false);
  45. resetTrialCounter(); // Set the numberOfTrials back to 0 (zero).
  46. } else if (a > b) {
  47. label2.setText("Move towards lesser values.");
  48. textfield.setText(null);
  49. numberOfTrials++;
  50. } else {
  51. label2.setText("Move towards larger values.");
  52. textfield.setText(null);
  53. numberOfTrials++;
  54. }
  55.  
  56. }
  57.  
  58. private static void initializeComponents() {
  59.  
  60. content = this.getContentPane();
  61. content.setLayout(new BorderLayout());
  62. content.setBackground(backgrounColor);
  63.  
  64. this.setSize(511, 134);
  65.  
  66. label1 = new JLabel("I have a number between 1 and 1000. Can you guess my number? Enter your guess:");
  67. label2 = new JLabel("--------------------------------------------------");
  68. backgroundColor = new Color(255, 250, 240);
  69.  
  70. button = new JButton("New Game");
  71. button.addActionListener(new ActionListener() {
  72. public void actionPerformed(ActionEvent ae) {
  73. label2.setText("--------------------------------------------------");
  74. textfield.setText(null);
  75. x = randomNumber(); // Assign x a new "random" value.
  76. }
  77. });
  78.  
  79. textfield = new JTextField(20);
  80. textfield.addActionListener(new ActionListener() {
  81. public void actionPerformed(ActionEvent ae) {
  82. compare(valueToGuess, Integer.parseInt(textfield.getText()));
  83. }
  84. });
  85.  
  86. }
  87.  
  88. private static void finalizeComponents() {
  89.  
  90. content.add(label1);
  91. content.add(textfield);
  92. content.add(label2);
  93. content.add(button);
  94.  
  95. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  96. this.setVisible(true);
  97.  
  98. }
  99.  
  100. }

I know you're not dumb, so I'm not going to give you the "don't copy it verbatim" spiel. I haven't written in Java for a while, and I was looking for something to hack out; just scratching an algorithmic itch. Use this as an example. What you had was good, it just wasn't very readable (at least to me). I like to stay away from extends and implements hooks when defining a class (there is a time and a place for them, and this is - sort of - one of them). I find it tends to make things a little harder to figure out.

"Makes things as simple as possible, but no simpler."
Last edited by indienick; Aug 5th, 2007 at 2:16 pm.
Angel-headed hipsters burning for the ancient heavenly connection, to the starry dynamo in the machinery of the night.
-Ginsburg

Don't tell me to "google it" - I already have.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,478
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: a for loop problem

 
0
  #13
Aug 5th, 2007
Actually, making every method static and running the whole thing through main() is just making a procedural program and misses the whole point of OO. Main should only be used as an entry point to set up the needed class(es).
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 71
Reputation: indienick is an unknown quantity at this point 
Solved Threads: 2
indienick's Avatar
indienick indienick is offline Offline
Junior Poster in Training

Re: a for loop problem

 
0
  #14
Aug 5th, 2007
So?

I learned Java just so I knew at least one, non-Smalltalk-based, OOP language.

I'm still making use of other classes within the program (javax.swing.JFrame, javax.swing.JButton, etc.). I don't see what the big deal is. And my take on that algorithm is *not* avoiding the "point" of OOP. I'm providing a clean, readable program that works.

And thank you for clarifying what main()'s purpose is in this world - here I am thinking it's just some useless, needed function. *rolls eyes*

I think it's stupid to instantiate a class from within itself.
Last edited by indienick; Aug 5th, 2007 at 2:39 pm.
Angel-headed hipsters burning for the ancient heavenly connection, to the starry dynamo in the machinery of the night.
-Ginsburg

Don't tell me to "google it" - I already have.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,478
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: a for loop problem

 
1
  #15
Aug 6th, 2007
Originally Posted by indienick View Post
So?

I learned Java just so I knew at least one, non-Smalltalk-based, OOP language.

I'm still making use of other classes within the program (javax.swing.JFrame, javax.swing.JButton, etc.). I don't see what the big deal is. And my take on that algorithm is *not* avoiding the "point" of OOP. I'm providing a clean, readable program that works.
At least you aren't touchy about a suggestion.
Your program certainly does work, it just demonstrates a methodology that a beginner may learn some bad habits from. Making everything static and calling it directly from main() is not a good standard practice.

And thank you for clarifying what main()'s purpose is in this world - here I am thinking it's just some useless, needed function. *rolls eyes*
Well, evidently you do need some clarification, so roll your eyes all you wish - it doesn't make it correct. main() should not directly call state-dependent methods directly inside a class. It should generally be used in a manner like an external driver program to initialize the object and utilize the public methods of that object. The object itself should handle any necessary internal interaction.

I think it's stupid to instantiate a class from within itself.
The object has to come from somewhere. Java only runs .class files and objects don't just pop into being on their own. You shouldn't really think of main() as being part of a class, but rather an entry point to running that class. Use main to set it up and let the object itself take it from there.

I'm sorry if you took my comment as a personal affront, as it wasn't the intention and the code was probably very helpful to the poster. However, those new to Java often aren't yet aware of why things are written the way they are and bad habits can be learned early from sample code they don't fully understand. That was the only reason I felt it necessary to mention the usage of main().
Last edited by Ezzaral; Aug 6th, 2007 at 1:04 pm. Reason: spelling
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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