Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
diegogarciacs authored Oct 30, 2021
1 parent 798d866 commit 15d4b29
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 0 deletions.
Binary file added Lab 2/CECS277-Lab2.pdf
Binary file not shown.
Binary file added Lab 2/Group1L2.zip
Binary file not shown.
11 changes: 11 additions & 0 deletions Lab 2/Lab 2.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
133 changes: 133 additions & 0 deletions Lab 2/src/CheckInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import java.util.Scanner;

/**
* Static functions used to check console input for validity.
*
* Use: Place CheckInput class in the same project folder as your code.
* Call CheckInput functions from your code using "CheckInput."
*
* Example: int num = CheckInput.getInt();
*
* @author Shannon Cleary 2020
*/
public class CheckInput {

/**
* Checks if the inputted value is an integer.
* @return the valid input.
*/
public static int getInt() {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
valid = true;
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}

/**
* Checks if the inputted value is an integer and
* within the specified range (ex: 1-10)
* @param low lower bound of the range.
* @param high upper bound of the range.
* @return the valid input.
*/
public static int getIntRange( int low, int high ) {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
if( input <= high && input >= low ) {
valid = true;
} else {
System.out.println( "Invalid Range." );
}
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}
/**
* Checks if the inputted value is a non-negative integer.
* @return the valid input.
*/
public static int getPositiveInt( ) {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
if( input >= 0 ) {
valid = true;
} else {
System.out.println( "Invalid Range." );
}
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}

/**
* Checks if the inputted value is a double.
* @return the valid input.
*/
public static double getDouble() {
Scanner in = new Scanner( System.in );
double input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextDouble() ) {
input = in.nextDouble();
valid = true;
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}

/**
* Takes in a string from the user.
* @return the inputted String.
*/
public static String getString() {
Scanner in = new Scanner( System.in );
String input = in.nextLine();
return input;
}

/**
* Takes in a yes/no from the user.
* @return true if yes, false if no.
*/
public static boolean getYesNo(){
boolean valid = false;
while( !valid ) {
String s = getString();
if( s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y") ) {
return true;
} else if( s.equalsIgnoreCase("no") || s.equalsIgnoreCase("n") ) {
return false;
} else {
System.out.println( "Invalid Input." );
}
}
return false;
}
}
82 changes: 82 additions & 0 deletions Lab 2/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Name: Diego Garcia, Erwin Pan
Date: 9/2/2021
Description: Postal Bar Code Printer
*/
import java.util.Scanner;
class Main {
public static void main(String[] args) {
String zip = getZip(); // calls getZip and stores zip code within zip
printBarCode(zip); // prints the bar code of inputted zip code
}
/**
Takes input of the zip code and check if it's within the range[10000-99999].
@return zip returns zip code in string format.
*/
public static String getZip(){
Scanner in = new Scanner(System.in); //creates scanner
System.out.print("Enter a zip code: "); // prompt
int zip = CheckInput.getIntRange(10000,99999); // checks for input in valid range
return String.valueOf(zip); // returns the zip in string format
}
/**
Calculates Check Digit.
@param zip zip code to be used to create the check digit.
@return checked returns character that corresponds to the integer of the check digit.
*/
public static char getCheckDigit(String zip){
int sum = 0; // prime accumulator
for (int i = 0; i < zip.length(); i++){
char charNum = zip.charAt(i); // obtain char at i
int num = Character.getNumericValue(charNum); // convert char to int and store in num
sum = sum + num; // add to accumulator
}
int rounded = (int)(Math.ceil((double)sum/10.0))*10; // converts sum to double, then back to int after rounding up to nearest 10
//int rounded = ((int)Math.ceil((double)sum/10))*10;
int checkInt = rounded - sum; // sub rounded from sum
char checkChar = (char)(48+checkInt); // convert checkInt to char with respect to ASCII
return checkChar; // return char to be used for bar code
}
/**
Prints out barcode given by the zip code.
@param zip zip code given by the user.
*/
public static void printBarCode(String zip){
System.out.print("|"); //prints the start of the frame bar.
for (int i = 0; i < zip.length(); i++){
char d = zip.charAt(i); //store char at i position of zip.
printDigit(d); // send char to printdigit to be printed
}
char checkDigit = getCheckDigit(zip); // generate check digit and store in char
printDigit(checkDigit); // print check digit
System.out.print("|"); //prints the end of the frame bar.
}
/**
* Prints the barcode corresponding to the given char.
* @param d the character that will be used to print the barcode.
*/
public static void printDigit(char d){
if (d == '1'){
System.out.print("...||");
} else if (d == '2'){
System.out.print("..|.|");
} else if (d == '3'){
System.out.print("..||.");
} else if (d == '4'){
System.out.print(".|..|");
} else if (d == '5'){
System.out.print(".|.|.");
} else if (d == '6'){
System.out.print(".||..");
} else if (d == '7'){
System.out.print("|...|");
} else if (d == '8'){
System.out.print("|..|.");
} else if(d == '9'){
System.out.print("|.|..");
} else if (d == '0'){
System.out.print("||...");
}
}

}

0 comments on commit 15d4b29

Please sign in to comment.