943,955 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1068
  • Java RSS
Nov 29th, 2008
0

Postal code program, need help to get in right direction

Expand Post »
hi so i have just joined today but if i had known about this site i would have joined much sooner, i asked if anyone could help me with how i should post in the intro but dont wanna wait a day for a response so if i make any mistakes just let me know and ill change for the future. My teacher is asking me to create a barcode program that takes a zipcode inputed by the user and changed into a postal barcode. he has given me a starting point and although i know im suppose to create a construct i have had trouble creating it and getting variables across classes so if anyone could tell me how to begin the construct and maybe even the next few steps in vague detail to get me in the right direction it would be a life savior.
requirements are Implement a class named PostalZipCode that; Has two instance variables (zipCode & barCode) of type String,
Has a constructor that accepts a String input parameter for the, zipCode and determines and sets the barCode
Has a method called getBarCode that returns the barCode String
(theres other information such as which charecters to use for the digits and also i need to add a check digit system but i should be able to work something out for that with a little research once i get the basis for the program)
sorry if i do this wrong.
this is what i have been given:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PostOffice extends JFrame implements ActionListener
{
private JButton zipCodeButton;
private JTextField zipCodeField, barCodeField;
private JLabel zipCodeLabel;

private PostalZipCode myZip;

public static void main ()
{
PostOffice myApplication = new PostOffice();
myApplication.setSize(400,140);
myApplication.setTitle("Zip Code");
myApplication.createGUI();
myApplication.setVisible(true);
}

private void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());

zipCodeLabel = new JLabel(" Zip Code ");
window.add(zipCodeLabel);
zipCodeField = new JTextField(5);
window.add(zipCodeField);

barCodeField = new JTextField(20);
barCodeField.setHorizontalAlignment(JTextField.CENTER);
barCodeField.setFont(new Font("Sans erif", Font.BOLD, 20));

window.add(barCodeField);

zipCodeButton = new JButton("Display Postal Bar Code");
zipCodeButton.addActionListener(this);
window.add(zipCodeButton);
}

public void actionPerformed (ActionEvent e)
{
String userInput = zipCodeField.getText();
myZip = new PostalZipCode(userInput);
barCodeField.setText(myZip.getBarCode());
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
littlebean21289 is offline Offline
4 posts
since Nov 2008
Nov 29th, 2008
0

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

Java Syntax (Toggle Plain Text)
  1. public class PostalZipCode
  2. {
  3. private String barCode;
  4.  
  5. public PostalZipCode(String zipCode)
  6. {
  7. determinateBarCode(zipCode);
  8. }
  9.  
  10. private void determinateBarCode(String zipCode)
  11. {
  12. //do what ever calculation you need to generate barCode
  13. setBarCode(GENERATED_STRING_FOR_BARCODE_HERE);
  14. }
  15.  
  16. private void setBarCode(String str)
  17. {
  18. barCode = str;
  19. }
  20.  
  21. public void getBarCode()
  22. {
  23. return barCode;
  24. }
  25. }

PS: Please use code tags in the future to post any size of code [code]YOUR CODE HERE[/code]
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Nov 29th, 2008
0

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

Your professor gave you whats called a GUI. A GUI is just a window that, in this case, provides the user with a way to enter data. Now, to elaborate on that idea, I'll comment some of your professor's code to help you understand what he wants you to do.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 29th, 2008
0

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

Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 30th, 2008
0

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

Thanks to both of you. This information has already helped me out tremendously in understanding whats being asked of me. I'll be sure to use the code tags from now on.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
littlebean21289 is offline Offline
4 posts
since Nov 2008

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: file splitter
Next Thread in Java Forum Timeline: inorder traverse





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


Follow us on Twitter


© 2011 DaniWeb® LLC