doing a program blinded

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

doing a program blinded

 
0
  #1
Apr 17th, 2008
I was sick the day on our review for this programming assignment so I am in kind of a dillema with a jerk for a professor...What I am going to do is after each section show you guys what I have and I don't think I should have too many problems but would it be possible for feedback. If not thats ok I was just seeing if that is possible. This is what I have so far from completing section 2 and I think I got it done right just making sure.....

  1.  
  2.  
  3. //packages to import
  4. import java.swing.JOptionPane;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7.  
  8. public class Checkerboard extends Frame implements ActionListener
  9. {
  10. private Panel topPanel = new Panel();
  11. TextArea topDisplay[] = new TextArea[16];
  12.  
  13. private Panel bottomPanel = new Panel();
  14. private TextField startField = new TextField(10);
  15. private TextField stopField = new TextField(10);
  16. private TextField stepField = new TextField(10);
  17. private Label startLabel = new Label ("Start");
  18. private Label stopLabel = new Label ("Stop");
  19. private Label stepLabel = new Label ("Step");
  20. private Button goButton;
  21. private Button clearButton;
  22.  
  23. private int start;
  24. private int stop;
  25. private int step;
  26.  
  27. public Checkerboard()
  28.  
  29. {
  30.  
  31.  
  32.  
  33.  
  34.  
  35. }//end action listener
  36. }//end class

this is my instructions:

This will incorporate arrays, for loops, and Frames all in one.

Create a panel containing an array of 16 TextArea components that change color to correspond with the start, stop, and step values entered by the user. Perform the following tasks to create the Checkerboard Array application shown below. When the user enters the start, stop, and step fields and then clicks the Go button, the results are also shown below.

1. Call your application Checkerboard.java
2. You will need the following variables… declare them as private:
a. 16 component TextArea array
b. a Panel to hold the array
c. 3 TextField components with length of 10
d. 3 int variables to receive the start, stop, and step values
e. 3 Labels to display the words Start, Stop, and Step
f. a Go button
g. a Clear button
h. a Panel to hold the 3 TextFields, 3 Labels, and the 2 Buttons
3. Create a constructor method to:
a. construct each of the components declared above and initializes the start, stop, and step variables to zero (when constructing the TextArea components, use the following parameters: null, 3, 5, 3)
b. set the Frame layout to BorderLayout
c. write a for loop to loop the array and set each of the 16 TextArea components in that array so they cannot be edited. In the same loop, set each of the TextArea components text to be 1 more than the index number. Also in this same loop, set the background of each of the TextArea components to white.
d. set the Panel for the TextArea components to GridLayout with 4 rows, 4 columns, and both gaps set to 10
e. set the Panel for the TextFields, Labels, and button to GridLayout with 3 rows, 3 columns, and both gaps set to 5
f. add the components to their respective Panels
g. make the buttons clickable
h. place the Panels in the Frame… put one in the NORTH and one in the CENTER
i. Enter the addWindowListener() method described in the chapter… this is the method that overrides the click of the X so it terminates the application
4. In your actionPerformed() method:
a. convert the data in your TextFields to int and store them in the variables declared above
b. write a loop that goes through the array setting every background color to blue
c. 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!!)


5. Write a main() method that creates an instance of the Checkerboard Frame.
a. set the bounds for the frame to 50, 100, 300, 400
b. set the title bar caption to Checkerboard Array
c. use the setVisible() method to display the application Frame during execution
6. After you get all of this complete, include error handling to make sure:
a. the values entered in the TextFields are valid integers
b. the start value is greater than or equal to 1 and less than or equal to 16
c. the stop value is greater than or equal to 1 and less than or equal to 16
d. the step value is greater than or equal to 1 and less than or equal to 16
e. the start condition is less than the stop condition
f. when an error occurs, give an error message in a dialog box that is specific to their error, remove the text from the invalid field, and put the cursor in that field so the user has a chance to re-enter… this can be accomplished by using multiple try/catch statements
g. only change the colors if the numbers are valid
7. Create a clear button as seen in the example below. This button should:
a. clear out all 3 TextFields
b. change the background color of all TextArea array elements to white
c. put the cursor in the start field
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 771
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 104
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: doing a program blinded

 
0
  #2
Apr 17th, 2008
Well I have to admit I usually use swing components rather than awt components for my GUIs. Has your instructor specifically asked you to use awt components? Changing to swing is easy - import javax.swing.* and add a J to the front of each of your GUI components. Other than that, looks like number 2 is done for you...
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
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
  #3
Apr 17th, 2008
Really got a lot done today...I finished up with constructor, main, and clear button and just trying to verify everything is right. Also I am getting one error and I am sure it is something minor just having a hard time trying to figure out what it is so could anyone if they have time look at what I have along with the instructions and just make sure I have everything done right with the exception of action performed and the error I am getting is...

H:\JAVA\Checkerboard.java:102: cannot resolve symbol
symbol : method setSize (int,int,int,int)
location: class Checkerboard
f.setSize(50, 100, 300, 400);
^
code is:

  1.  
  2. //packages to import
  3. import javax.swing.JOptionPane;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class Checkerboard extends Frame implements ActionListener
  8. {
  9. private Panel topPanel;
  10. private TextField topDisplay;
  11. private Panel bottomPanel;
  12. private TextField startField = new TextField(10);
  13. private TextField stopField = new TextField(10);
  14. private TextField stepField = new TextField(10);
  15. private Label startLabel = new Label ("Start");
  16. private Label stopLabel = new Label ("Stop");
  17. private Label stepLabel = new Label ("Step");
  18. private Button goButton[];
  19. private Button clearButton[];
  20. private boolean clearText;
  21. private boolean first;
  22.  
  23. private int start;
  24. private int stop;
  25. private int step;
  26.  
  27. //constructor methods
  28. public Checkerboard()
  29.  
  30. {
  31. //set layouts for the Frame and Panels
  32. setLayout(new BorderLayout());
  33. topPanel.setLayout(new GridLayout(4, 4, 10, 10));
  34. bottomPanel.setLayout(new GridLayout(3, 3, 5, 5));
  35.  
  36. //construct components and initialize beginning values
  37. topDisplay = new TextField(16);
  38. topDisplay.setEditable(false);
  39. topPanel = new Panel();
  40. bottomPanel = new Panel();
  41. bottomPanel.add(startField);
  42. bottomPanel.add(stopField);
  43. bottomPanel.add(stepField);
  44. bottomPanel.add(startLabel);
  45. bottomPanel.add(stopLabel);
  46. bottomPanel.add(stepLabel);
  47.  
  48. //add components to frame
  49. add(topPanel, BorderLayout.NORTH);
  50. add(bottomPanel, BorderLayout.CENTER);
  51.  
  52. //set color to the panel
  53. //row 1
  54. for(int i = 1; i <= 4; i++)
  55. //set color for
  56. setBackground(Color.white);
  57.  
  58. //row 2
  59. for(int i = 5; i <= 8; i++)
  60. //set color for
  61. setBackground(Color.white);
  62.  
  63. //row 3
  64. for(int i = 9; i <= 12; i++)
  65. //set color for
  66. setBackground(Color.white);
  67.  
  68. //row 4
  69. for(int i = 13; i <= 16; i++)
  70. //set color for
  71. setBackground(Color.white);
  72.  
  73. //register the listener with each Button
  74. for(int i = 0; i < goButton.length; i++)
  75. goButton[i].addActionListener(this);
  76.  
  77. for(int i = 0; i < clearButton.length; i++)
  78. clearButton[i].addActionListener(this);
  79.  
  80. //allow the x to close the application
  81. addWindowListener(new WindowAdapter()
  82. {
  83. public void windowclosing(WindowEvent e)
  84. {
  85. System.exit(0);
  86. }
  87. } //end window adapter
  88. );
  89.  
  90. }
  91.  
  92. public static void main(String args[])
  93. {
  94. Checkerboard f = new Checkerboard();
  95. f.setTitle("Checkerboard Array");
  96. f.setSize(50, 100, 300, 400);
  97. f.setLocationRelativeTo(null);
  98. f.setVisible(true);
  99.  
  100. } //end main
  101.  
  102. public void actionPerformed(ActionEvent e)
  103. {
  104. //test clear
  105. String arg = e.getActionCommand();
  106.  
  107. //clear button was clicked
  108. if(arg.equals("Clear"))
  109. {
  110. clearText = true;
  111. start = 0;
  112. stop = 0;
  113. step = 0;
  114. first = true;
  115. setBackground(Color.white);
  116. startField.requestFocus();
  117.  
  118. } //end the if clear
  119.  
  120. }//end action listener
  121. }//end class
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
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: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: doing a program blinded

 
1
  #4
Apr 17th, 2008
You just used the wrong method call for the Frame sizing - easy to fix. setSize() will take either a (width,height) or a Dimension argument. There is another method setBounds() that takes (x,y,width,height), which is probably the one you are trying to use.
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
  #5
Apr 17th, 2008
yea that worked fine...one other quick question...i tried to run my code and it will not let me but compiled fine...is there something I did wrong with my code or something that I just have not programmed yet because it looks fine on my end...

  1.  
  2. //packages to import
  3. import javax.swing.JOptionPane;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class Checkerboard extends Frame implements ActionListener
  8. {
  9. private Panel topPanel;
  10. private TextField topDisplay;
  11. private Panel bottomPanel;
  12. private TextField startField = new TextField(10);
  13. private TextField stopField = new TextField(10);
  14. private TextField stepField = new TextField(10);
  15. private Label startLabel = new Label ("Start");
  16. private Label stopLabel = new Label ("Stop");
  17. private Label stepLabel = new Label ("Step");
  18. private Button goButton[];
  19. private Button clearButton[];
  20. private boolean clearText;
  21. private boolean first;
  22.  
  23. private int start;
  24. private int stop;
  25. private int step;
  26.  
  27. //constructor methods
  28. public Checkerboard()
  29.  
  30. {
  31. //set layouts for the Frame and Panels
  32. setLayout(new BorderLayout());
  33. topPanel.setLayout(new GridLayout(4, 4, 10, 10));
  34. bottomPanel.setLayout(new GridLayout(3, 3, 5, 5));
  35.  
  36. //construct components and initialize beginning values
  37. topDisplay = new TextField(16);
  38. topDisplay.setEditable(false);
  39. topPanel = new Panel();
  40. bottomPanel = new Panel();
  41. bottomPanel.add(startField);
  42. bottomPanel.add(stopField);
  43. bottomPanel.add(stepField);
  44. bottomPanel.add(startLabel);
  45. bottomPanel.add(stopLabel);
  46. bottomPanel.add(stepLabel);
  47.  
  48. //add components to frame
  49. add(topPanel, BorderLayout.NORTH);
  50. add(bottomPanel, BorderLayout.CENTER);
  51.  
  52. //set color to the panel
  53. //row 1
  54. for(int i = 1; i <= 4; i++)
  55. //set color for
  56. setBackground(Color.white);
  57.  
  58. //row 2
  59. for(int i = 5; i <= 8; i++)
  60. //set color for
  61. setBackground(Color.white);
  62.  
  63. //row 3
  64. for(int i = 9; i <= 12; i++)
  65. //set color for
  66. setBackground(Color.white);
  67.  
  68. //row 4
  69. for(int i = 13; i <= 16; i++)
  70. //set color for
  71. setBackground(Color.white);
  72.  
  73. //register the listener with each Button
  74. for(int i = 0; i < goButton.length; i++)
  75. goButton[i].addActionListener(this);
  76.  
  77. for(int i = 0; i < clearButton.length; i++)
  78. clearButton[i].addActionListener(this);
  79.  
  80. //allow the x to close the application
  81. addWindowListener(new WindowAdapter()
  82. {
  83. public void windowclosing(WindowEvent e)
  84. {
  85. System.exit(0);
  86. }
  87. } //end window adapter
  88. );
  89.  
  90. }
  91.  
  92. public static void main(String args[])
  93. {
  94. Checkerboard f = new Checkerboard();
  95. f.setTitle("Checkerboard Array");
  96. f.setBounds(50, 100, 300, 400);
  97. f.setLocationRelativeTo(null);
  98. f.setVisible(true);
  99.  
  100. } //end main
  101.  
  102. public void actionPerformed(ActionEvent e)
  103. {
  104. //test clear
  105. String arg = e.getActionCommand();
  106.  
  107. //clear button was clicked
  108. if(arg.equals("Clear"))
  109. {
  110. clearText = true;
  111. start = 0;
  112. stop = 0;
  113. step = 0;
  114. first = true;
  115. setBackground(Color.white);
  116. startField.requestFocus();
  117.  
  118. } //end the if clear
  119.  
  120. }//end action listener
  121. }//end class
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 771
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 104
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: doing a program blinded

 
0
  #6
Apr 18th, 2008
Originally Posted by jimJohnson View Post
yea that worked fine...one other quick question...i tried to run my code and it will not let me but compiled fine...is there something I did wrong with my code or something that I just have not programmed yet because it looks fine on my end...
What error message do you get when you try to run the program?
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
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: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: doing a program blinded

 
0
  #7
Apr 18th, 2008
The null pointer error (with line number noted) indicates this is the problem
  1. topPanel.setLayout(new GridLayout(4, 4, 10, 10));
NullPointerException indicates you tried to call a method or access a variable on an object reference that has not been initialized (it's still null). Your initialization of "topPanel" occurs 6 lines below that code, so the solution should be apparent.
Last edited by Ezzaral; Apr 18th, 2008 at 11:58 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,145
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: doing a program blinded

 
0
  #8
Apr 18th, 2008
"I was sick the day on our review for this programming assignment so I am in kind of a dillema with a jerk for a professor..."

Bad attitude. You're "professor" (more likely a highschool teacher if that) is no jerk. You're mistaken if you think he's going to allow you to fall behind on your studies and give you full marks for homework not done and material not studied just because you had a headache the night before.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
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
  #9
Apr 18th, 2008
This is the error I am getting in the command line console.....

Exception in thread "main" java.lang.NullPointerException
at Checkerboard.<init><Checkerboard.java:39>
at Checkerboard.main<Checkerboard.java:100>
Press any key to continue

my code is:

  1. //packages to import
  2. import javax.swing.JOptionPane;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class Checkerboard extends Frame implements ActionListener
  7. {
  8. private Panel topPanel;
  9. private TextField topDisplay;
  10. private Panel bottomPanel;
  11. private TextField startField = new TextField(10);
  12. private TextField stopField = new TextField(10);
  13. private TextField stepField = new TextField(10);
  14. private Label startLabel = new Label ("Start");
  15. private Label stopLabel = new Label ("Stop");
  16. private Label stepLabel = new Label ("Step");
  17. private Button goButton[];
  18. private Button clearButton[];
  19. private boolean clearText;
  20. private boolean first;
  21.  
  22. private int start;
  23. private int stop;
  24. private int step;
  25.  
  26. //constructor methods
  27. public Checkerboard()
  28.  
  29. {
  30. //set layouts for the Frame and Panels
  31. setLayout(new BorderLayout());
  32. topPanel.setLayout(new GridLayout(4, 4, 10, 10));
  33. bottomPanel.setLayout(new GridLayout(3, 3, 5, 5));
  34.  
  35. //construct components and initialize beginning values
  36. topDisplay = new TextField(16);
  37. topDisplay.setEditable(false);
  38. topPanel = new Panel();
  39. bottomPanel = new Panel();
  40. bottomPanel.add(startField);
  41. bottomPanel.add(stopField);
  42. bottomPanel.add(stepField);
  43. bottomPanel.add(startLabel);
  44. bottomPanel.add(stopLabel);
  45. bottomPanel.add(stepLabel);
  46.  
  47. //add components to frame
  48. add(topPanel, BorderLayout.NORTH);
  49. add(bottomPanel, BorderLayout.CENTER);
  50.  
  51. //set color to the panel
  52. //row 1
  53. for(int i = 1; i <= 4; i++)
  54. //set color for
  55. setBackground(Color.white);
  56.  
  57. //row 2
  58. for(int i = 5; i <= 8; i++)
  59. //set color for
  60. setBackground(Color.white);
  61.  
  62. //row 3
  63. for(int i = 9; i <= 12; i++)
  64. //set color for
  65. setBackground(Color.white);
  66.  
  67. //row 4
  68. for(int i = 13; i <= 16; i++)
  69. //set color for
  70. setBackground(Color.white);
  71.  
  72. //register the listener with each Button
  73. for(int i = 0; i < goButton.length; i++)
  74. goButton[i].addActionListener(this);
  75.  
  76. for(int i = 0; i < clearButton.length; i++)
  77. clearButton[i].addActionListener(this);
  78.  
  79. //allow the x to close the application
  80. addWindowListener(new WindowAdapter()
  81. {
  82. public void windowclosing(WindowEvent e)
  83. {
  84. System.exit(0);
  85. }
  86. } //end window adapter
  87. );
  88.  
  89. }
  90.  
  91. public static void main(String args[])
  92. {
  93. Checkerboard f = new Checkerboard();
  94. f.setTitle("Checkerboard Array");
  95. f.setBounds(50, 100, 300, 400);
  96. f.setLocationRelativeTo(null);
  97. f.setVisible(true);
  98.  
  99. } //end main
  100.  
  101. public void actionPerformed(ActionEvent e)
  102. {
  103. //test clear
  104. String arg = e.getActionCommand();
  105.  
  106. //clear button was clicked
  107. if(arg.equals("Clear"))
  108. {
  109. clearText = true;
  110. start = 0;
  111. stop = 0;
  112. step = 0;
  113. first = true;
  114. setBackground(Color.white);
  115. startField.requestFocus();
  116.  
  117. } //end the if clear
  118.  
  119. }//end action listener
  120. }//end class
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
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: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: doing a program blinded

 
0
  #10
Apr 18th, 2008
I already explained the reason for that error - you have not yet initialized your panels when you call
  1. topPanel.setLayout(new GridLayout(4, 4, 10, 10));
  2. bottomPanel.setLayout(new GridLayout(3, 3, 5, 5));
The code that creates those panels is several lines below those calls
  1. topPanel = new Panel();
  2. bottomPanel = new Panel();
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