Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this assignment (Java): this is the original assignment https:/

ID: 3669124 • Letter: I

Question

I need help with this assignment (Java): this is the original assignment https://www.chegg.com/homework-help/questions-and-answers/please-help-program-java-faster-sorting-letters-united-states-postal-service-encourages-co-q10589181

So far I have done zipcode to barcode conversion and it compiles well. But now I need to convert barcode to zipcode. It has to be done in "public String toZipCode (String bc)" method

Here is the code:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
*/
public class PostalBarCode extends javax.swing.JFrame {

/**
* Creates new form PostalBarCode
*/
public PostalBarCode() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {

zipLabel = new javax.swing.JLabel();
zipText = new javax.swing.JTextField();
barButton = new javax.swing.JButton();
clearButton = new javax.swing.JButton();
zipButton = new javax.swing.JButton();
barLabel = new javax.swing.JLabel();
barText = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

zipLabel.setText("Zip Code:");

zipText.setText("18512");

barButton.setText("Bar");
barButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
barButtonActionPerformed(evt);
}
});

clearButton.setText("Clear");
clearButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearButtonActionPerformed(evt);
}
});

zipButton.setText("Zip");
zipButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
zipButtonActionPerformed(evt);
}
});

barLabel.setText("Postal Bar Code:");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(111, 111, 111)
.addComponent(zipLabel))
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(zipText, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(barText, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(barButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(clearButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(zipButton)))))
.addContainerGap(53, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(barLabel)
.addGap(97, 97, 97))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(zipLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(zipText, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(barButton)
.addComponent(clearButton)
.addComponent(zipButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
.addComponent(barLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(barText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(19, 19, 19))
);

pack();
}//

private void barButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String barCodeText = toPostalBarCode(zipText.getText());
   barText.setText(barCodeText);
}   

private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {
barText.setText(""); // Write empty string to text fields
   zipText.setText("");
}   

private void zipButtonActionPerformed(java.awt.event.ActionEvent evt) {
String zipCodeText = toZipCode(barText.getText());
   zipText.setText(zipCodeText);
}   

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(PostalBarCode.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PostalBarCode.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PostalBarCode.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PostalBarCode.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PostalBarCode().setVisible(true);
}
});
  
}

// Variables declaration - do not modify   
private javax.swing.JButton barButton;
private javax.swing.JLabel barLabel;
private javax.swing.JTextField barText;
private javax.swing.JButton clearButton;
private javax.swing.JButton zipButton;
private javax.swing.JLabel zipLabel;
private javax.swing.JTextField zipText;
// End of variables declaration   

public String toZipCode (String bc)
{
  

  
return "toZipCode executing";
}

public String toPostalBarCode (String zc)
{
//create stringbuilder for creating returning postal barCode
StringBuilder postal = new StringBuilder();
int zipDigit =-1; // individual number for each zip digit
int checkDigitSum = 0;
String bcDigit; // postal barCode string for each digit
  
// Complete error checking
if ((zc.length() == 5) || (zc.length()==10 && zc.charAt(5)=='-'))
{
if (zc.length()==10) // remove dash
{
zc = zc.substring(0, 5) + zc.substring(6);
}
//examing each char in the string
for (int i = 0; i < zc.length(); i++)
{
//Digit to Bar Code Translation
switch (zc.charAt(i))
{
case '0': bcDigit = "||:::"; zipDigit = 0; break; // cannot user zc here ad you would overwrite the input value
case '1': bcDigit = ":::||"; zipDigit = 1;break;
case '2': bcDigit = "::|:|"; zipDigit = 2;break;
case '3': bcDigit = "::||:"; zipDigit = 3;break;
case '4': bcDigit = ":|::|"; zipDigit = 4;break;
case '5': bcDigit = ":|:|:"; zipDigit = 5;break;
case '6': bcDigit = ":||::"; zipDigit = 6;break;
case '7': bcDigit = "|:::|"; zipDigit = 7;break;
case '8': bcDigit = "|::|:"; zipDigit = 8;break;
case '9': bcDigit = "|:|::"; zipDigit = 9;break;
default:
return "Invalid Zip Code Character"; // if char not 0-9 the input zc is invalid
  
}
// add number to Check Digit Sum
checkDigitSum = checkDigitSum + zipDigit;

// append the postal barCode
postal.append(bcDigit);
}

  
return ("|" + postal.toString() + checkBar(checkDigitSum) +"|"); // convert the String builder to string and append frame bars
}
else
{
return "Invalid zip code";
}
}


public static String checkBar(int sum)
  
{   
  
int reminder = sum % 10;
int checkBar = 10 - reminder;
String checkBarString=""; // create string and initialize to empty string
  
switch (checkBar) // int value
{
case 0: checkBarString = "||:::"; break; // cannot user zc here ad you would overwrite the input value
case 1: checkBarString = ":::||"; break;
case 2: checkBarString = "::|:|"; break;
case 3: checkBarString = "::||:"; break;
case 4: checkBarString = ":|::|"; break;
case 5: checkBarString = ":|:|:"; break;
case 6: checkBarString = ":||::"; break;
case 7: checkBarString = "|:::|"; break;
case 8: checkBarString = "|::|:"; break;
case 9: checkBarString = "|:|::"; break;
  
  
}
return checkBarString;
}
}

Explanation / Answer

package zip;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
*/
public class PostalBarCode extends javax.swing.JFrame {
/**
* Creates new form PostalBarCode
*/
public PostalBarCode() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
zipLabel = new javax.swing.JLabel();
zipText = new javax.swing.JTextField();
barButton = new javax.swing.JButton();
clearButton = new javax.swing.JButton();
zipButton = new javax.swing.JButton();
barLabel = new javax.swing.JLabel();
barText = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
zipLabel.setText("Zip Code:");
zipText.setText("18512");
barButton.setText("Bar");
barButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
barButtonActionPerformed(evt);
}
});
clearButton.setText("Clear");
clearButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearButtonActionPerformed(evt);
}
});
zipButton.setText("Zip");
zipButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
zipButtonActionPerformed(evt);
}
});
barLabel.setText("Postal Bar Code:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(111, 111, 111)
.addComponent(zipLabel))
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(zipText, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(barText, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(barButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(clearButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(zipButton)))))
.addContainerGap(53, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(barLabel)
.addGap(97, 97, 97))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(zipLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(zipText, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(barButton)
.addComponent(clearButton)
.addComponent(zipButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
.addComponent(barLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(barText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(19, 19, 19))
);
pack();
}//
private void barButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String barCodeText = toPostalBarCode(zipText.getText());
barText.setText(barCodeText);
}   
private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {
barText.setText(""); // Write empty string to text fields
zipText.setText("");
}   
private void zipButtonActionPerformed(java.awt.event.ActionEvent evt) {
String zipCodeText = toZipCode(barText.getText());
zipText.setText(zipCodeText);
}   
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(PostalBarCode.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PostalBarCode.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PostalBarCode.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PostalBarCode.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PostalBarCode().setVisible(true);
}
});
  
}
// Variables declaration - do not modify   
private javax.swing.JButton barButton;
private javax.swing.JLabel barLabel;
private javax.swing.JTextField barText;
private javax.swing.JButton clearButton;
private javax.swing.JButton zipButton;
private javax.swing.JLabel zipLabel;
private javax.swing.JTextField zipText;
// End of variables declaration   
public String toZipCode (String bc)
{

//create stringbuilder for creating returning postal zipCode
StringBuilder zip = new StringBuilder();
//String zip;
//StringBuilder zipCode = new StringBuilder();
//int zipDigit =-1; // individual number for each zip digit
//int checkDigitSum = 0;
int bcCode=0; // zip Code string for each barCode 5 digit

  
// Complete error checking
if ((bc.length() == 32) || (bc.length()==52))
{

if (bc.length()==32) // remove full bars
{
bc = bc.substring(1, 31);
}
if (bc.length()==52) // remove full bars
{
bc = bc.substring(1, 51);
}

int count=0;
int zipcode=0;
//zip.setLength(0);
//examing each char in the string
for (int i = 0; i < ((bc.length())); i++)
{
if (count==5)
{
if(zipcode==11)
zipcode=0;
count=0;
zip.append(zipcode);
zipcode=0;
}
if (count==0 && bc.charAt(i)=='|')
zipcode=7+zipcode;
if (count==1 && bc.charAt(i)=='|')
zipcode=4+zipcode;
if (count==2 && bc.charAt(i)=='|')
zipcode=2+zipcode;
if (count==3 && bc.charAt(i)=='|')
zipcode=1+zipcode;
if (count==4 && bc.charAt(i)=='|')
zipcode=0+zipcode;
count=count+1;
}

  
/*{
zip=bc.substring(5*i,5*i+4);
if (zip=='||:::')
bcCode=0;
else
bcCode=1;
zipCode.append(bcCode);
}
*//*
for(int j=0; j <5; j++)
{
zip.append(bc.charAt(i*j));
}
//enum Pill p = Pill.valueOf(zip.toString());
//ValueEnum enumval = ValueEnum.fromString(zip.toString());
//switch(enumval) {
//switch (zip.toString())

case "||:::": bcCode = "0"; zipDigit = 0; break; // cannot user zc here ad you would overwrite the input value
case ":::||": bcCode = "1"; zipDigit = 1;break;
case "::|:|": bcCode = "2"; zipDigit = 2;break;
case "::||:": bcCode = "3"; zipDigit = 3;break;
case ":|::|": bcCode = "4"; zipDigit = 4;break;
case ":|:|:": bcCode = "5"; zipDigit = 5;break;
case ":||::": bcCode = "6"; zipDigit = 6;break;
case "|:::|": bcCode = "7"; zipDigit = 7;break;
case "|::|:": bcCode = "8"; zipDigit = 8;break;
case "|:|::": bcCode = "9"; zipDigit = 9;break;
default:
return "Invalid bar Code Character"; // if char not 0-9 the input zc is invalid
  
}

}
/*
//Digit to Bar Code Translation
switch (zc.charAt(i))
{
case '0': bcDigit = "||:::"; zipDigit = 0; break; // cannot user zc here ad you would overwrite the input value
case '1': bcDigit = ":::||"; zipDigit = 1;break;
case '2': bcDigit = "::|:|"; zipDigit = 2;break;
case '3': bcDigit = "::||:"; zipDigit = 3;break;
case '4': bcDigit = ":|::|"; zipDigit = 4;break;
case '5': bcDigit = ":|:|:"; zipDigit = 5;break;
case '6': bcDigit = ":||::"; zipDigit = 6;break;
case '7': bcDigit = "|:::|"; zipDigit = 7;break;
case '8': bcDigit = "|::|:"; zipDigit = 8;break;
case '9': bcDigit = "|:|::"; zipDigit = 9;break;
default:
return "Invalid Zip Code Character"; // if char not 0-9 the input zc is invalid
  
}
// add number to Check Digit Sum
checkDigitSum = checkDigitSum + zipDigit;

// append the postal barCode
postal.append(bcDigit);
}

  
return ("|" + postal.toString() + checkBar(checkDigitSum) +"|"); // convert the String builder to string and append frame bars
}
else
{
return "Invalid zip code";
}
*/
  
return zip.toString();
}
else
return "barcode invalid";
}

public String toPostalBarCode (String zc)
{
//create stringbuilder for creating returning postal barCode
StringBuilder postal = new StringBuilder();
int zipDigit =-1; // individual number for each zip digit
int checkDigitSum = 0;
String bcDigit; // postal barCode string for each digit
  
// Complete error checking
if ((zc.length() == 5) || (zc.length()==10 && zc.charAt(5)=='-'))
{
if (zc.length()==10) // remove dash
{
zc = zc.substring(0, 5) + zc.substring(6);
}
//examing each char in the string
for (int i = 0; i < zc.length(); i++)
{
//Digit to Bar Code Translation
switch (zc.charAt(i))
{
case '0': bcDigit = "||:::"; zipDigit = 0; break; // cannot user zc here ad you would overwrite the input value
case '1': bcDigit = ":::||"; zipDigit = 1;break;
case '2': bcDigit = "::|:|"; zipDigit = 2;break;
case '3': bcDigit = "::||:"; zipDigit = 3;break;
case '4': bcDigit = ":|::|"; zipDigit = 4;break;
case '5': bcDigit = ":|:|:"; zipDigit = 5;break;
case '6': bcDigit = ":||::"; zipDigit = 6;break;
case '7': bcDigit = "|:::|"; zipDigit = 7;break;
case '8': bcDigit = "|::|:"; zipDigit = 8;break;
case '9': bcDigit = "|:|::"; zipDigit = 9;break;
default:
return "Invalid Zip Code Character"; // if char not 0-9 the input zc is invalid
  
}
// add number to Check Digit Sum
checkDigitSum = checkDigitSum + zipDigit;

// append the postal barCode
postal.append(bcDigit);
}

  
return ("|" + postal.toString() + checkBar(checkDigitSum) +"|"); // convert the String builder to string and append frame bars
}
else
{
return "Invalid zip code";
}
}

public static String checkBar(int sum)
  
{   
  
int reminder = sum % 10;
int checkBar = 10 - reminder;
String checkBarString=""; // create string and initialize to empty string
  
switch (checkBar) // int value
{
case 0: checkBarString = "||:::"; break; // cannot user zc here ad you would overwrite the input value
case 1: checkBarString = ":::||"; break;
case 2: checkBarString = "::|:|"; break;
case 3: checkBarString = "::||:"; break;
case 4: checkBarString = ":|::|"; break;
case 5: checkBarString = ":|:|:"; break;
case 6: checkBarString = ":||::"; break;
case 7: checkBarString = "|:::|"; break;
case 8: checkBarString = "|::|:"; break;
case 9: checkBarString = "|:|::"; break;
  
  
}
return checkBarString;
}


}