From 09faf3c2ff4ffcb99592d808737b0b520a27ea69 Mon Sep 17 00:00:00 2001 From: Ziad Arafat Date: Mon, 1 May 2023 14:43:09 -0600 Subject: [PATCH] Created PA9 --- PA009/.gitignore | 29 ++++++++ PA009/Makefile | 25 +++++++ PA009/README.md | 8 +++ PA009/deliverables.md | 157 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 219 insertions(+) create mode 100644 PA009/.gitignore create mode 100644 PA009/Makefile create mode 100644 PA009/README.md create mode 100644 PA009/deliverables.md diff --git a/PA009/.gitignore b/PA009/.gitignore new file mode 100644 index 0000000..3904a57 --- /dev/null +++ b/PA009/.gitignore @@ -0,0 +1,29 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +replay_pid* +# Ignore Gradle project-specific cache directory +.gradle + +# Ignore Gradle build output directory +build diff --git a/PA009/Makefile b/PA009/Makefile new file mode 100644 index 0000000..a0a42bf --- /dev/null +++ b/PA009/Makefile @@ -0,0 +1,25 @@ +red=`tput setaf 1` +green=`tput setaf 2` +reset=`tput sgr0` + +# Variables so its easy to make changes if I need to (it ended up being helpful) + +all: test + +test: pa008/App.java pa008/Natural.java + @echo "${green} Building and executing Java code ${reset}" + javac -cp pa008/ pa008/*java + java -cp . pa008/App + +run_ada: grade_distribution.adb + @echo "${green} Building and executing Ada code ${reset}" + gcc -c grade_distribution.adb + gnatbind grade_distribution + gnatlink grade_distribution + ./grade_distribution + +clean: + rm -rf *.o + rm -rf *.ali + rm -rf grade_distribution + rm -rf pa008/*.class \ No newline at end of file diff --git a/PA009/README.md b/PA009/README.md new file mode 100644 index 0000000..1462df1 --- /dev/null +++ b/PA009/README.md @@ -0,0 +1,8 @@ + +## Usage +1. Build and execute the Java program + 1. `make` +2. Build and execute the Ada program. + 1. `make run_ada` +3. Clean all the generated files + 1. `make clean` \ No newline at end of file diff --git a/PA009/deliverables.md b/PA009/deliverables.md new file mode 100644 index 0000000..67d6fd9 --- /dev/null +++ b/PA009/deliverables.md @@ -0,0 +1,157 @@ +# PA008 + +## Deliverables + +### Name and problem description + +1. Ziad Arafat +2. This program demonstrates how exception handling works in different languages + 1. We translated a program written in Ada + 1. The program takes in test scores as inputs then prints out the frequency of each score range. + 2. The Ada was translated to java + 1. The java code does the same thing as the Ada code but with some changes that make it work well in Java. + +### Our new code + +#### App.java +This is the main code that replicates the Ada code. +```java + +/** + * Ziad Arafat + * 04/26/2023 + */ + +package pa008; + +import pa008.Natural; // Custom natural class for natural numbers from Ada. +import java.util.Scanner; + +/** + * Exception handling loop example translated from Ada. + * User inputs grade scores between 0 and 100 and the program counts + * the frequencies in each range in an array. + * It then prints out the frequencies in a table. + */ +public class App { + public static void GradeDistribution() + { + int[] Freq = new int[10]; // Array of integers initialized to 0 + + // Natural number to store the grade + Natural NewGrade = new Natural(0); + + // Index: Controls the index to count the score to. + // Limit_1, Limit_2 used to store the ranges in the output rable + int Index, Limit_1, Limit_2; + + // keep asking for a number until the user enters a negative. + while (true) { + + + Scanner GradeScanner = new Scanner(System.in); + + // Try to set the value to what the user entered. + // If it's not a natural number break the loop. + try { + NewGrade.setValue( + GradeScanner.nextInt()); + + } catch (IllegalArgumentException e) { + break; + } + + // Set the index to the grade value over 10 + // This allows us to update the appropriate category + // based on. + Index = NewGrade.getValue()/10; + + // Try to append the grade value. + // If it is out of bounds then either error or append + // it as the 100 grade + try { + Freq[Index] = Freq[Index] + 1; + } catch (ArrayIndexOutOfBoundsException e) { + if (NewGrade.getValue() == 100) { + Freq[9] = Freq[9] + 1; + } else { + System.out.print( + "Error -- new grade: "); + System.out.print(NewGrade.getValue()); + System.out.println(" is out of range"); + } + } + } + + // Print a table with all the frequencies. + System.out.println("Limits\tFrequency"); + + for (Index = 0; Index < 10; Index++) { + + // Sets the bounds of the range + Limit_1 = 10 * Index; + Limit_2 = Limit_1 + 9; + if (Index == 9) { + Limit_2 = 100; + } + + System.out.print(Limit_1); + System.out.print("-"); + System.out.print(Limit_2); + System.out.print("\t"); + System.out.println(Freq[Index]); + + } + } + + public static void main(String[] args) { + GradeDistribution(); + } +} +``` + +#### Natural.java +This class is used to implement the same functionality as Ada's Natural type. Because java does not have a native Natural type. It also implements the exception throws when a negative number is passed. + +```java +/** + * Ziad Arafat + * 04/26/2023 + */ + +package pa008; + + +/** + * Class to store natural numbers and throw exceptions equivalent to Adas + * Contraint errors that natural types can throw. + */ +public class Natural { + private int value; + + public Natural(int value) { + if (value < 0) { + throw new IllegalArgumentException( + "Value must be non-negative."); + } + this.value = value; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + if (value < 0) { + throw new IllegalArgumentException( + "Value must be non-negative."); + } + this.value = value; + } +} +``` + +### A test run of our code +This is the output of running `make` and giving a bunch of test inputs +Screenshot generated with https://carbon.now.sh +![Java Output](./javaoutput.png) \ No newline at end of file