| | |
Postal code program, need help to get in right direction
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2008
Posts: 4
Reputation:
Solved Threads: 0
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());
}
}
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());
}
}
Java Syntax (Toggle Plain Text)
public class PostalZipCode { private String barCode; public PostalZipCode(String zipCode) { determinateBarCode(zipCode); } private void determinateBarCode(String zipCode) { //do what ever calculation you need to generate barCode setBarCode(GENERATED_STRING_FOR_BARCODE_HERE); } private void setBarCode(String str) { barCode = str; } public void getBarCode() { return barCode; } }
PS: Please use code tags in the future to post any size of code [code]YOUR CODE HERE[/code]
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Sep 2008
Posts: 1,627
Reputation:
Solved Threads: 205
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PostOffice extends JFrame implements ActionListener { //This appears on the screen as a clickable button private JButton zipCodeButton; //This appears as two text fields where you can type private JTextField zipCodeField, barCodeField; //This appears as text, a label next to the zip code field private JLabel zipCodeLabel; //This is the variable your professor has created to get input //from the GUI. private PostalZipCode myZip; public static void main () { //All of this code creates the GUI and shows it to you //You shouldn't be concerned with it, and you do not //need to alter it or add to it in any way. PostOffice myApplication = new PostOffice(); myApplication.setSize(400,140); myApplication.setTitle("Zip Code"); myApplication.createGUI(); myApplication.setVisible(true); } //Again, more code to create the GUI. You do not need to //change any of this code either 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); } //The code that you are primarily concerned with is in here. public void actionPerformed (ActionEvent e) { //Your professor reads in the zip code String userInput = zipCodeField.getText(); //Here, your professor takes a zip code, "userInput", and //calls a constructor that converts it to a PostalZipCode. //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. myZip = new PostalZipCode(userInput); //In your PostalZipCode class, you need a method call getBarCode() that returns a String. The String should be the bar code. barCodeField.setText(myZip.getBarCode()); } }
And here is an outline of what the class you are writing should look like and contain:
Java Syntax (Toggle Plain Text)
public class PostalZipCode{ String thisZipCode = ""; String postalZipCode = ""; String barCode = ""; public PostalZipCode(String zipCode){ thisZipCode = zipCode; postalZipCode = convertZipcodeToPostal(zipCode); setBarCode(); } public String convertZipcodeToPostal(String aZip){ String postalCode = ""; //Do whatever you need to to aZip to convert it to a postalZipCode. return postalCode; } public void setBarCode(){ //write code that uses the zipCode to set the bar code } public String getBarCode(){ return barCode; } }
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.
![]() |
Other Threads in the Java Forum
- Previous Thread: file splitter
- Next Thread: inorder traverse
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arc arguments array arrays automation binary bluetooth c++ chat class classes client code codesnippet component csv database doctype draw ebook eclipse error event exception fractal freeze game givemetehcodez graphics gui html ide image input integer intellij iphone j2me java java.xls javaprojects jmf jni jpanel julia linux list loop loops mac map method methods mobile netbeans newbie number online oracle page parameter plazmic print problem program programming project recursion reporting rotatetext scanner screen sell server set size sms socket sort sourcelabs sql string superclass swing system template test testautomation threads time title tree tutorial-sample windows working






