doing a program blinded

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

Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: doing a program blinded

 
0
  #21
Apr 20th, 2008
I can try explaining it better if noone understands what I mean
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: doing a program blinded

 
0
  #22
Apr 20th, 2008
Jim you need to pay close attention to your run-time errors. You have a problem here:

  1. for(int i = 0; i <=16; i++)
  2. topDisplay[i].setBackground(Color.blue);
  3.  
  4. for(int i = start; i <= stop; step++)
  5. topDisplay[i].setBackground(Color.yellow);
You have two loops. Don't assume the problem is in the second one. Look closely at the numbers in the first loop, and look closely at the instructions regarding array indexes give by your teacher (see bold):

write another loop that’s based on the user inputs. Each time the loop is executed, change the background color to yellow (so… start your loop at the user’s specified starting condition. You’ll stop at the user’s specified stopping value. You’ll change the fields to yellow every time you increment your loop based on the step value. REMEMBER: Your displayed values are 1 off from your index numbers!!)
Also, did you get something like this?
  1. Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 16
  2. at Checkerboard.actionPerformed(Checkerboard.java:117)

You need to read these error messages carefully. They'll tell you exactly what the problem is and where it is usually.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: doing a program blinded

 
0
  #23
Apr 20th, 2008
got the yellow boxes to come up but just one box.....so is there something wrong with my yellow set background because I am not seeing any more errors

  1.  
  2. //packages to import
  3. import javax.swing.JOptionPane;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7.  
  8. public class Checkerboard extends Frame implements ActionListener
  9. {
  10. private Panel topPanel;
  11. private TextArea topDisplay[];
  12. private Panel bottomPanel;
  13. private TextField startField = new TextField(10);
  14. private TextField stopField = new TextField(10);
  15. private TextField stepField = new TextField(10);
  16. private Label startLabel = new Label ("Start");
  17. private Label stopLabel = new Label ("Stop");
  18. private Label stepLabel = new Label ("Step");
  19. private Button goButton;
  20. private Button clearButton;
  21. private boolean clearText;
  22. private boolean first;
  23. private int start;
  24. private int stop;
  25. private int step;
  26.  
  27. //constructor methods
  28. public Checkerboard()
  29.  
  30. {
  31. //construct components and initialize beginning values
  32. topPanel = new Panel();
  33. topDisplay = new TextArea[16];
  34.  
  35.  
  36. goButton = new Button("Go");
  37. clearButton = new Button("Clear");
  38. first = true;
  39. bottomPanel = new Panel();
  40. int start = 0;
  41. int stop = 0;
  42. int step = 0;
  43. bottomPanel.add(startField);
  44. bottomPanel.add(stopField);
  45. bottomPanel.add(stepField);
  46. bottomPanel.add(startLabel);
  47. bottomPanel.add(stopLabel);
  48. bottomPanel.add(stepLabel);
  49. bottomPanel.add(goButton);
  50. goButton.addActionListener(this);
  51. bottomPanel.add(clearButton);
  52. clearButton.addActionListener(this);
  53. clearText = true;
  54.  
  55. //set layouts for the Frame and Panels
  56. setLayout(new BorderLayout());
  57. topPanel.setLayout(new GridLayout(4, 4, 10, 10));
  58. bottomPanel.setLayout(new GridLayout(3, 3, 5, 5));
  59.  
  60. //construct the Display
  61. for(int i = 0; i <= 15; i++)
  62. {
  63. topDisplay[i] = new TextArea(null, 3, 5, 3);
  64. topDisplay[i].setText(String.valueOf(i+1));
  65. topDisplay[i].setEditable(false);
  66. topPanel.add(topDisplay[i]);
  67. }
  68.  
  69. //add components to frame
  70. add(topPanel, BorderLayout.NORTH);
  71. add(bottomPanel, BorderLayout.CENTER);
  72.  
  73.  
  74. //allow the x to close the application
  75. addWindowListener(new WindowAdapter()
  76. {
  77. public void windowClosing(WindowEvent e)
  78. {
  79. System.exit(0);
  80. }
  81. } //end window adapter
  82. );
  83. }
  84.  
  85. public static void main(String args[])
  86. {
  87. Checkerboard f = new Checkerboard();
  88. f.setTitle("Checkerboard Array");
  89. f.setBounds(50, 100, 300, 400);
  90. f.setLocationRelativeTo(null);
  91. f.setVisible(true);
  92. } //end main
  93.  
  94. public void actionPerformed(ActionEvent e)
  95. {
  96. boolean done = false;
  97.  
  98. //test go
  99. String arg = e.getActionCommand();
  100.  
  101. //go button was clicked
  102. if(arg.equals("Go"))
  103. {
  104. //convert data in TextField to int
  105. int start = Integer.parseInt(startField.getText());
  106. int stop = Integer.parseInt(stopField.getText());
  107. int step = Integer.parseInt(stepField.getText());
  108.  
  109. while(!done)
  110. {
  111. try
  112. {
  113. if((start <= 1) && (start > 16)) throw new NumberFormatException();
  114. else done = true;
  115. } //end try
  116. catch (NumberFormatException f)
  117. {
  118. JOptionPane.showMessageDialog(null, "You must enter start between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE);
  119. startField.setText(" ");
  120. startField.requestFocus();
  121. } //end catch
  122. try
  123. {
  124. if ((stop < 1) && (stop > 16)) throw new NumberFormatException();
  125. else done = true;
  126. } //end try
  127. catch (NumberFormatException f)
  128. {
  129. JOptionPane.showMessageDialog(null, "You must enter stop between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE);
  130. stopField.setText(" ");
  131. stopField.requestFocus();
  132. } //end catch
  133. try
  134. {
  135. if ((step < 1) && (step > 16)) throw new NumberFormatException();
  136. else done = true;
  137. } //end try
  138. catch (NumberFormatException f)
  139. {
  140. JOptionPane.showMessageDialog(null, "You must enter step between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE);
  141. stepField.setText(" ");
  142. stepField.requestFocus();
  143. } //end catch
  144. try
  145. {
  146. if (start > stop) throw new NumberFormatException();
  147. else done = true;
  148. } //end try
  149. catch (NumberFormatException f)
  150. {
  151. JOptionPane.showMessageDialog(null, "Stop cannot be larger than start", "Error", JOptionPane.ERROR_MESSAGE);
  152. startField.setText(" ");
  153. stopField.setText(" ");
  154. stepField.setText(" ");
  155. startField.requestFocus();
  156. } //end catch
  157. } //end while
  158.  
  159. for(int i = 0; i <=15; i++)
  160. topDisplay[i].setBackground(Color.blue);
  161.  
  162. for(int i = start; i <= stop; step++)
  163. topDisplay[i].setBackground(Color.yellow);
  164. } //end the if go
  165.  
  166. //clear button was clicked
  167. if(arg.equals("Clear"))
  168. {
  169. clearText = true;
  170. startField.setText("");
  171. stopField.setText("");
  172. stepField.setText("");
  173. first = true;
  174. setBackground(Color.white);
  175. startField.requestFocus();
  176.  
  177. } //end the if clear
  178. }//end action listener
  179. }//end class
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: doing a program blinded

 
0
  #24
Apr 20th, 2008
Almost finished just working on my try\catch and I will be done can anyone let me know if I am on the right track and if I am any advice id greatly appreciate it if its before 11?

  1.  
  2. //packages to import
  3. import javax.swing.JOptionPane;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7.  
  8. public class Checkerboard extends Frame implements ActionListener
  9. {
  10. private Panel topPanel;
  11. private TextArea topDisplay[];
  12. private Panel bottomPanel;
  13. private TextField startField = new TextField(10);
  14. private TextField stopField = new TextField(10);
  15. private TextField stepField = new TextField(10);
  16. private Label startLabel = new Label ("Start");
  17. private Label stopLabel = new Label ("Stop");
  18. private Label stepLabel = new Label ("Step");
  19. private Button goButton;
  20. private Button clearButton;
  21. private boolean clearText;
  22. private boolean first;
  23. private int start;
  24. private int stop;
  25. private int step;
  26.  
  27. //constructor methods
  28. public Checkerboard()
  29.  
  30. {
  31. //construct components and initialize beginning values
  32. topPanel = new Panel();
  33. topDisplay = new TextArea[16];
  34.  
  35.  
  36. goButton = new Button("Go");
  37. clearButton = new Button("Clear");
  38. first = true;
  39. bottomPanel = new Panel();
  40. int start = 0;
  41. int stop = 0;
  42. int step = 0;
  43. bottomPanel.add(startField);
  44. bottomPanel.add(stopField);
  45. bottomPanel.add(stepField);
  46. bottomPanel.add(startLabel);
  47. bottomPanel.add(stopLabel);
  48. bottomPanel.add(stepLabel);
  49. bottomPanel.add(goButton);
  50. goButton.addActionListener(this);
  51. bottomPanel.add(clearButton);
  52. clearButton.addActionListener(this);
  53. clearText = true;
  54.  
  55. //set layouts for the Frame and Panels
  56. setLayout(new BorderLayout());
  57. topPanel.setLayout(new GridLayout(4, 4, 10, 10));
  58. bottomPanel.setLayout(new GridLayout(3, 3, 5, 5));
  59.  
  60. //construct the Display
  61. for(int i = 0; i <= 15; i++)
  62. {
  63. topDisplay[i] = new TextArea(null, 3, 5, 3);
  64. topDisplay[i].setText(String.valueOf(i+1));
  65. topDisplay[i].setEditable(false);
  66. topPanel.add(topDisplay[i]);
  67. }
  68.  
  69. //add components to frame
  70. add(topPanel, BorderLayout.NORTH);
  71. add(bottomPanel, BorderLayout.CENTER);
  72.  
  73.  
  74. //allow the x to close the application
  75. addWindowListener(new WindowAdapter()
  76. {
  77. public void windowClosing(WindowEvent e)
  78. {
  79. System.exit(0);
  80. }
  81. } //end window adapter
  82. );
  83. }
  84.  
  85. public static void main(String args[])
  86. {
  87. Checkerboard f = new Checkerboard();
  88. f.setTitle("Checkerboard Array");
  89. f.setBounds(50, 100, 300, 400);
  90. f.setLocationRelativeTo(null);
  91. f.setVisible(true);
  92. } //end main
  93.  
  94. public void actionPerformed(ActionEvent e)
  95. {
  96. boolean done = false;
  97.  
  98. //test go
  99. String arg = e.getActionCommand();
  100.  
  101. //go button was clicked
  102. if(arg.equals("Go"))
  103. {
  104. //convert data in TextField to int
  105. int start = Integer.parseInt(startField.getText());
  106. int stop = Integer.parseInt(stopField.getText());
  107. int step = Integer.parseInt(stepField.getText());
  108.  
  109. while(!done)
  110. {
  111. try
  112. {
  113. if((start <= 1) && (start > 16)) throw new NumberFormatException();
  114. else done = true;
  115. } //end try
  116. catch (NumberFormatException f)
  117. {
  118. JOptionPane.showMessageDialog(null, "You must enter start between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE);
  119. startField.setText(" ");
  120. startField.requestFocus();
  121. } //end catch
  122. try
  123. {
  124. if ((stop < 1) && (stop > 16)) throw new NumberFormatException();
  125. else done = true;
  126. } //end try
  127. catch (NumberFormatException f)
  128. {
  129. JOptionPane.showMessageDialog(null, "You must enter stop between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE);
  130. stopField.setText(" ");
  131. stopField.requestFocus();
  132. } //end catch
  133. try
  134. {
  135. if ((step < 1) && (step > 16)) throw new NumberFormatException();
  136. else done = true;
  137. } //end try
  138. catch (NumberFormatException f)
  139. {
  140. JOptionPane.showMessageDialog(null, "You must enter step between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE);
  141. stepField.setText(" ");
  142. stepField.requestFocus();
  143. } //end catch
  144. try
  145. {
  146. if (start > stop) throw new NumberFormatException();
  147. else done = true;
  148. } //end try
  149. catch (NumberFormatException f)
  150. {
  151. JOptionPane.showMessageDialog(null, "Stop cannot be larger than start", "Error", JOptionPane.ERROR_MESSAGE);
  152. startField.setText(" ");
  153. stopField.setText(" ");
  154. stepField.setText(" ");
  155. startField.requestFocus();
  156. } //end catch
  157. } //end while
  158.  
  159. for(int i = 0; i <=15; i++)
  160. topDisplay[i].setBackground(Color.blue);
  161.  
  162. for(int i = start-1; i < stop; i+=step)
  163. topDisplay[i].setBackground(Color.yellow);
  164. } //end the if go
  165.  
  166. //clear button was clicked
  167. if(arg.equals("Clear"))
  168. {
  169. clearText = true;
  170. startField.setText("");
  171. stopField.setText("");
  172. stepField.setText("");
  173. first = true;
  174. setBackground(Color.white);
  175. startField.requestFocus();
  176.  
  177. } //end the if clear
  178. }//end action listener
  179. }//end class
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: doing a program blinded

 
0
  #25
Apr 21st, 2008
last question and I will be done this is the problem I am having and not sure how to do this.....

only change the colors if the numbers are valid

  1.  
  2. //packages to import
  3. import javax.swing.JOptionPane;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7.  
  8. public class Checkerboard extends Frame implements ActionListener
  9. {
  10. private Panel topPanel;
  11. private TextArea topDisplay[];
  12. private Panel bottomPanel;
  13. private TextField startField = new TextField(10);
  14. private TextField stopField = new TextField(10);
  15. private TextField stepField = new TextField(10);
  16. private Label startLabel = new Label ("Start");
  17. private Label stopLabel = new Label ("Stop");
  18. private Label stepLabel = new Label ("Step");
  19. private Button goButton;
  20. private Button clearButton;
  21. private boolean clearText;
  22. private boolean first;
  23. private int start;
  24. private int stop;
  25. private int step;
  26.  
  27. //constructor methods
  28. public Checkerboard()
  29.  
  30. {
  31. //construct components and initialize beginning values
  32. topPanel = new Panel();
  33. topDisplay = new TextArea[16];
  34.  
  35.  
  36. goButton = new Button("Go");
  37. clearButton = new Button("Clear");
  38. first = true;
  39. bottomPanel = new Panel();
  40. int start = 0;
  41. int stop = 0;
  42. int step = 0;
  43. bottomPanel.add(startField);
  44. bottomPanel.add(stopField);
  45. bottomPanel.add(stepField);
  46. bottomPanel.add(startLabel);
  47. bottomPanel.add(stopLabel);
  48. bottomPanel.add(stepLabel);
  49. bottomPanel.add(goButton);
  50. goButton.addActionListener(this);
  51. bottomPanel.add(clearButton);
  52. clearButton.addActionListener(this);
  53. clearText = true;
  54.  
  55. //set layouts for the Frame and Panels
  56. setLayout(new BorderLayout());
  57. topPanel.setLayout(new GridLayout(4, 4, 10, 10));
  58. bottomPanel.setLayout(new GridLayout(3, 3, 5, 5));
  59.  
  60. //construct the Display
  61. for(int i = 0; i <= 15; i++)
  62. {
  63. topDisplay[i] = new TextArea(null, 3, 5, 3);
  64. topDisplay[i].setText(String.valueOf(i+1));
  65. topDisplay[i].setEditable(false);
  66. topPanel.add(topDisplay[i]);
  67. }
  68.  
  69. //add components to frame
  70. add(topPanel, BorderLayout.NORTH);
  71. add(bottomPanel, BorderLayout.CENTER);
  72.  
  73.  
  74. //allow the x to close the application
  75. addWindowListener(new WindowAdapter()
  76. {
  77. public void windowClosing(WindowEvent e)
  78. {
  79. System.exit(0);
  80. }
  81. } //end window adapter
  82. );
  83. }
  84.  
  85. public static void main(String args[])
  86. {
  87. Checkerboard f = new Checkerboard();
  88. f.setTitle("Checkerboard Array");
  89. f.setBounds(50, 100, 300, 400);
  90. f.setLocationRelativeTo(null);
  91. f.setVisible(true);
  92. } //end main
  93.  
  94. public void actionPerformed(ActionEvent e)
  95. {
  96. boolean done = false;
  97.  
  98. //test go
  99. String arg = e.getActionCommand();
  100.  
  101. //go button was clicked
  102. if(arg.equals("Go"))
  103. {
  104. //convert data in TextField to int
  105. int start = Integer.parseInt(startField.getText());
  106. int stop = Integer.parseInt(stopField.getText());
  107. int step = Integer.parseInt(stepField.getText());
  108.  
  109. while(!done)
  110. {
  111. try
  112. {
  113. if((start <= 1) || (start > 16)) throw new NumberFormatException();
  114. else done = true;
  115. } //end try
  116. catch (NumberFormatException f)
  117. {
  118. JOptionPane.showMessageDialog(null, "You must enter start between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE);
  119. startField.setText(" ");
  120. startField.requestFocus();
  121. } //end catch
  122. try
  123. {
  124. if ((stop < 1) || (stop > 16)) throw new NumberFormatException();
  125. else done = true;
  126. } //end try
  127. catch (NumberFormatException f)
  128. {
  129. JOptionPane.showMessageDialog(null, "You must enter stop between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE);
  130. stopField.setText(" ");
  131. stopField.requestFocus();
  132. } //end catch
  133. try
  134. {
  135. if ((step < 1) || (step > 16)) throw new NumberFormatException();
  136. else done = true;
  137. } //end try
  138. catch (NumberFormatException f)
  139. {
  140. JOptionPane.showMessageDialog(null, "You must enter step between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE);
  141. stepField.setText(" ");
  142. stepField.requestFocus();
  143. } //end catch
  144. try
  145. {
  146. if (start > stop) throw new NumberFormatException();
  147. else done = true;
  148. } //end try
  149. catch (NumberFormatException f)
  150. {
  151. JOptionPane.showMessageDialog(null, "Stop cannot be larger than start", "Error", JOptionPane.ERROR_MESSAGE);
  152. startField.setText(" ");
  153. stopField.setText(" ");
  154. stepField.setText(" ");
  155. startField.requestFocus();
  156. } //end catch
  157. } //end while
  158.  
  159. for(int i = 0; i <=15; i++)
  160. topDisplay[i].setBackground(Color.blue);
  161.  
  162. for(int i = start-1; i < stop; i+=step)
  163. topDisplay[i].setBackground(Color.yellow);
  164. } //end the if go
  165.  
  166. //clear button was clicked
  167. if(arg.equals("Clear"))
  168. {
  169. clearText = true;
  170. startField.setText("");
  171. stopField.setText("");
  172. stepField.setText("");
  173. first = true;
  174. setBackground(Color.white);
  175. startField.requestFocus();
  176. } //end the if clear
  177. }//end action listener
  178. }//end class
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