Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C# Write a ZipCode class using Main. Your ZipCode class will encode and decode five digit bar codes used by the US Postal Service

In C#

Write a ZipCode class using Main. Your ZipCode class will encode and decode five digit bar codes used by the US Postal Service on envelopes. The class should have two constructors.

The first constructor should input the zip code as an integer o If it has less than 5 digits, just assume that any missing digits are leading 0s o If it has more than 5 digits display an error message.

The second constructor should input the zip code as a bar code string consisting of 0s and 1s as described above. o It should check for various errors and report them. The output sample illustrates several errors that must be reported.

Although you have two ways to input the zip code, internally the class must only store the zip code as an integer value. The bar-code representation can always be reconstructed from the integer value.

TheZipCode class should also have a public method, GetBarCode() to return the zip code as a properly constructed string of binary digits.Or else report a specific error.

This class should use a Property for zip similar to the treatment of balance in Account.cs (Please see Startup Notes 4). Notice in this implementation that it is easily possible to check that the zipcode value is no more than 5 digits. (The Property feature of C# avoids the need for extra methods such as GetZip and SetZip (And similar method names if there are additional member variables of classes.) This feature is not \ available in Java.

You also need a private method ParseBarCode()to decode the string of binary digits and return the bar code value as an int. This is the most complicated method of the assignment

Main

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

// ********************* MAIN METHOD ********************* public class ZipTest { public static void Main() { Console.WriteLine("Required Output for Zipcode Testing"); ZipCode zip1 = new ZipCode(995047), zip2 = new ZipCode("100101010011100001100110001"), zip3 = new ZipCode(12345), zip4 = new ZipCode(67890);

Console.WriteLine("{0}'s bar code is '{1}'", zip1.Zip, zip1.GetBarCode()); Console.WriteLine("{0}'s bar code is '{1}'", zip2.Zip, zip2.GetBarCode()); Console.WriteLine("{0}'s bar code is '{1}'", zip3.Zip, zip3.GetBarCode()); Console.WriteLine("{0}'s bar code is '{1}'", zip4.Zip, zip4.GetBarCode());

// Test a range of values by first constructing a zip code with // an integer, then retrieving the bar code and using that to // construct another ZipCode. int zip_int = 0; for (int i = 0; i < 25; i++) { // Make an aribrary 5-digit zip code integer, and use it // to construct a ZipCode int five_digit_zip = (zip_int * zip_int) % 100000; ZipCode z1 = new ZipCode(five_digit_zip);

// Construct a next ZipCode from the current one's bar code String z1_code = z1.GetBarCode(); ZipCode z2 = new ZipCode(z1_code);

Console.Write(" {0:D2}: {1:D5} has code '{2}'", i + 1, z2.Zip, z1_code); if ((z1_code == z2.GetBarCode()) && (z1.Zip == z2.Zip)/* && (z2.Zip == five_digit_zip)*/) { Console.WriteLine(" [OK]"); } else { Console.WriteLine(" [ERR]"); }

// Increment the test value arbitrarily zip_int += (233 + zip_int % 7); } // end for

Console.WriteLine(); Console.ReadKey(); // Test some error conditions. This test assumes that // ZipCode will simply print an error message to Console // and will not exit the program. Console.WriteLine(" Initialize an array of bar codes with known errors:"); int BAD_ZIP_COUNT = 5; // Use 2-D array of strings for tests. String[,] badZips = { { "10010101001110000110011001", "bad length" }, { "000101010011100001100110001", "bad start/end character" }, { "100101010011100001100110000", "bad start/end character" }, { "100104010011100001100110021", "bad digit" }, { "100101010011100001100111001", "bad sequence" } }; for (int i = 0; i < BAD_ZIP_COUNT; i++) { Console.WriteLine("Testing {0} for {1}", badZips[i, 0], badZips[i, 1]); // Construct ZipCode object and it will detect errors ZipCode testZip = new ZipCode(badZips[i, 0]); Console.WriteLine(); Console.ReadKey(); }

} //************** END OF main() ************ } // end ZipTest class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2008 Comprehensive Concepts And Techniques

Authors: Gary B. Shelly, Corinne Hoisington

1st Edition

1423927168, 978-1423927167

More Books

Students also viewed these Databases questions

Question

3. Develop a case study.

Answered: 1 week ago