Java: finish code so it compiles; already commented as to what to do. Client alr
ID: 3596176 • Letter: J
Question
Java: finish code so it compiles; already commented as to what to do. Client already complete and posted as well. when finished, compile both and make sure it produces the correct output.
/**
* SERVICE CLASS Translate ZIP code into its bar code representation
*
* @author YOUR NAME
* @version 10/14/2017
*/
public class PostalCode
{
/**
* Instance variable. <BR>
* Contains the zipCode in a DDDDD-DDDD format
* (for example 12345-6789)
*/
private String zipCode;
/**
* Secondary constructor.<BR>
* Allows user to provide the value of the zipCode
*
* @param zipCode initial value of the zipCode
*/
public PostalCode(String zipCode)
{
setZipCode(zipCode);
}
/**
* Mutator method.<BR>
* Sets the value of a zipCode to the given new value
*
* @param zipCode
*/
public void setZipCode(String zipCode)
{
this.zipCode = zipCode;
}
/**
* method that converts the zipCode into the bar code format
*/
public StringBuilder computeBarCode()
{
// TODO Project 1
// IMPLEMENT THIS METHOD
// 1. first remove all '-' for easy processing by calling replaceAll
// 2. calculate the checksum by calling the private calculateChecksum method
// 3. calculate the check digit by calling the private calculateCheckDigit method
// 4. append the check digit to the zip string for final processing
// 5. produce the bar code and save it in barCode variable
// - check java API for description of StringBuilder append method
StringBuilder barCode = new StringBuilder("|");
// use a for loop and a switch inside the loop
return barCode;
}
/**
* Business method that calculates the checksum for the given zipCode.
*
* @param zipCodeDigits the zipCode without '-'
* @return checkSum
*/
private int calculateChecksum(String zipCodeDigits)
{
// TODO Project 1
int checkSum = 0;
// IMPLEMENT THIS METHOD
// HINT: to get the numeric value of each character
// utilize Character.digit(zipCodeDigits.charAt(i),10) method inside
// a for loop which runs over zipCodeDigits string
return checkSum;
}
/**
* Business method that calculates the check digit.
*
* @param checkSum sum of the digits in the zip code
* @return check digit
*/
private int calculateCheckDigit(int checkSum)
{
// TODO Project 1
int checkDigit = 0;
// IMPLEMENT THIS METHOD
return checkDigit;
}
/**
* Accessor method.
*
* @return the instance variable zipCode
*/
public String getZipCode()
{
return this.zipCode;
}
/**
* toString method.
*
* @return the string representation of the PostalCode object
*/
public String toString()
{
return "The zip code: " + this.zipCode + " has the following bar code: " + computeBarCode();
}
}
/**
* CLIENT CLASS Translate ZIP code into its bar code representation
*
* @author Anna Bieszczad
* @version 10/14/2017
*/
import java.util.*;
public class PostalCodeClient
{
public static void main(String[] args)
{
final String VALID_AREA_CODE = "[0-9]{5}[-][0-9]{4}";
String zipInputStr;
boolean valid;
String answer;
//get and validate user's input
Scanner keyboard = new Scanner(System.in);
do
{
do
{
System.out.println("Enter a ZIP code in the form DDDDD-DDDD");
zipInputStr = keyboard.nextLine();
valid = zipInputStr.matches(VALID_AREA_CODE);
if (!valid)
{
System.out.println("Your input is invalid, please re-enter");
}
} while (!valid);
// create postalCode object
PostalCode postalCode = new PostalCode(zipInputStr);
// display the zipCode and its barCode
System.out.println(postalCode);
// check if the user wants to convert another zipCode
System.out.println();
System.out.println("Would you like to generate another bar code?");
answer = keyboard.nextLine();
} while (answer.equalsIgnoreCase("yes"));
System.out.println("Goodbye!");
}
}
Explanation / Answer
Please find the Service class:
Checksum is calculated as sum of digits of code and checkdigit is calculated as immediate multiple of 10 of checksum/10-checksum
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.