View Single Post
Join Date: Sep 2008
Posts: 1,654
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Postal code program, need help to get in right direction

 
0
  #4
Nov 29th, 2008
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class PostOffice extends JFrame implements ActionListener
  6. {
  7. //This appears on the screen as a clickable button
  8. private JButton zipCodeButton;
  9. //This appears as two text fields where you can type
  10. private JTextField zipCodeField, barCodeField;
  11. //This appears as text, a label next to the zip code field
  12. private JLabel zipCodeLabel;
  13.  
  14. //This is the variable your professor has created to get input
  15. //from the GUI.
  16. private PostalZipCode myZip;
  17.  
  18. public static void main ()
  19. {
  20. //All of this code creates the GUI and shows it to you
  21. //You shouldn't be concerned with it, and you do not
  22. //need to alter it or add to it in any way.
  23. PostOffice myApplication = new PostOffice();
  24. myApplication.setSize(400,140);
  25. myApplication.setTitle("Zip Code");
  26. myApplication.createGUI();
  27. myApplication.setVisible(true);
  28. }
  29.  
  30. //Again, more code to create the GUI. You do not need to
  31. //change any of this code either
  32. private void createGUI()
  33. {
  34. setDefaultCloseOperation(EXIT_ON_CLOSE);
  35. Container window = getContentPane();
  36. window.setLayout(new FlowLayout());
  37.  
  38. zipCodeLabel = new JLabel(" Zip Code ");
  39. window.add(zipCodeLabel);
  40. zipCodeField = new JTextField(5);
  41. window.add(zipCodeField);
  42.  
  43. barCodeField = new JTextField(20);
  44. barCodeField.setHorizontalAlignment(JTextField.CENTER);
  45. barCodeField.setFont(new Font("Sans erif", Font.BOLD, 20));
  46.  
  47. window.add(barCodeField);
  48.  
  49. zipCodeButton = new JButton("Display Postal Bar Code");
  50. zipCodeButton.addActionListener(this);
  51. window.add(zipCodeButton);
  52. }
  53.  
  54. //The code that you are primarily concerned with is in here.
  55. public void actionPerformed (ActionEvent e)
  56. {
  57. //Your professor reads in the zip code
  58. String userInput = zipCodeField.getText();
  59. //Here, your professor takes a zip code, "userInput", and
  60. //calls a constructor that converts it to a PostalZipCode.
  61. //You need to write that constructor. Doing so involves creating a new class called PostalZipCode, writing a constructor called PostalZipCode that converts a Zip code to a PostalZipCode.
  62. myZip = new PostalZipCode(userInput);
  63. //In your PostalZipCode class, you need a method call getBarCode() that returns a String. The String should be the bar code.
  64. barCodeField.setText(myZip.getBarCode());
  65. }
  66. }


And here is an outline of what the class you are writing should look like and contain:

  1. public class PostalZipCode{
  2. String thisZipCode = "";
  3. String postalZipCode = "";
  4. String barCode = "";
  5.  
  6. public PostalZipCode(String zipCode){
  7. thisZipCode = zipCode;
  8. postalZipCode = convertZipcodeToPostal(zipCode);
  9. setBarCode();
  10. }
  11.  
  12. public String convertZipcodeToPostal(String aZip){
  13. String postalCode = "";
  14. //Do whatever you need to to aZip to convert it to a postalZipCode.
  15. return postalCode;
  16. }
  17.  
  18. public void setBarCode(){
  19. //write code that uses the zipCode to set the bar code
  20. }
  21.  
  22. public String getBarCode(){
  23. return barCode;
  24. }
  25.  
  26. }

You can compare Peter's code and my code to help you figure out how Java works. Both of our codes do the same thing.
Reply With Quote