943,846 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2757
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Aug 5th, 2007
0

Re: a for loop problem

Thanks for your advice and interest.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lamees is offline Offline
11 posts
since Oct 2006
Aug 5th, 2007
0

Re: a for loop problem

What you've got is a good start. Here's how I would do it:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 23
Solved Threads: 2
Junior Poster in Training
indienick is offline Offline
71 posts
since Aug 2005
Aug 5th, 2007
0

Re: a for loop problem

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).
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 5th, 2007
0

Re: a for loop problem

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.
Reputation Points: 23
Solved Threads: 2
Junior Poster in Training
indienick is offline Offline
71 posts
since Aug 2005
Aug 6th, 2007
1

Re: a for loop problem

Click to Expand / Collapse  Quote originally posted by indienick ...
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.

Quote ...
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.

Quote ...
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
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: engg. java project
Next Thread in Java Forum Timeline: From Java beginner:Am I right with answers?:)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC