15-110 Introduction to Programming [Reid-Miller]
LAB 0 [Introduction to Java]
Objective
- To introduce you to how zip codes are converted to postal bar codes.
- To write a simple Java program in Eclipse that uses static methods.
- To get acquainted with submitting a lab to the Blackboard automated handin.
Postal Bar Codes
The U.S. Post Office speeds its delivery of mail by
the use of a postal bar code. Each digit of the 5-digit zip code
is encoded using one of the 10 patterns shown below:
Number Bar code Number Bar code
----------------- ------------------
0 ||... 5 .|.|.
1 ...|| 6 .||..
2 ..|.| 7 |...|
3 ..||. 8 |..|.
4 .|..| 9 |.|..
| represents a tall bar
. represents a short bar
For example, if your zip code is 15213, then the bar code that should be
generated is:
...||.|.|...|.|...||..||.
Exercise
- Following the documentation Introduction to Eclipse, write a simple
Java program in a project named Lab0 that contains a class
named BarCodeGenerator. (Remember when you make the class to
check the button to include a main method.) The main method
should display 1) the numeric zipcode of your current home address (if you live
on campus, your zipcode is 15213) and 2) its postal bar code. Use MySecondProgram on the Handouts page as model for your program.
- Start by writing a comment at the top of your program (above the class definition) that
includes your name, andrewID, section, and a statement of what your
program will do.
- Next, after the main method, write (at most 5) simple
static methods to print out the bar code pattern for each digit that
you need (one pattern per method). For example, one method might be
named displayCode1 and prints out ...|| on the
console.
- Then use your main method to print out your zipcode on
one line and your postal bar codes on the following line. To print
your bar codes call the static methods you wrote above in the correct
order.
Additional Exercises (if you have time)
- Once you get the program above to work correctly, write a second
version of your program in your Lab0 project by adding a
second class named BarCodeGenerator2, also with a
main method. For this part use MyFirstProgram in the Eclipse
documentation as your model.
- In the main method
define a String variable for each bar code pattern you need.
- Again, print out your zipcode and bar code using these variables.
- Finally, include a comment at the top of the class that answers the
following reflective questions.
- Which version of the program, in you opinion is better, and why?
- Suppose you wanted to display the bar code using graphics so that
it looks like real bar codes. To make a graphical display would require
calling multiple methods for each bar code pattern. Which version of
the program would be easier to modify to use graphics and why?
- If you still have time, modify either class so that it contains
either 10 static methods (besides main) or 10 variables, as
appropriate, for each bar code pattern, one for each digit. Real US
postal barcodes not only includes codes for the five digit zipcode but
additional codes and check codes. To find out this additional
coding go to the web page support.microsoft.com/kb/214181.
Modify your program so that the main method prints out the following
address* and its correct postal bar code:
Homer J. Simpson
742 Evergreen Terrace
Springfield, OR 97477-1357
The postal bar code for this address should be:
||.|..|...|.|..||...||...|...||..||..|.|.|...|.|..|..|.|.|..||
*DISCLAIMER: There is no evidence from The Simpsons that suggests that
Homer Simpson really lives in Oregon.
When You're Done...
Click the following link for instructions on how to hand in your lab
electronically to Blackboard:
Electronic Handin Instructions
DO NOT ERASE YOUR LAB PROJECT FOLDER, IN CASE OF A SUBMISSION ERROR.