added old projects
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Austin Bennett
|
||||
* App.java
|
||||
* Project 2
|
||||
* This has the main method and handles the menu
|
||||
*/
|
||||
|
||||
package Project2;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
// initialize the list and the scanner
|
||||
ShoppingCart shoppingList = new ShoppingCart();
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
// show the options initially
|
||||
shoppingList.PrintOptions();
|
||||
|
||||
// Main loop
|
||||
while (true) {
|
||||
|
||||
// Get input from user
|
||||
System.out.print("\nWhat would you like to do? (Type 8 to view options again): ");
|
||||
String input;
|
||||
input = sc.nextLine();
|
||||
|
||||
switch (input) { // switch method to run menu. using strings to prevent issues with user entering
|
||||
// possible wrong type
|
||||
case "1": // Print Items
|
||||
shoppingList.print();
|
||||
break;
|
||||
case "2": // Add single item
|
||||
shoppingList.addItem();
|
||||
break;
|
||||
case "3": // Add multiple items
|
||||
shoppingList.addMultiple();
|
||||
break;
|
||||
case "4": // remove item
|
||||
shoppingList.removeItem();
|
||||
break;
|
||||
case "5": // sort by name
|
||||
shoppingList.sortName();
|
||||
break;
|
||||
case "6": // sort by cost
|
||||
shoppingList.sortCost();
|
||||
break;
|
||||
case "7": // search the list for item
|
||||
shoppingList.search();
|
||||
break;
|
||||
case "8": // print the options again
|
||||
shoppingList.PrintOptions();
|
||||
break;
|
||||
case "9": // Checkout
|
||||
shoppingList.checkout();
|
||||
System.exit(0);
|
||||
default: // used if user inputs a non valid choice
|
||||
System.out.println("Invalid Choice");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Austin Bennett
|
||||
* Item.java
|
||||
* Project 2
|
||||
* This provides a class for the items to add to the shopping cart
|
||||
*/
|
||||
|
||||
package Project2;
|
||||
|
||||
public class Item { // used to hold the item name and the item cost
|
||||
public String _name;
|
||||
public Double _cost;
|
||||
|
||||
public Item(String name, double cost) { // constuctor
|
||||
_name = name;
|
||||
_cost = cost;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Austin Bennett
|
||||
* LinearSearch.java
|
||||
* Project 2
|
||||
* This handles searching the list
|
||||
*/
|
||||
|
||||
package Project2;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class LinearSearch {
|
||||
// runs through all elements until it find the one it's looking for
|
||||
static public int search(List<Item> list, String input) { // O(n)
|
||||
int index = -1;
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (list.get(i)._name.toLowerCase().equals(input.toLowerCase())) {
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Austin Bennett
|
||||
* MergeSort.java
|
||||
* Project 2
|
||||
* This handles sorting the list
|
||||
*/
|
||||
|
||||
package Project2;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class MergeSort {
|
||||
public static void sort(List<Item> list, int sortBy) {
|
||||
if (list.size() < 2) { // check to make sure that list can be sorted
|
||||
return;
|
||||
}
|
||||
int mid = list.size() / 2; // Getting the midpoint
|
||||
List<Item> left = new ArrayList<Item>(list.subList(0, mid)); // left sub list
|
||||
List<Item> right = new ArrayList<Item>(list.subList(mid, list.size())); // right sub list
|
||||
|
||||
// recursive sorting for left and right trees until the list size is 1
|
||||
sort(left, sortBy);
|
||||
sort(right, sortBy);
|
||||
|
||||
merge(left, right, list, sortBy);
|
||||
}
|
||||
|
||||
// merges the l and r lists
|
||||
private static void merge(
|
||||
List<Item> left, List<Item> right, List<Item> list, int sortBy) {
|
||||
int lIndex = 0;
|
||||
int rIndex = 0;
|
||||
int listIndex = 0;
|
||||
|
||||
// sorting
|
||||
while (lIndex < left.size() && rIndex < right.size()) {
|
||||
|
||||
// if sortBy is 1 -> sort by name
|
||||
if (sortBy == 1) {
|
||||
if (left.get(lIndex)._name.compareTo(right.get(rIndex)._name) < 0) {
|
||||
list.set(listIndex++, left.get(lIndex++)); // sets the new element
|
||||
} else {
|
||||
list.set(listIndex++, right.get(rIndex++)); // sets the new element
|
||||
}
|
||||
}
|
||||
// if sortBy is 2 -> sort by cost
|
||||
if (sortBy == 2) {
|
||||
if (left.get(lIndex)._cost.compareTo(right.get(rIndex)._cost) < 0) {
|
||||
list.set(listIndex++, left.get(lIndex++)); // sets the new element
|
||||
} else {
|
||||
list.set(listIndex++, right.get(rIndex++)); // sets the new element
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (lIndex < left.size()) {
|
||||
list.set(listIndex++, left.get(lIndex++)); // sets the new element
|
||||
}
|
||||
while (rIndex < right.size()) {
|
||||
list.set(listIndex++, right.get(rIndex++)); // sets the new element
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Austin Bennett
|
||||
* ShoppingList.java
|
||||
* Project 2
|
||||
* This has all the methods for about everything in the project
|
||||
*/
|
||||
|
||||
package Project2;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ShoppingCart {
|
||||
// initialize the variables and scanner
|
||||
List<Item> list = new ArrayList<Item>();
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
// prints all the items in the list exactly how they were ordered
|
||||
public void print() { // O(n)
|
||||
if (list.size() == 0)
|
||||
System.out.println("Add items to cart.");
|
||||
for (int i = 0; i < list.size(); i++)
|
||||
System.out.println(list.get(i)._name + ": $" + list.get(i)._cost);
|
||||
|
||||
}
|
||||
|
||||
// adds items to the list
|
||||
public void addItem() { // O(1) ignoring a user just spmming the wrong input type
|
||||
|
||||
System.out.print("\nEnter Item: ");
|
||||
String name = sc.nextLine();
|
||||
|
||||
// getting how much the item costs
|
||||
Double cost = 0.0;
|
||||
while (true) { // forces user to input a double for price
|
||||
System.out.print("Enter Cost ($): ");
|
||||
String costInput = sc.nextLine();
|
||||
try {
|
||||
cost = Double.parseDouble(costInput); // parses input for double
|
||||
break;
|
||||
} catch (NumberFormatException nfe) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// creates new item to add and adds it
|
||||
Item newItem = new Item(name, cost);
|
||||
list.add(newItem);
|
||||
}
|
||||
|
||||
// adds multiple items for convience
|
||||
public void addMultiple() { // O(n) ignoring a user just spamming the wrong input type
|
||||
int num;
|
||||
while (true) { // forces user to input a int for items to add
|
||||
System.out.print("Enter number of Items: ");
|
||||
String numInput = sc.nextLine();
|
||||
try {
|
||||
num = Integer.parseInt(numInput); // parses input for int
|
||||
break;
|
||||
} catch (NumberFormatException nfe) {
|
||||
|
||||
}
|
||||
}
|
||||
// runs through loop to add items
|
||||
for (int i = 0; i < num; i++) {
|
||||
addItem();
|
||||
}
|
||||
}
|
||||
|
||||
public void removeItem() { // O(n) due to the Linear Search
|
||||
// gets item to remove
|
||||
System.out.print("Enter item to remove: ");
|
||||
String item = sc.nextLine();
|
||||
|
||||
int index = LinearSearch.search(list, item); // gets index of item to search for
|
||||
|
||||
if (index != -1) // if item is in list
|
||||
list.remove(index);
|
||||
else
|
||||
System.out.println("Not in shopping list");
|
||||
|
||||
}
|
||||
|
||||
// MergeSort based on name
|
||||
public void sortName() { // O(n log n)
|
||||
if (list.size() == 0)
|
||||
System.out.println("Add items to cart.");
|
||||
else {
|
||||
MergeSort.sort(list, 1);
|
||||
print();
|
||||
}
|
||||
}
|
||||
|
||||
// MergeSort base on price
|
||||
public void sortCost() { // O(n log n)
|
||||
if (list.size() == 0)
|
||||
System.out.println("Add items to cart.");
|
||||
else {
|
||||
MergeSort.sort(list, 2);
|
||||
print();
|
||||
}
|
||||
}
|
||||
|
||||
// returns result of the search
|
||||
public void search() { // O(n)
|
||||
if (list.size() == 0)
|
||||
System.out.println("Add items to cart.");
|
||||
else {
|
||||
System.out.print("Enter item to search for: ");
|
||||
String input = sc.nextLine();
|
||||
int index = LinearSearch.search(list, input);
|
||||
if (index != -1)
|
||||
System.out.println("Item is in cart at index " + index);
|
||||
else
|
||||
System.out.println("Item is not in cart.");
|
||||
}
|
||||
}
|
||||
|
||||
// prints all the options
|
||||
public void PrintOptions() { // O(1)
|
||||
System.out.println();
|
||||
System.out.println("1. View Shopping Cart");
|
||||
System.out.println("2. Add Singular Item");
|
||||
System.out.println("3. Add Multiple Items");
|
||||
System.out.println("4. Remove Item");
|
||||
System.out.println("5. Sort by name");
|
||||
System.out.println("6. Sort by cost");
|
||||
System.out.println("7. Check if item is in cart");
|
||||
System.out.println("8. Show options");
|
||||
System.out.println("9. Checkout");
|
||||
}
|
||||
|
||||
// totals the cart and prints the result
|
||||
public void checkout() { // O(n)
|
||||
Double total = 0.0;
|
||||
for (int i = 0; i < list.size(); i++)
|
||||
total += list.get(i)._cost;
|
||||
|
||||
System.out.println("\nYour total is: $" + total);
|
||||
print();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
This is not as organized as CS101 unfortuantely
|
||||
Vendored
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Vendored
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Vendored
BIN
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Vendored
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Vendored
BIN
Binary file not shown.
@@ -0,0 +1,65 @@
|
||||
package Iterator;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Course implements Serializable {
|
||||
private String _prefix;
|
||||
private int _number;
|
||||
private String _title;
|
||||
private String _grade;
|
||||
|
||||
public Course (String prefix, int number, String title, String grade) {
|
||||
_prefix = prefix;
|
||||
_number = number;
|
||||
_title = title;
|
||||
if (grade == null)
|
||||
_grade = " ";
|
||||
else
|
||||
_grade = grade;
|
||||
}
|
||||
|
||||
public Course (String prefix, int number, String title) {
|
||||
this(prefix, number, title, " ");
|
||||
}
|
||||
|
||||
public String getPrefix() {
|
||||
return _prefix;
|
||||
}
|
||||
|
||||
public int getNumber() {
|
||||
return _number;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return _title;
|
||||
}
|
||||
|
||||
public String getGrade() {
|
||||
return _grade;
|
||||
}
|
||||
|
||||
public void setGrade(String grade) {
|
||||
_grade = grade;
|
||||
}
|
||||
|
||||
public boolean taken() {
|
||||
return !_grade.equals(" ");
|
||||
}
|
||||
|
||||
public boolean equals(Object other) {
|
||||
boolean result = false;
|
||||
if (other instanceof Course) {
|
||||
Course otherCourse = (Course) other;
|
||||
if (_prefix.equals(otherCourse.getPrefix()) && _number == otherCourse.getNumber())
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
String result = _prefix + " " + _number + ": " + _title;
|
||||
if (!_grade.equals(" "))
|
||||
result += " [" + _grade + "]";
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package Iterator;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
|
||||
public class POSGrades {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Course mth121 = new Course("MTH", 121, "Calc I", "A");
|
||||
Course mth122 = new Course("MTH", 122, "Calc II", "B");
|
||||
Course cs101 = new Course("CS", 101, "CS 101", "C");
|
||||
Course cs102 = new Course("CS", 102, "CS 102", "D");
|
||||
Course cs140 = new Course("CS", 140, "CS 102");
|
||||
Course cs210 = new Course("CS", 210, "CS 104", "F");
|
||||
|
||||
ProgramOfStudy pos = new ProgramOfStudy();
|
||||
pos.addCourse(mth121);
|
||||
pos.addCourse(mth122);
|
||||
pos.addCourse(cs101);
|
||||
pos.addCourse(cs102);
|
||||
pos.addCourse(cs140);
|
||||
pos.addCourse(cs210);
|
||||
|
||||
pos.save("ProgramOfStudy");
|
||||
|
||||
pos.load("ProgramOfStudy");
|
||||
|
||||
for (Course course : pos) {
|
||||
if (!course.getGrade().equals(" ") && !course.getGrade().equals("F"))
|
||||
System.out.println(course);
|
||||
}
|
||||
|
||||
System.out.println("Classes with Grades of C or D \n");
|
||||
|
||||
for (Course course : pos) {
|
||||
if (course.getGrade().equals("C") || course.getGrade().equals("D"))
|
||||
System.out.println(course);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package Iterator;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class ProgramOfStudy implements Iterable<Course>, Serializable {
|
||||
private List<Course> list;
|
||||
|
||||
public ProgramOfStudy() {
|
||||
list = new LinkedList<Course>();
|
||||
}
|
||||
|
||||
public void addCourse(Course course) {
|
||||
if (course != null)
|
||||
list.add(course);
|
||||
}
|
||||
|
||||
public Course find(String prefix, int number) {
|
||||
for (Course course : list)
|
||||
if (prefix.equals(course.getPrefix()) && number == course.getNumber())
|
||||
return course;
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addCourseAfter(Course target, Course newCourse) {
|
||||
if (target == null || newCourse == null)
|
||||
return;
|
||||
|
||||
int targetIndex = list.indexOf(target);
|
||||
if (targetIndex != -1)
|
||||
list.add(targetIndex + 1, newCourse);
|
||||
}
|
||||
|
||||
public void replace(Course target, Course newCourse) {
|
||||
if (target == null || newCourse == null)
|
||||
return;
|
||||
|
||||
int targetIndex = list.indexOf(target);
|
||||
if (targetIndex != -1)
|
||||
list.set(targetIndex, newCourse);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
String result = "";
|
||||
for (Course course : list)
|
||||
result += course + "\n";
|
||||
return result;
|
||||
}
|
||||
|
||||
public Iterator<Course> iterator() {
|
||||
return list.iterator();
|
||||
}
|
||||
|
||||
public void save(String fileName) throws IOException {
|
||||
FileOutputStream fos = new FileOutputStream(fileName);
|
||||
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
||||
oos.writeObject(this);
|
||||
oos.close();
|
||||
}
|
||||
|
||||
public static ProgramOfStudy load(String fileName) throws IOException, ClassNotFoundException {
|
||||
FileInputStream fis = new FileInputStream(fileName);
|
||||
ObjectInputStream ois = new ObjectInputStream(fis);
|
||||
ProgramOfStudy pos = (ProgramOfStudy) ois.readObject();
|
||||
ois.close();
|
||||
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
public class Sum {
|
||||
public static void main(String args[]) {
|
||||
System.out.println(factorial(5));
|
||||
}
|
||||
|
||||
public static int factorial (int num) {
|
||||
int result = 1;
|
||||
int last;
|
||||
|
||||
for (int i = num; i >= 0; i--) {
|
||||
result *= i;
|
||||
}
|
||||
return result ;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
EmptyCollectionException.java
|
||||
Homework 2
|
||||
This code handles empty stack exceptions
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package homework1.exceptions;
|
||||
|
||||
public class EmptyCollectionException extends RuntimeException {
|
||||
public EmptyCollectionException(String collection) {
|
||||
//Prints error to cmd line
|
||||
super("The " + collection + " is empty.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
PostfixEvaluator.java
|
||||
Homework 1
|
||||
This code computes postfix expressions
|
||||
*/
|
||||
|
||||
package homework1.question1;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.Stack;
|
||||
|
||||
public class PostfixEvaluator {
|
||||
//Variables
|
||||
private static final char ADD = '+';
|
||||
private static final char SUBTRACT = '-';
|
||||
private static final char MULTIPLY = '*';
|
||||
private static final char DIVIDE = '/';
|
||||
private Stack<Integer> stack = new Stack();
|
||||
|
||||
//main code
|
||||
public int evaluate(String expr) {
|
||||
int result = 0;
|
||||
Scanner parser = new Scanner(expr);
|
||||
//while there's another expression keep running
|
||||
while (parser.hasNext()) {
|
||||
String token = parser.next();
|
||||
//if there's an operator it calculates it using the two
|
||||
//top values and adds them to the stack
|
||||
if (this.isOperator(token)) {
|
||||
int op2 = this.stack.pop();
|
||||
int op1 = this.stack.pop();
|
||||
result = this.evaluateSingleOperator(token.charAt(0), op1, op2);
|
||||
this.stack.push(new Integer(result));
|
||||
continue;
|
||||
}
|
||||
this.stack.push(new Integer(Integer.parseInt(token)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//Returns true if the input is a operator
|
||||
private boolean isOperator(String token) {
|
||||
return token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/");
|
||||
}
|
||||
|
||||
//adds, subtracts, multiplies, or divides
|
||||
private int evaluateSingleOperator(char operation, int op1, int op2) {
|
||||
int result = 0;
|
||||
switch (operation) {
|
||||
case '+': {
|
||||
result = op1 + op2;
|
||||
break;
|
||||
}
|
||||
case '-': {
|
||||
result = op1 - op2;
|
||||
break;
|
||||
}
|
||||
case '*': {
|
||||
result = op1 * op2;
|
||||
break;
|
||||
}
|
||||
case '/': {
|
||||
result = op1 / op2;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
TestPostfixEvaluator.java
|
||||
Homework 1
|
||||
This code test PostfixEvaluator.java
|
||||
*/
|
||||
|
||||
|
||||
package homework1.question1;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class TestPostfixEvaluator {
|
||||
public static void main(String[] args) {
|
||||
String again;
|
||||
Scanner in = new Scanner(System.in);
|
||||
do {
|
||||
PostfixEvaluator evaluator = new PostfixEvaluator();
|
||||
//gets user input
|
||||
System.out.println("Enter a valid post-fix expression one token at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)");
|
||||
System.out.println("Each token must be an integer or an operator (+,-,*,/)");
|
||||
String expression = in.nextLine();
|
||||
|
||||
//calculates
|
||||
int result = evaluator.evaluate(expression);
|
||||
|
||||
//Asks if user wants to compute another
|
||||
System.out.println();
|
||||
System.out.println("That expression equals " + result);
|
||||
System.out.println("Evaluate another expression [Y/N]? ");
|
||||
again = in.nextLine();
|
||||
System.out.println();
|
||||
} while (again.equalsIgnoreCase("y"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
ArrayStack.java
|
||||
Homework 1
|
||||
This code creates an array stack
|
||||
*/
|
||||
|
||||
|
||||
package homework1.question2;
|
||||
|
||||
|
||||
import homework1.exceptions.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
import homework1.exceptions.*;
|
||||
|
||||
public class ArrayStack<T> implements StackADT<T> {
|
||||
//Variables
|
||||
private static final int DEFAULT_CAPACITY = 100;
|
||||
private int top;
|
||||
private T[] stack;
|
||||
|
||||
//Default Contructor
|
||||
public ArrayStack() {
|
||||
this(DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
//Sets the initialCapacity to the user input
|
||||
public ArrayStack(int initialCapacity) {
|
||||
top = 0;
|
||||
stack = (T[]) (new Object[initialCapacity]);
|
||||
}
|
||||
|
||||
//Adds T element to the stack
|
||||
public void push(T element) {
|
||||
//If the stack is full it expands
|
||||
if (size() == stack.length)
|
||||
expandCapacity();
|
||||
stack[top] = element;
|
||||
top++;
|
||||
}
|
||||
|
||||
//doubles the length of the stack
|
||||
private void expandCapacity() {
|
||||
stack = Arrays.copyOf(stack, stack.length * 2);
|
||||
}
|
||||
|
||||
//Removes the top object
|
||||
public T pop() throws EmptyCollectionException {
|
||||
if (isEmpty())
|
||||
throw new EmptyCollectionException("stack");
|
||||
|
||||
top--;
|
||||
T result = stack[top];
|
||||
stack[top] = null;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//Returns the top object
|
||||
public T peek() throws EmptyCollectionException {
|
||||
if (isEmpty())
|
||||
throw new EmptyCollectionException("stack");
|
||||
|
||||
return stack[top - 1];
|
||||
|
||||
}
|
||||
|
||||
//checks if the stack is empty
|
||||
public boolean isEmpty() {
|
||||
return top == 0;
|
||||
}
|
||||
|
||||
//returns size of the stack
|
||||
public int size() {
|
||||
return top;
|
||||
}
|
||||
|
||||
//returns the stack as a string
|
||||
public String toString() {
|
||||
String result = "";
|
||||
if (top > 0) {
|
||||
result = result + stack[top-1];
|
||||
}
|
||||
for (int i = top - 2; i >= 0; i--) {
|
||||
result = result + " " + stack[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
StackADT.java
|
||||
Homework 1
|
||||
this provides an interface for ArrayStack.java
|
||||
*/
|
||||
|
||||
package homework1.question2;
|
||||
|
||||
public interface StackADT<T> {
|
||||
public void push(T element);
|
||||
|
||||
public T pop();
|
||||
|
||||
public T peek();
|
||||
|
||||
public boolean isEmpty();
|
||||
|
||||
public int size();
|
||||
|
||||
public String toString();
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package homework1.question2;
|
||||
|
||||
import homework1.question2.*;
|
||||
|
||||
public class TestArrayStack {
|
||||
public static void main(String[] args) {
|
||||
ArrayStack s = new ArrayStack<>(3);
|
||||
s.peek();
|
||||
s.push(1);
|
||||
s.push(2);
|
||||
s.push(3);
|
||||
System.out.println(s.peek());
|
||||
s.push(4);
|
||||
s.push(5);
|
||||
s.pop();
|
||||
System.out.println(s.peek());
|
||||
System.out.println(s.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package homework1.question3;
|
||||
import java.util.Arrays;
|
||||
import homework1.exceptions.*;
|
||||
|
||||
public class IntStack {
|
||||
private final static int DEFAULT_CAPACITY = 7;
|
||||
private int stack[];
|
||||
private int top=0;
|
||||
|
||||
//option 1
|
||||
public IntStack(){
|
||||
this (DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
//option 2
|
||||
public IntStack (int initialCapacity){
|
||||
top=0;
|
||||
stack = new int [initialCapacity];
|
||||
}
|
||||
|
||||
|
||||
public void push (int element)
|
||||
{
|
||||
if (size() == stack.length)
|
||||
expandCapacity();
|
||||
|
||||
stack[top] = element;
|
||||
++top;
|
||||
}
|
||||
|
||||
public int pop() throws EmptyCollectionException{
|
||||
if (isEmpty())
|
||||
throw new EmptyCollectionException("Stack");
|
||||
|
||||
int result;
|
||||
--top;
|
||||
result = stack[top];
|
||||
stack[top]=0;
|
||||
return result;
|
||||
}
|
||||
|
||||
public int peek() throws EmptyCollectionException{
|
||||
if (isEmpty())
|
||||
throw new EmptyCollectionException("Stack");
|
||||
|
||||
int result;
|
||||
result=stack[top-1];
|
||||
return result;
|
||||
}
|
||||
|
||||
public int size(){
|
||||
return top;
|
||||
}
|
||||
|
||||
public boolean isEmpty(){
|
||||
return top ==0;
|
||||
}
|
||||
|
||||
//print out the Stack
|
||||
public void showStack(){
|
||||
for (int n: stack){
|
||||
System.out.print(n + " ");
|
||||
}
|
||||
}
|
||||
|
||||
private void expandCapacity(){
|
||||
stack = Arrays.copyOf(stack, stack.length*2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
TestIntStack.java
|
||||
Homework 1
|
||||
this code tests IntStack.java
|
||||
*/
|
||||
|
||||
package homework1.question3;
|
||||
|
||||
import homework1.question3.*;
|
||||
|
||||
public class TestIntStack {
|
||||
public static void main(String[] args) {
|
||||
IntStack s = new IntStack(3);
|
||||
s.push(1);
|
||||
s.push(2);
|
||||
s.push(3);
|
||||
System.out.println(s.peek());
|
||||
s.push(4);
|
||||
s.push(5);
|
||||
s.pop();
|
||||
System.out.println(s.peek());
|
||||
s.showStack();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package homework1.question4;
|
||||
|
||||
import java.util.Arrays;
|
||||
import homework1.exceptions.*;
|
||||
|
||||
public class DoubleStack {
|
||||
private final static int DEFAULT_CAPACITY = 7;
|
||||
private double stack[];
|
||||
private int top=0;
|
||||
|
||||
//option 1
|
||||
public DoubleStack(){
|
||||
this (DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
//option 2
|
||||
public DoubleStack (int initialCapacity){
|
||||
top=0;
|
||||
stack = new double [initialCapacity];
|
||||
}
|
||||
|
||||
|
||||
public void push (double element)
|
||||
{
|
||||
if (size() == stack.length)
|
||||
expandCapacity();
|
||||
|
||||
stack[top] = element;
|
||||
++top;
|
||||
}
|
||||
|
||||
public double pop() throws EmptyCollectionException{
|
||||
if (isEmpty())
|
||||
throw new EmptyCollectionException("Stack");
|
||||
|
||||
double result;
|
||||
--top;
|
||||
result = stack[top];
|
||||
stack[top]=0;
|
||||
return result;
|
||||
}
|
||||
|
||||
public double peek() throws EmptyCollectionException{
|
||||
if (isEmpty())
|
||||
throw new EmptyCollectionException("Stack");
|
||||
|
||||
double result;
|
||||
result=stack[top-1];
|
||||
return result;
|
||||
}
|
||||
|
||||
public int size(){
|
||||
return top;
|
||||
}
|
||||
|
||||
public boolean isEmpty(){
|
||||
return top ==0;
|
||||
}
|
||||
|
||||
//print out the Stack
|
||||
public void showStack(){
|
||||
for (double n: stack){
|
||||
System.out.print(n + " ");
|
||||
}
|
||||
}
|
||||
|
||||
private void expandCapacity(){
|
||||
stack = Arrays.copyOf(stack, stack.length*2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package homework1.question4;
|
||||
|
||||
public class TestDoubleStack {
|
||||
public static void main(String[] args) {
|
||||
DoubleStack s = new DoubleStack(3);
|
||||
s.push(1.2);
|
||||
s.push(2.3);
|
||||
s.push(3.4);
|
||||
System.out.println(s.peek());
|
||||
s.push(4.5);
|
||||
s.push(5.6);
|
||||
s.pop();
|
||||
System.out.println(s.peek());
|
||||
s.showStack();
|
||||
}
|
||||
}
|
||||
Vendored
BIN
Binary file not shown.
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
CircularArrayQueue.java
|
||||
Homework 2
|
||||
This is a queue that's last node point back to the first node*/
|
||||
|
||||
package homework2;
|
||||
|
||||
import homework2.exceptions.*;
|
||||
|
||||
import project1.exceptions.EmptyCollectionException;
|
||||
|
||||
public class CircularArrayQueue<T> implements QueueADT<T> {
|
||||
//variables
|
||||
private final static int DEFAULT_CAPACITY = 100;
|
||||
private int front, rear, count;
|
||||
private T[] queue;
|
||||
|
||||
//constructor for initial capacity
|
||||
public CircularArrayQueue(int initialCapacity) {
|
||||
front = rear = count = 0;
|
||||
queue = (T[]) (new Object[initialCapacity]);
|
||||
}
|
||||
|
||||
//default constructor
|
||||
public CircularArrayQueue() {
|
||||
this(DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
//adds an element to the queue
|
||||
public void enqueue(T element) {
|
||||
if (size() == queue.length)
|
||||
expandCapacity();
|
||||
|
||||
queue[rear] = element;
|
||||
rear = (rear +1) % queue.length;
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
//doubles the queue's length
|
||||
private void expandCapacity() {
|
||||
T[] larger = (T[]) (new Object[queue.length * 2]);
|
||||
|
||||
//copying over the queue
|
||||
for (int scan = 0; scan < count; scan++) {
|
||||
larger[scan] = queue[front];
|
||||
front = (front + 1) % queue.length;
|
||||
}
|
||||
|
||||
front = 0;
|
||||
rear = count;
|
||||
queue = larger;
|
||||
}
|
||||
|
||||
//removes an element
|
||||
public T dequeue() throws EmptyCollectionException {
|
||||
if (isEmpty())
|
||||
throw new EmptyCollectionException("queue");
|
||||
|
||||
T result = queue[front];
|
||||
queue[front] = null;
|
||||
front = (front + 1) % queue.length;
|
||||
|
||||
count--;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//checks if the queue is empty
|
||||
public boolean isEmpty() {
|
||||
return (count == 0);
|
||||
}
|
||||
|
||||
//returns the first elemnent of the queue
|
||||
public T first() throws EmptyCollectionException {
|
||||
if (isEmpty())
|
||||
throw new EmptyCollectionException ("queue");
|
||||
|
||||
return queue[front];
|
||||
}
|
||||
|
||||
//returns size
|
||||
public int size() {
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
Codes.java
|
||||
Homework 2
|
||||
an implementation of the ceaser cypher
|
||||
*/
|
||||
|
||||
package homework2;
|
||||
|
||||
public class Codes {
|
||||
public static void main(String[] args) {
|
||||
|
||||
//variables
|
||||
int[] key = {5, 12, -3, 8, -9, 4, 10};
|
||||
Integer keyValue;
|
||||
String encoded = "", decoded = "";
|
||||
|
||||
//original message
|
||||
String message = "All programmers are playwrights and all computers are lousy actors";
|
||||
|
||||
CircularArrayQueue<Integer> encodingQueue = new CircularArrayQueue<Integer>();
|
||||
CircularArrayQueue<Integer> decodingQueue = new CircularArrayQueue<Integer>();
|
||||
|
||||
//start key queue
|
||||
for (int scan = 0; scan < key.length; scan++) {
|
||||
encodingQueue.enqueue(key[scan]);
|
||||
decodingQueue.enqueue(key[scan]);
|
||||
}
|
||||
|
||||
//encode
|
||||
for (int scan = 0; scan < message.length(); scan++) {
|
||||
keyValue = encodingQueue.dequeue();
|
||||
encoded += (char) (message.charAt(scan) + keyValue);
|
||||
encodingQueue.enqueue(keyValue);
|
||||
}
|
||||
|
||||
//print out encoded message
|
||||
System.out.println("Encoded Message:\n" + encoded + "\n");
|
||||
|
||||
//decode
|
||||
for (int scan = 0; scan < encoded.length(); scan ++) {
|
||||
keyValue = decodingQueue.dequeue();
|
||||
decoded += (char) (encoded.charAt(scan) - keyValue);
|
||||
decodingQueue.enqueue(keyValue);
|
||||
}
|
||||
|
||||
//print out decoded message
|
||||
System.out.println("Encoded Message:\n" + decoded + "\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
Customer.java
|
||||
Homework 2
|
||||
This is a class for Customer for the TicketCounter.java
|
||||
*/
|
||||
|
||||
package homework2;
|
||||
|
||||
public class Customer {
|
||||
//variables
|
||||
private int arrivalTime, departureTime;
|
||||
|
||||
//constructer that sets arival time
|
||||
public Customer(int arrives) {
|
||||
arrivalTime = arrives;
|
||||
departureTime = 0;
|
||||
}
|
||||
|
||||
//returns arrival time
|
||||
public int getArrivalTime() {
|
||||
return arrivalTime;
|
||||
}
|
||||
|
||||
//sets depature time
|
||||
public void setDepartureTime(int departs) {
|
||||
departureTime = departs;
|
||||
}
|
||||
|
||||
//returns departure time
|
||||
public int getDepartureTime() {
|
||||
return departureTime;
|
||||
}
|
||||
|
||||
//returns the total time the Customer spent in the "store"
|
||||
public int totalTime() {
|
||||
return departureTime - arrivalTime;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Executable
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
LinearNode.java
|
||||
Homework 2
|
||||
This code handles nodes for LinearQueue.java
|
||||
*/
|
||||
|
||||
package homework2;
|
||||
|
||||
public class LinearNode<T> {
|
||||
private LinearNode<T> next;
|
||||
private T element;
|
||||
|
||||
public LinearNode() {
|
||||
next = null;
|
||||
element = null;
|
||||
}
|
||||
|
||||
public LinearNode(T elem) {
|
||||
next = null;
|
||||
element = elem;
|
||||
}
|
||||
|
||||
public LinearNode<T> getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setNext(LinearNode<T> node) {
|
||||
next = node;
|
||||
}
|
||||
|
||||
public T getElement() {
|
||||
return element;
|
||||
}
|
||||
|
||||
public void setElement(T elem) {
|
||||
element = elem;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
LinkedQueue.java
|
||||
Homework 2
|
||||
THis code implements a queue with linked nodes
|
||||
*/
|
||||
|
||||
package homework2;
|
||||
|
||||
import homework2.exceptions.*;
|
||||
|
||||
public class LinkedQueue<T> implements QueueADT<T> {
|
||||
|
||||
//variables
|
||||
private int count;
|
||||
private LinearNode<T> head, tail;
|
||||
|
||||
//default construcor tha tsets the count of total nodes to 0
|
||||
//and sets the front and back of the queue to null
|
||||
public LinkedQueue() {
|
||||
count = 0;
|
||||
head = tail = null;
|
||||
}
|
||||
|
||||
//adds element to queue
|
||||
public void enqueue(T element) {
|
||||
LinearNode<T> node = new LinearNode<T>(element);
|
||||
|
||||
if(isEmpty())
|
||||
head = node;
|
||||
else
|
||||
tail.setNext(node);
|
||||
|
||||
//sets the tail to the node
|
||||
tail = node;
|
||||
count++;
|
||||
}
|
||||
|
||||
//removes an element to queue
|
||||
public T dequeue() throws EmptyCollectionException {
|
||||
if (isEmpty())
|
||||
throw new EmptyCollectionException("queue");
|
||||
|
||||
//sets the result to head and sets the head to the next
|
||||
//element. it also decreases the count by 1
|
||||
T result = head.getElement();
|
||||
head = head.getNext();
|
||||
count--;
|
||||
|
||||
if (isEmpty())
|
||||
tail = null;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//returns true if the queue is empty
|
||||
public boolean isEmpty() {
|
||||
return (count == 0);
|
||||
}
|
||||
|
||||
//returnns the first element of the queue
|
||||
public T first() throws EmptyCollectionException {
|
||||
if (isEmpty())
|
||||
throw new EmptyCollectionException ("queue");
|
||||
|
||||
return head.getElement();
|
||||
}
|
||||
|
||||
//returns the size
|
||||
public int size() {
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
QueueADT.java
|
||||
Homework 2
|
||||
This code provides and interface for CircularArrayQueue and LinkedQueue
|
||||
*/
|
||||
|
||||
package homework2;
|
||||
|
||||
public interface QueueADT<T> {
|
||||
|
||||
public void enqueue (T element);
|
||||
|
||||
public T dequeue();
|
||||
|
||||
public T first();
|
||||
|
||||
public boolean isEmpty();
|
||||
|
||||
public int size();
|
||||
|
||||
public String toString();
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
TicketCounter.java
|
||||
Homework 2
|
||||
This code computes postfix expressions
|
||||
*/
|
||||
|
||||
package homework2;
|
||||
|
||||
public class TicketCounter {
|
||||
//variables
|
||||
private final static int PROCESS = 855; //last 3 of my id
|
||||
private final static int MAX_CASHIERS = 10;
|
||||
private final static int NUM_CUSTOMERS = 100;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//more variables
|
||||
Customer customer;
|
||||
LinkedQueue<Customer> customerQueue = new LinkedQueue<Customer>();
|
||||
int[] cashierTime = new int[MAX_CASHIERS];
|
||||
int totalTime, averageTime, departs, start;
|
||||
|
||||
//runs a loop for the time taken for the each number of cashiers 1-10
|
||||
for (int cashiers = 0; cashiers < MAX_CASHIERS; cashiers++) {
|
||||
//loop that resets the casheir time
|
||||
for (int count = 0; count <= cashiers; count++) {
|
||||
cashierTime[count] = 0;
|
||||
}
|
||||
|
||||
//queus customers
|
||||
for (int count = 1; count <= NUM_CUSTOMERS; count++) {
|
||||
customerQueue.enqueue(new Customer(count * 15));
|
||||
}
|
||||
totalTime = 0;
|
||||
|
||||
//runs through each customer dequeing as it goes
|
||||
//it adds up the process time and add it to the total
|
||||
while(!(customerQueue.isEmpty())) {
|
||||
for (int count = 0; count <= cashiers; count++) {
|
||||
if (!(customerQueue.isEmpty())) {
|
||||
customer = customerQueue.dequeue();
|
||||
if (customer.getArrivalTime() > cashierTime[count]){
|
||||
start = customer.getArrivalTime();
|
||||
} else {
|
||||
start = cashierTime[count];
|
||||
}
|
||||
departs = start + PROCESS;
|
||||
customer.setDepartureTime(departs);
|
||||
cashierTime[count] = departs;
|
||||
totalTime += customer.totalTime();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//results
|
||||
averageTime = totalTime / NUM_CUSTOMERS;
|
||||
System.out.println("Number of cashiers: " + (cashiers + 1));
|
||||
System.out.println("average TIme: " + averageTime + "\n");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
EmptyCollectionException.java
|
||||
Homework 2
|
||||
This code handles empty stack exceptions
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package homework2.exceptions;
|
||||
|
||||
public class EmptyCollectionException extends RuntimeException {
|
||||
public EmptyCollectionException(String collection) {
|
||||
//Prints error to cmd line
|
||||
super("The " + collection + " is empty.");
|
||||
}
|
||||
}
|
||||
Vendored
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
SearchCombo.java
|
||||
Homework3
|
||||
this code contains the linearSearch and binarySearch methods
|
||||
*/
|
||||
|
||||
package homework3.SearchCombo;
|
||||
|
||||
public class SearchCombo<T extends Comparable<T>> {
|
||||
|
||||
// code for linear search
|
||||
public int linearSearch(T[] array, T target) {
|
||||
int comparisons = 0;
|
||||
int index = 0;
|
||||
boolean found = false;
|
||||
while (!found && index <= array.length - 1) {
|
||||
found = array[index].equals(target);
|
||||
index++;
|
||||
comparisons++;
|
||||
// checking for not found
|
||||
if (comparisons == array.length) {
|
||||
System.out.println("Not found!");
|
||||
return comparisons;
|
||||
}
|
||||
}
|
||||
return comparisons;
|
||||
}
|
||||
|
||||
// binary search
|
||||
public int binarySearch(T[] arr, T target) {
|
||||
int low = 0;
|
||||
int high = arr.length - 1;
|
||||
int comparisons = 0;
|
||||
|
||||
while (low <= high) {
|
||||
int mid = (low + high) / 2;
|
||||
|
||||
// checking if it has been found other wise making a new section to check
|
||||
if (target.compareTo(arr[mid]) == 0) {
|
||||
comparisons++;
|
||||
return comparisons;
|
||||
} else if (target.compareTo(arr[mid]) < 0) {
|
||||
comparisons++;
|
||||
high = mid - 1;
|
||||
} else {
|
||||
comparisons++;
|
||||
low = mid + 1;
|
||||
}
|
||||
}
|
||||
System.out.println("Not found!");
|
||||
return comparisons;
|
||||
|
||||
}
|
||||
}
|
||||
Vendored
BIN
Binary file not shown.
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
Tester.java
|
||||
Homework3
|
||||
This code tests the binary and linear seach in SeachCombo.java
|
||||
*/
|
||||
|
||||
package homework3.Tester;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Tester {
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
SearchCombo<String> sCombo = new SearchCombo<String>();
|
||||
|
||||
System.out.println("Enter the elements (search pool):");
|
||||
String vals = input.nextLine();
|
||||
String[] arr = vals.toString().split(" ");
|
||||
System.out.println("Target:");
|
||||
String target = input.nextLine();
|
||||
System.out.println();
|
||||
|
||||
System.out.println("# of Comparisons for Linear Search: " + sCombo.linearSearch(arr, target));
|
||||
System.out.println("# of comparisons for Binary Search: " + sCombo.binarySearch(arr, target));
|
||||
|
||||
}
|
||||
}
|
||||
Executable
+88
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
ArrayStack.java
|
||||
Project 1
|
||||
This code creates an array stack
|
||||
*/
|
||||
|
||||
|
||||
package project1;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import project1.exceptions.*;
|
||||
|
||||
public class ArrayStack<T> implements StackADT<T> {
|
||||
//Variables
|
||||
private static final int DEFAULT_CAPACITY = 100;
|
||||
private int top;
|
||||
private T[] stack;
|
||||
|
||||
//Default Contructor
|
||||
public ArrayStack() {
|
||||
this(DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
//Sets the initialCapacity to the user input
|
||||
public ArrayStack(int initialCapacity) {
|
||||
top = 0;
|
||||
stack = (T[]) (new Object[initialCapacity]);
|
||||
}
|
||||
|
||||
//Adds T element to the stack
|
||||
public void push(T element) {
|
||||
//If the stack is full it expands
|
||||
if (size() == stack.length)
|
||||
expandCapacity();
|
||||
stack[top] = element;
|
||||
top++;
|
||||
}
|
||||
|
||||
//doubles the length of the stack
|
||||
private void expandCapacity() {
|
||||
stack = Arrays.copyOf(stack, stack.length * 2);
|
||||
}
|
||||
|
||||
//Removes the top object
|
||||
public T pop() throws EmptyCollectionException {
|
||||
if (isEmpty())
|
||||
throw new EmptyCollectionException("stack");
|
||||
|
||||
top--;
|
||||
T result = stack[top];
|
||||
stack[top] = null;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//Returns the top object
|
||||
public T peek() throws EmptyCollectionException {
|
||||
if (isEmpty())
|
||||
throw new EmptyCollectionException("stack");
|
||||
|
||||
return stack[top - 1];
|
||||
|
||||
}
|
||||
|
||||
//checks if the stack is empty
|
||||
public boolean isEmpty() {
|
||||
return top == 0;
|
||||
}
|
||||
|
||||
//returns size of the stack
|
||||
public int size() {
|
||||
return top;
|
||||
}
|
||||
|
||||
//returns the stack as a string
|
||||
public String toString() {
|
||||
String result = "";
|
||||
if (top > 0) {
|
||||
result = result + stack[top-1];
|
||||
}
|
||||
for (int i = top - 2; i >= 0; i--) {
|
||||
result = result + " " + stack[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Executable
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
LinearNode.java
|
||||
Project 1
|
||||
This code handles nodes for LinkedStack.java
|
||||
*/
|
||||
|
||||
package project1;
|
||||
|
||||
public class LinearNode<T> {
|
||||
private LinearNode<T> next;
|
||||
private T element;
|
||||
|
||||
public LinearNode() {
|
||||
next = null;
|
||||
element = null;
|
||||
}
|
||||
|
||||
public LinearNode(T elem) {
|
||||
next = null;
|
||||
element = elem;
|
||||
}
|
||||
|
||||
public LinearNode<T> getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setNext(LinearNode<T> node) {
|
||||
next = node;
|
||||
}
|
||||
|
||||
public T getElement() {
|
||||
return element;
|
||||
}
|
||||
|
||||
public void setElement(T elem) {
|
||||
element = elem;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Executable
+74
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
LinkedStack.java
|
||||
Project 1
|
||||
This code is a linked stack
|
||||
*/
|
||||
|
||||
package project1;
|
||||
import project1.exceptions.*;
|
||||
|
||||
public class LinkedStack<T> implements StackADT<T> {
|
||||
private int count;
|
||||
private LinearNode<T> top;
|
||||
|
||||
//default contructor
|
||||
public LinkedStack() {
|
||||
count = 0;
|
||||
top = null;
|
||||
}
|
||||
|
||||
//pushes new element
|
||||
public void push(T element) {
|
||||
LinearNode<T> temp = new LinearNode<T>(element);
|
||||
|
||||
temp.setNext(top);
|
||||
top = temp;
|
||||
count++;
|
||||
}
|
||||
|
||||
//removes element
|
||||
public T pop() throws EmptyCollectionException {
|
||||
if (isEmpty())
|
||||
throw new EmptyCollectionException("stack");
|
||||
|
||||
T result = top.getElement();
|
||||
top = top.getNext();
|
||||
count--;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//returns top element
|
||||
public T peek() throws EmptyCollectionException {
|
||||
if (isEmpty())
|
||||
throw new EmptyCollectionException("stack");
|
||||
|
||||
return top.getElement();
|
||||
}
|
||||
|
||||
//returns size
|
||||
public int size() {
|
||||
return count;
|
||||
}
|
||||
|
||||
//returns if empty
|
||||
public boolean isEmpty() {
|
||||
return top == null;
|
||||
}
|
||||
|
||||
|
||||
//prints the LinkedStack as string
|
||||
public String toString() {
|
||||
LinearNode<T> temp = top;
|
||||
String result = "";
|
||||
while(temp != null) {
|
||||
result = result + temp.getElement() + " ";
|
||||
temp = temp.getNext();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
PostfixToInflixTranslator.java
|
||||
Project 1
|
||||
This code changes a Postfix expression to an Inflix expression
|
||||
*/
|
||||
|
||||
package project1;
|
||||
|
||||
import project1.ArrayStack;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class PostfixToInfixTranslator {
|
||||
public static void main(String[] args) {
|
||||
//initialize scanner
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
|
||||
|
||||
//setup user
|
||||
String user;
|
||||
|
||||
//calculate the inflix expression
|
||||
//this is assuming no incorrect postfix expressions
|
||||
//otherwise there would be checks for insufficient values in expersion
|
||||
//and too many values
|
||||
do {
|
||||
//initialize stack
|
||||
ArrayStack inflix = new ArrayStack<>();
|
||||
|
||||
//get postfix expression
|
||||
System.out.print("Enter a postfix expression: ");
|
||||
String postfix = scan.nextLine();
|
||||
|
||||
//spilt postfix expression to each symbol
|
||||
String inputSymbols[] = postfix.split(" ");
|
||||
|
||||
for (String inputSymbol : inputSymbols) {
|
||||
//if operand -> pop the top two and put the operand between them and wrap them in parenthesis
|
||||
if (inputSymbol.equals("+") || inputSymbol.equals("-") ||
|
||||
inputSymbol.equals("*") || inputSymbol.equals("/") ) {
|
||||
String a = inflix.pop().toString();
|
||||
String b = inflix.pop().toString();
|
||||
String toPush = ("(" + b + " " + inputSymbol + " " + a + ")");
|
||||
inflix.push(toPush);
|
||||
|
||||
}
|
||||
//if it's a number -> just chuck it on the stack to be used for when theres an operand
|
||||
else {
|
||||
inflix.push(inputSymbol);
|
||||
}
|
||||
}
|
||||
|
||||
//print out inflix expression
|
||||
System.out.print("In inflix notation that is: ");
|
||||
System.out.println(inflix.toString());
|
||||
|
||||
//ask if user is done
|
||||
System.out.print("Translate another expression [y/n]? ");
|
||||
user = scan.nextLine();
|
||||
System.out.println();
|
||||
|
||||
} while(user.equalsIgnoreCase("y"));
|
||||
}
|
||||
}
|
||||
Executable
+51
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
ReversCharacters.java
|
||||
Project 1
|
||||
This code reverse each word in a string
|
||||
*/
|
||||
|
||||
package project1;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ReverseCharacters {
|
||||
|
||||
public static void main(String[] args) {
|
||||
//setup scanner and result
|
||||
Scanner scan = new Scanner(System.in);
|
||||
String result = "";
|
||||
|
||||
//get user input
|
||||
System.out.println("Enter a sentence:");
|
||||
String str = scan.nextLine();
|
||||
|
||||
//splits the input to an array of words
|
||||
String words[] = str.split(" ");
|
||||
|
||||
//setup the linked stack
|
||||
LinkedStack ls = new LinkedStack<>();
|
||||
|
||||
//for every word in the input
|
||||
for(String word : words) {
|
||||
//loops through the word pushing the charAt so the first character is at the bottom of the stack
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
ls.push(word.charAt(i));
|
||||
}
|
||||
//pop each element with the latest one first so that the word is reversed
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
result += ls.pop();
|
||||
}
|
||||
|
||||
//adds spacing
|
||||
result += " ";
|
||||
|
||||
}
|
||||
|
||||
//prints result
|
||||
System.out.println("Reversing characters:");
|
||||
System.out.println(result);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
StackADT.java
|
||||
Homework 1
|
||||
this provides an interface for ArrayStack.java
|
||||
*/
|
||||
|
||||
package project1;
|
||||
|
||||
public interface StackADT<T> {
|
||||
public void push(T element);
|
||||
|
||||
public T pop();
|
||||
|
||||
public T peek();
|
||||
|
||||
public boolean isEmpty();
|
||||
|
||||
public int size();
|
||||
|
||||
public String toString();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
Austin Bennett
|
||||
EmptyCollectionException.java
|
||||
Project 1
|
||||
This code handles empty stack exceptions
|
||||
*/
|
||||
|
||||
package project1.exceptions;
|
||||
|
||||
public class EmptyCollectionException extends RuntimeException {
|
||||
public EmptyCollectionException (String collection) {
|
||||
super ("The " + collection + " is empty.");
|
||||
}
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,99 @@
|
||||
package project2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
class Node<T> {
|
||||
private final T root;
|
||||
private Node<T> parent;
|
||||
private final ArrayList<Node<T>> children;
|
||||
|
||||
public Node(T root) {
|
||||
this.root = root;
|
||||
children = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Node<T> addChild(T child) {
|
||||
Node<T> childNode = new Node<T>(child);
|
||||
childNode.parent = this;
|
||||
this.children.add(childNode);
|
||||
return childNode;
|
||||
}
|
||||
|
||||
public T getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
public boolean isRoot() {
|
||||
return parent == null;
|
||||
}
|
||||
|
||||
public boolean isLeaf() {
|
||||
return children.size() == 0;
|
||||
}
|
||||
|
||||
public int getLevel() {
|
||||
if (this.isRoot())
|
||||
return 0;
|
||||
else
|
||||
return parent.getLevel() + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return root != null ? root.toString() : "null";
|
||||
}
|
||||
}
|
||||
|
||||
public class JavaTree {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Node<String> x = new Node<String>("parent1");
|
||||
Node<String> y = new Node<String>("parent2");
|
||||
|
||||
System.out.println(x.getRoot());
|
||||
|
||||
Node<String> child1 = x.addChild("child1");
|
||||
{
|
||||
Node<String> innerChild1 = child1.addChild("innerChild1OfChild1");
|
||||
Node<String> innerChild2 = child1.addChild("innerChild2OfChild1");
|
||||
Node<String> innerChild3 = child1.addChild("innerChild3OfChild1");
|
||||
|
||||
System.out.println("-" + child1);
|
||||
|
||||
System.out.println("--" + innerChild1);
|
||||
System.out.println("--" + innerChild2);
|
||||
System.out.println("--" + innerChild3);
|
||||
|
||||
System.out.println("Level of child1: " + child1.getLevel());
|
||||
System.out.println("Level of innerChild2 in Child1: " + innerChild2.getLevel());
|
||||
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
||||
System.out.println(y.getRoot());
|
||||
|
||||
Node<String> child2 = x.addChild("child2");
|
||||
{
|
||||
Node<String> innerChild1 = child2.addChild("innerChild2OfChild2");
|
||||
Node<String> innerChild2 = child2.addChild("innerChild3OfChild2");
|
||||
Node<String> innerChild3 = child2.addChild("innerChild4OfChild2");
|
||||
{
|
||||
Node<String> innerChild4 = innerChild3.addChild("innerChild4OfChild3");
|
||||
System.out.println(innerChild4.getLevel());
|
||||
System.out.println("\nIs inner Child4 Leaf? " + innerChild4.isLeaf());
|
||||
}
|
||||
|
||||
System.out.println("-" + child2);
|
||||
|
||||
System.out.println("--" + innerChild1);
|
||||
System.out.println("--" + innerChild2);
|
||||
System.out.println("--" + innerChild3);
|
||||
|
||||
System.out.println("Level of child1: " + child2.getLevel());
|
||||
System.out.println("Level of innerChild2 in Child2: " + innerChild2.getLevel());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user