2016-12-07

posted Dec 7, 2016, 6:33 AM by Samuel Konstantinovich   [ updated Dec 7, 2016, 11:35 AM ]
Create an data type to represent Postal Bar Codes:

On git: MKS21X/03Barcode. Read the description, discuss as needed.

A bar code is generated from a 5 digit zip code by representing each digit with
a combination of 3 half bars and 2 full bars. Note that leading 0's are allowed
for zip codes. 

Represent a full bar with "|" and a half bar with ":".

To represent each of the 10 digits uniquely, the codes use the following scheme:
1  :::||   
2  ::|:|
3  ::||:
4  :|::|
5  :|:|:
6  :||::
7  |:::|
8  |::|:
9  |:|::
0  ||:::   

Hint: a switch statement is your friend here. It is optional, however.

A check digit is added to the right of a zip code. The check digit is equal
to the sum of the digits of the zip code modulo 10.

For example,
   if the zip code is : 08451
   then the check digit is : 8
   thus the zip + check digit is : 084518

A bar code has a 2 guard rails (full bars) that lead and end the code.

So the bar code of 084518 is:

|||:::|::|::|::|:|:|::::|||::|:|      



Implement the following constructors and methods. You may also write any additional private methods you wish.

public class Barcode implements Comparable<Barcode>{
// instance variables
   private String _zip;
   private int _checkDigit;

// constructors
//precondtion: _zip.length() = 5 and zip contains only digits.
//postcondition: throws a runtime exception zip is not the correct length
//               or zip contains a non digit
//               _zip and _checkDigit are initialized.
  public Barcode(String zip) {}

// postcondition: Creates a copy of a bar code.
  public Barcode clone(){}


// postcondition: computes and returns the check sum for _zip
  private int checkSum(){}

//postcondition: format zip + check digit + Barcode 
//ex. "084518  |||:::|::|::|::|:|:|::::|||::|:|"      
  public String toString(){}


// postcondition: compares the zip + checkdigit, in numerical order. 
  public int compareTo(Barcode other){}
    
}

Write test cases:
Several good cases 
too long
invalid characters
Comments