fork download
  1. /*
  2. Chapter 8: Midterms
  3. Programmer: Ryan Barker
  4. Date: 3-12-2013
  5. Program name: Midterm2.java
  6. */
  7.  
  8. import java.io.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import javax.swing.*;
  12. import javax.swing.JTextField.*;
  13. import java.text.*;
  14. import java.util.*;
  15. import java.lang.Integer.*;
  16.  
  17. public class Midterm2 extends JFrame implements ActionListener
  18. {
  19. //Declare output stream
  20.  
  21. //Construct a panel for each row
  22. JPanel firstRow = new JPanel();
  23. JPanel secondRow = new JPanel();
  24. JPanel thirdRow = new JPanel();
  25. JPanel fourthRow = new JPanel();
  26. JPanel fifthRow = new JPanel();
  27. JPanel sixthRow = new JPanel();
  28. JPanel seventhRow = new JPanel();
  29. JPanel eighthRow = new JPanel();
  30.  
  31. //Construct a panel for the fields and buttons
  32. JPanel fieldPanel = new JPanel();
  33. JPanel buttonPanel = new JPanel();
  34.  
  35. //Variable declaration per wall
  36. //int wall1Area = 81.6;
  37. int widthWall2, heighthWall2, areaWall2;
  38. int widthWall3, heighthWall3, areaWall3;
  39. int widthWall4, heighthWall4, areaWall4;
  40. int totalArea, totalRolls;
  41.  
  42. //Construct labels and text boxes
  43. JLabel totalRollsLabel = new JLabel("Total # of rolls needed: ");
  44. JLabel areaLabel = new JLabel("Width x Heighth = Area");
  45. JLabel wall1Label = new JLabel("Wall 1: 10.2 8 = 81.6 sqft");
  46. JLabel wall2Label = new JLabel("Wall 2");
  47. JTextField wall2Width = new JTextField(5);
  48. JTextField wall2Heighth = new JTextField(5);
  49. JTextField wall2Area = new JTextField(5);
  50. JLabel wall3Label = new JLabel("Wall 3");
  51. JTextField wall3Width = new JTextField(5);
  52. JTextField wall3Heighth = new JTextField(5);
  53. JTextField wall3Area = new JTextField(5);
  54. JLabel wall4Label = new JLabel("Wall 4");
  55. JTextField wall4Width = new JTextField(5);
  56. JTextField wall4Heighth = new JTextField(5);
  57. JTextField wall4Area = new JTextField(5);
  58. JLabel totalAreaLabel = new JLabel("Total Area + 10% = ");
  59.  
  60. //Construct button
  61. JButton submitButton = new JButton("Calculate");
  62.  
  63. public static void main(String[] args) //main method
  64. {
  65. //set the look and feel of the interface
  66. try
  67. {
  68. UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
  69. }
  70. catch(Exception e)
  71. {
  72. JOptionPane.showMessageDialog(null, "The UIManager could not set the Look and Feel.","error",JOptionPane.INFORMATION_MESSAGE);
  73. }
  74.  
  75. //main method executes at run time
  76. Midterm2 f = new Midterm2();
  77. f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  78. f.setSize(450,300);
  79. f.setTitle("Crandall Power and Light Customer Payments");
  80. f.setResizable(false);
  81. f.setLocation(200,200);
  82. f.setVisible(true);
  83. }
  84.  
  85. public Midterm2()
  86. {
  87. Container c = getContentPane();//content pane assigned
  88. c.setLayout((new BorderLayout()));//layout methods
  89. fieldPanel.setLayout(new GridLayout(8,1));
  90. FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
  91. firstRow.setLayout(rowSetup);//layout methods
  92. secondRow.setLayout(rowSetup);
  93. thirdRow.setLayout(rowSetup);
  94. fourthRow.setLayout(rowSetup);
  95. fifthRow.setLayout(rowSetup);
  96. sixthRow.setLayout(rowSetup);
  97. seventhRow.setLayout(rowSetup);
  98. eighthRow.setLayout(rowSetup);
  99. buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));//layout methods
  100.  
  101. //Add fields to rows
  102. firstRow.add(areaLabel);
  103.  
  104. secondRow.add(wall1Label);
  105.  
  106. thirdRow.add(wall2Label);
  107. thirdRow.add(wall2Width);
  108. thirdRow.add(wall2Heighth);
  109. thirdRow.add(wall2Area);
  110.  
  111. fourthRow.add(wall3Label);
  112. fourthRow.add(wall3Width);
  113. fourthRow.add(wall3Heighth);
  114. fourthRow.add(wall3Area);
  115.  
  116. fifthRow.add(wall4Label);
  117. fifthRow.add(wall4Width);
  118. fifthRow.add(wall4Heighth);
  119. fifthRow.add(wall4Area);
  120.  
  121. sixthRow.add(totalAreaLabel);
  122. seventhRow.add(totalRollsLabel);
  123.  
  124. eighthRow.add(submitButton);
  125.  
  126. //Add rows to panel
  127. fieldPanel.add(firstRow);
  128. fieldPanel.add(secondRow);
  129. fieldPanel.add(thirdRow);
  130. fieldPanel.add(fourthRow);
  131. fieldPanel.add(fifthRow);
  132. fieldPanel.add(sixthRow);
  133. fieldPanel.add(seventhRow);
  134. fieldPanel.add(eighthRow);
  135.  
  136.  
  137. //Add button to panel
  138. buttonPanel.add(submitButton);
  139.  
  140. //Add panels to frame
  141. c.add(fieldPanel, BorderLayout.CENTER);
  142. c.add(buttonPanel, BorderLayout.SOUTH);
  143.  
  144. //Add functionality to buttons
  145. submitButton.addActionListener(this);
  146.  
  147. //get current date and open the file
  148. Date today = new Date();
  149. SimpleDateFormat myFormat = new SimpleDateFormat("MMddyy");
  150. String filename = "rolls" + myFormat.format(today);
  151.  
  152. try
  153. {
  154. output = new DataOutputStream(new FileOutputStream(filename));
  155. }
  156. catch(IOException io)
  157. {
  158. JOptionPane.showMessageDialog(null, "The program could not create a storage location. Please check the disk drive and the run the program again.","Error",JOptionPane.INFORMATION_MESSAGE);
  159.  
  160. System.exit(1);
  161. }
  162. addWindowListener(
  163. {
  164. public void windowClosing(WindowEvent e)
  165. {
  166. int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit and submit the file?", "File Submission",JOptionPane.YES_NO_OPTION);
  167. if (answer == JOptionPane.YES_OPTION)
  168. System.exit(0);
  169. }
  170. }
  171. );
  172.  
  173. }
  174. public void actionPerformed(ActionEvent e)
  175. {
  176. String arg = e.getActionCommand();
  177.  
  178. if (checkFields())
  179. {
  180. try
  181. {
  182. output.writeUTF(wall2Width.getText());
  183. output.writeUTF(wall2Heighth.getText());
  184. output.writeUTF(wall3Width.getText());
  185. output.writeUTF(wall3Heighth.getText());
  186. output.writeUTF(wall4Width.getText());
  187. output.writeUTF(wall4Heighth.getText());
  188. JOptionPane.showMessageDialog(null,"The wall dimensions have been saved.", "Submission Successful", JOptionPane.INFORMATION_MESSAGE);
  189. }
  190. catch(IOException c)
  191. {
  192. System.exit(1);
  193. }
  194. clearFields();
  195. }
  196. }
  197.  
  198. public boolean checkFields()
  199. {
  200. if ((wall2Width.getText().compareTo("")<1) ||
  201. (wall2Heighth.getText().compareTo("")<1) ||
  202. (wall3Width.getText().compareTo("")<1) ||
  203. (wall3Heighth.getText().compareTo("")<1) ||
  204. (wall4Width.getText().compareTo("")<1) ||
  205. (wall4Heighth.getText().compareTo("")<1))
  206. {
  207. JOptionPane.showMessageDialog(null, "You must complete all fields", "Data Entry Error", JOptionPane.WARNING_MESSAGE);
  208. return false;
  209. }
  210. else
  211. {
  212. widthWall2 = Integer.parseInteger(wall2Width.getText());
  213. heighthWall2 = Integer.parseInteger(wall2Heighth.getText());
  214. //wall2Area = widthWall2*heighthWall2;
  215. wall2Area = Integer.valueOf(wall2Width.getText()) * Integer.valueOf(wall2Heighth.getText());
  216. /*
  217.   widthWall3 = Integer.parseDouble(wall3Width.getText());
  218.   heighthWall3 = Integer.parseDouble(wall3Heighth.getText());
  219. wall3Area = widthWall3 * heighthWall3;
  220.  
  221.   widthWall4 = Integer.parseDouble(wall4Width.getText());
  222.   heighthWall4 = IntegerDouble.parseDouble(wall4Heighth.getText());
  223.   wall4Area = widthWall4 * heighthWall4;
  224.  
  225.   totalArea = wall1Area + wall2Area + wall3Area + wall4Area;
  226.   totalArea = totalArea + (totalArea*.10);
  227.  
  228.   totalRolls = totalArea/35;*/
  229. //return true;
  230. }
  231. }
  232.  
  233. public void clearFields()
  234. {
  235. //clear fields and reset the focus
  236. wall2Width.setText("");
  237. wall2Heighth.setText("");
  238. wall3Width.setText("");
  239. wall3Heighth.setText("");
  240. wall4Width.setText("");
  241. wall4Heighth.setText("");
  242. wall2Area.setText("");
  243. wall3Area.setText("");
  244. wall4Area.setText("");
  245. }
  246. }
  247.  
  248.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:17: error: class Midterm2 is public, should be declared in a file named Midterm2.java
public class Midterm2 extends JFrame implements ActionListener
       ^
Main.java:214: error: cannot find symbol
		    widthWall2 = Integer.parseInteger(wall2Width.getText());
		                        ^
  symbol:   method parseInteger(String)
  location: class Integer
Main.java:215: error: cannot find symbol
                    heighthWall2 = Integer.parseInteger(wall2Heighth.getText());
                                          ^
  symbol:   method parseInteger(String)
  location: class Integer
Main.java:217: error: incompatible types
		    wall2Area = Integer.valueOf(wall2Width.getText()) * Integer.valueOf(wall2Heighth.getText());
		                                                      ^
  required: JTextField
  found:    int
4 errors
stdout
Standard output is empty