diff --git a/src/ca/bcit/comp1510/lab10/Name.java b/src/ca/bcit/comp1510/lab10/Name.java new file mode 100644 index 0000000..dac28a3 --- /dev/null +++ b/src/ca/bcit/comp1510/lab10/Name.java @@ -0,0 +1,121 @@ +package ca.bcit.comp1510.lab10; + +/** + * Name does name stuff. + * @author Braeden Sowinski + * @version 1.0.0 + */ +public class Name implements Comparable { + + /** first name string. */ + private final String first; + + /** middle name string. */ + private final String middle; + + /** last name string. */ + private final String last; + + + /** + * Name constructor. + * @param first name string + * @param middle name string + * @param last name string + */ + public Name( + String first, + String middle, + String last + ) throws IllegalArgumentException { + if (!valid(first) || !valid(last)) { + throw new IllegalArgumentException( + "First or last name cannot be null or empty." + ); + } + + this.first = first; + this.middle = middle; + this.last = last; + } + + /** + * Name constructor. + * @param first name string + * @param last name string + */ + public Name( + String first, + String last + ) throws IllegalArgumentException { + if (!valid(first) || !valid(last)) { + throw new IllegalArgumentException( + "First or last name cannot be null or empty." + ); + } + + this.first = first; + this.middle = null; + this.last = last; + } + + /** + * valid name entry. + * @param n string test + * @return valid boolean + */ + private boolean valid(String n) { + return !(n.equals("") || n.equals(null)); + } + + /** + * getFirst. + * @return first name string. + */ + public String getFirst() { + return this.first; + } + + /** + * getMiddle. + * @return middle name string. + */ + public String getMiddle() { + return this.middle; + } + + /** + * getLast. + * @return last name string. + */ + public String getLast() { + return this.last; + } + + /** + * compareTo other Name. + * @param n Name object + * @return compared int + */ + @Override + public int compareTo(Name n) { + if (!this.last.equals(n.last)) { + return this.last.compareTo(n.last); + } else { + if (!this.first.equals(n.first)) { + return this.first.compareTo(n.first); + } else { + return this.middle.compareTo(n.middle); + } + } + } + + /** + * toString names. + * @return first last name string + */ + public String toString() { + return this.middle == null ? this.last + " " + this.first + : this.last + " " + this.middle + " " + this.first; + } +} diff --git a/src/ca/bcit/comp1510/lab10/NameDriver.java b/src/ca/bcit/comp1510/lab10/NameDriver.java new file mode 100644 index 0000000..3e45957 --- /dev/null +++ b/src/ca/bcit/comp1510/lab10/NameDriver.java @@ -0,0 +1,32 @@ +package ca.bcit.comp1510.lab10; + +import java.util.List; +import java.util.ArrayList; + +/** + * NameDriver drives Name. + * @author Braeden Sowinski + * @version 1.0.0 + */ +public class NameDriver { + + /** + * main program entry. + * @param args unused + */ + public static void main(String[] args) { + List names = new ArrayList(); + + names.add(new Name("Braeden", "Sowinski")); + names.add(new Name("Mitch", "Paredes")); + names.add(new Name("Calvin", "B", "LastName")); + names.add(new Name("Joaquin", "Paredes")); + names.add(new Name("Calvin", "A", "LastName")); + + names.sort(null); + + for (int i = 0; i < names.size(); i++) { + System.out.println(names.get(i).toString()); + } + } +} diff --git a/src/ca/bcit/comp1510/lab10/RandomWalker.java b/src/ca/bcit/comp1510/lab10/RandomWalker.java new file mode 100644 index 0000000..d3f4f99 --- /dev/null +++ b/src/ca/bcit/comp1510/lab10/RandomWalker.java @@ -0,0 +1,149 @@ +package ca.bcit.comp1510.lab10; + +import java.util.random.RandomGenerator; + +/** + * RandomWalker walks in funny ways. + * @author Braeden Sowinski + * @version 1.0.0 + */ +public class RandomWalker { + + private enum Directions { + /** Up direction. */ + Up, + /** Down direction. */ + Down, + /** Left direction. */ + Left, + /** Right direction. */ + Right + } + + /** x coordinate of walker. */ + private int x; + + /** y coordinate of walker. */ + private int y; + + /** max steps of walker. */ + private int max; + + /** bound of walk areas. */ + private int bound; + + /** steps taken. */ + private int steps; + + /** maximumDistance travelled. */ + private int maximumDistance; + + /** + * RandomWalker Constructor with no starting x, y. + * @param maxSteps of walker + * @param boundary of grid + */ + public RandomWalker(int maxSteps, int boundary) { + this.max = maxSteps; + this.bound = boundary; + this.x = 0; + this.y = 0; + this.steps = 0; + this.maximumDistance = 0; + } + + /** + * RandomWalker Constructor with starting x, y. + * @param maxSteps of walker + * @param x starting position + * @param y starting position + * @param boundary of grid + */ + public RandomWalker(int maxSteps, int x, int y, int boundary) { + this.max = maxSteps; + this.bound = boundary; + this.x = x; + this.y = y; + this.steps = 0; + this.maximumDistance = 0; + } + + /** + * toString returns string representation. + * @return RandomWalker string representation + * */ + public String toString() { + return "(" + this.x + ", " + this.y + ")" + + " max steps: " + this.max + + " bounds: " + this.bound; + } + + /** + * takeStep in a random direction. + */ + public void takeStep() { + RandomGenerator rand = RandomGenerator.getDefault(); + + int i = rand.nextInt(Directions.values().length); + Directions dir = Directions.values()[i]; + + switch (dir) { + case Directions.Up -> this.y++; + case Directions.Down -> this.y--; + case Directions.Left -> this.x--; + default -> this.x++; + } + + this.steps++; + + this.maximumDistance = max( + this.maximumDistance, + max(Math.abs(this.x), Math.abs(this.y)) + ); + } + + /** + * moreSteps available? + * @return more steps available + */ + public boolean moreSteps() { + return this.steps < this.max; + } + + /** + * inBounds check if x, y coords are in bounds. + * @return in boundary + */ + public boolean inBounds() { + return this.x <= this.bound && this.x >= (-1 * this.bound) + && this.y <= this.bound && this.y >= (-1 * this.bound); + } + + /** + * walk simulation till out of bounds + * or out of steps. + */ + public void walk() { + while (moreSteps() && inBounds()) { + takeStep(); + } + } + + /** + * max of a and b. + * @param a int + * @param b int + * @return max of a and b + * */ + private int max(int a, int b) { + return a > b ? a : b; + } + + /** + * getMaximumDistance. + * @return maximumDistance int + */ + public int getMaximumDistance() { + return this.maximumDistance; + } +} diff --git a/src/ca/bcit/comp1510/lab10/TestWalker.java b/src/ca/bcit/comp1510/lab10/TestWalker.java new file mode 100644 index 0000000..a65ef45 --- /dev/null +++ b/src/ca/bcit/comp1510/lab10/TestWalker.java @@ -0,0 +1,80 @@ +package ca.bcit.comp1510.lab10; + +import java.util.Scanner; + +/** + * TestWalker to test RandomWalker. + * @author Braeden Sowinski + * @version 1.0.0 + */ +public class TestWalker { + + /** + * readValidInt reads and validates an integer. + * @param msg to print; + * @param scan to read input + * @return validated int + */ + private static int readValidInt(String msg, Scanner scan) { + int number = 0; + boolean valid = false; + + do { + System.out.print(msg); + + if (!scan.hasNextInt()) { + System.out.println(scan.next() + " is not a valid number.\n"); + continue; + } + + number = scan.nextInt(); + valid = true; + + } while (!valid); + + return number; + } + + /** + * main program entry. + * @param args unused + */ + public static void main(String[] args) { + + final int maxSteps = 10; + final int bound = 5; + + RandomWalker walker = new RandomWalker(maxSteps, bound); + + Scanner scan = new Scanner(System.in); + int numSteps = readValidInt("Enter a maximum number of steps: ", scan); + int boundary = readValidInt("Enter a boundary size: ", scan); + scan.close(); + + RandomWalker userWalker = new RandomWalker(numSteps, boundary); + + System.out.println(); + System.out.println(walker.toString()); + System.out.println(userWalker.toString()); + final int steps = 5; + for (int i = 0; i < steps; i++) { + walker.takeStep(); + userWalker.takeStep(); + + System.out.println(walker.toString()); + System.out.println(walker.getMaximumDistance()); + System.out.println(userWalker.toString()); + System.out.println(userWalker.getMaximumDistance()); + } + + System.out.println("\nFar Walker"); + final int bigSteps = 200; + final int bigBound = 10; + RandomWalker farWalker = new RandomWalker(bigSteps, bigBound); + + System.out.println(farWalker.toString()); + farWalker.walk(); + System.out.println(farWalker.toString()); + System.out.println(farWalker.getMaximumDistance()); + } +}