diff --git a/src/ca/bcit/comp1510/lab02/Student.java b/src/ca/bcit/comp1510/lab02/Student.java index aa0d4eb..f9106e3 100644 --- a/src/ca/bcit/comp1510/lab02/Student.java +++ b/src/ca/bcit/comp1510/lab02/Student.java @@ -6,7 +6,7 @@ package ca.bcit.comp1510.lab02; * @version 1.0.0 */ public class Student { - + /** * Student holds student info. * @param name student name diff --git a/src/ca/bcit/comp1510/lab07/DebugStar.java b/src/ca/bcit/comp1510/lab07/DebugStar.java new file mode 100644 index 0000000..c18fa0b --- /dev/null +++ b/src/ca/bcit/comp1510/lab07/DebugStar.java @@ -0,0 +1,93 @@ +package ca.bcit.comp1510.lab07; + +/** + * DebugStar does something. + * @author Braeden Sowinski + * @version 1.0.0 + */ +public class DebugStar { + + /** + * main drives the program. + * @param args unused + */ + public static void main(String[] args) { + final int six = 6; + final int seven = 7; + run("+", six, seven); + run("-", six, seven); + run(six); + } + + /** + * Operation gets operation. + * @param key string + * @return Operation + */ + private static Operation getOperation(final String key) { + final Operation operation; + if (key.equals("+")) { + operation = new Add(); + } else { + operation = new Subtract(); + } + + return (operation); + } + + /** + * run runs stuff. + * @param key final string + * @param a final int + * @param b final int + */ + private static void run(final String key, final int a, final int b) { + final Operation operation; + final int result; + operation = getOperation(key); + result = operation.perform(a, b); + System.out.println("result = " + result); + } + + /** + * run runs stuff. + * @param n final int + */ + private static void run(final int n) { + final Factorial factorial; + final int result; + factorial = new Factorial(); + result = factorial.perform(n); + System.out.println("result = " + result); + } + +} + +interface Operation { + int perform(int a, int b); +} + +class Add implements Operation { + @Override + public int perform(final int a, final int b) { + return (a + b); + } +} + +class Subtract implements Operation { + @Override + public int perform(final int a, final int b) { + return (a - b); + } +} + +class Factorial { + int perform(final int n) { + int ret; + ret = 1; + for (int i = 1; i < n; i++) { + ret *= i; + } + return (ret); + } +} diff --git a/src/ca/bcit/comp1510/lab07/FibonacciBuggy.java b/src/ca/bcit/comp1510/lab07/FibonacciBuggy.java new file mode 100644 index 0000000..9b4d1e6 --- /dev/null +++ b/src/ca/bcit/comp1510/lab07/FibonacciBuggy.java @@ -0,0 +1,65 @@ +package ca.bcit.comp1510.lab07; +/* + * Version 1 (Buggy). + * + * This programs asks the user to enter a number "n" (bigger than 2). + * It then prints out the first "n" numbers of the Fibonacci Sequence. + * Each number is the sum of the two previous numbers. + * + * Example: The output for n=11 should look exactly like this: + * + * 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ... + * + * Fix all compile-time and run-time errors. + * + *@author Carly Orr + *@version 1 + */ + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +/** + * FibonacciBuggy is littered with bugs. + * @author BCIT + * @version 1.0.0 + */ +public class FibonacciBuggy { + + /** + * main program entry. + * @param args unused + */ + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = 0; + while (n <= 2) { + System.out.println("Enter a number bigger than 2: "); + n = scanner.nextInt(); + } + printList(getFiboList(n)); + scanner.close(); + } + + private static List getFiboList(int n) { + List f = new ArrayList(n); + f.add(0); + f.add(1); + int i = 2; + while (i < n) { + f.add(f.get(i - 1) + f.get(i - 2)); + i++; + } + return f; + } + + private static void printList(List fiboList) { + int i = 0; + while (i <= fiboList.size() - 1) { + System.out.println(fiboList.get(i)); + i++; + } + System.out.println("..."); + } +} diff --git a/src/ca/bcit/comp1510/lab07/TestThis.java b/src/ca/bcit/comp1510/lab07/TestThis.java new file mode 100644 index 0000000..b66e35a --- /dev/null +++ b/src/ca/bcit/comp1510/lab07/TestThis.java @@ -0,0 +1,49 @@ +package ca.bcit.comp1510.lab07; + +import java.util.List; + +/** + * Provide code for testing. + * @author blink + * @version 1 + * + */ +public class TestThis { + + /** + * Return the largest value in the parameters. + * @param a first number + * @param b second number + * @param c third number + * @return largest of three numbers + */ + public int largest(int a, int b, int c) { + int max = a; + if (b > max) { + max = b; + } + if (c > max) { + max = c; + } + return max; + } + + /** + * Return the largest value in the parameters. + * @param a List of integers (use ArrayList in test) + * @return largest of numbers in List + */ + public int largest(List a) { + int max = a.get(0); + int i = 1; + while (i < a.size()) { + if (a.get(i) > max) { + max = a.get(i); + } + i++; + } + + return max; + } + +} diff --git a/src/ca/bcit/comp1510/lab08/Date.java b/src/ca/bcit/comp1510/lab08/Date.java new file mode 100644 index 0000000..d15edcb --- /dev/null +++ b/src/ca/bcit/comp1510/lab08/Date.java @@ -0,0 +1,152 @@ +package ca.bcit.comp1510.lab08; + +import java.util.Scanner; + +/** + * Represent a valid Gregorian date on or after 24 February 1582. + * @author blink + * @version 1.0.0 + * @param day day of month. 1 .. max days in month + * @param month month of year. 1 .. 12 + * @param year year in Gregorian calendar. 1582 .. 2999 + */ +public record Date(int day, int month, int year) { + + /** Date constructor. + * @throws IllegalArgumentException if invalid input. */ + public Date { + if (!isDateValid(day, month, year)) { + throw new IllegalArgumentException("Invalid Date arguments"); + } + } + + /** + * isMonthValid validates stuff. + * @param m int mont number + * @return boolean valid month number + */ + public static boolean isMonthValid(int m) { + final int minMonth = 1; + final int maxMonth = 12; + return minMonth <= m && m <= maxMonth; + } + + /** + * isYearValid validates stuff. + * @param year int year number + * @return boolean valid year number + */ + public static boolean isYearValid(int year) { + final int min = 1582; + final int max = 2999; + return min <= year && year <= max; + } + + /** + * isLeapYear validates stuff. + * @param year int year number + * @return boolean is leap year + */ + public static boolean isLeapYear(int year) { + final int mod1 = 4; + final int mod2 = 100; + final int mod3 = 400; + return (year % mod3 == 0) + || (year % mod1 == 0 && year % mod2 != 0); + } + + /** + * daysInMonth gives info. + * @param month int month number + * @param isLeapYear boolean + * @return int number of days + */ + public static int daysInMonth(int month, boolean isLeapYear) { + final int min = 1; + final int max = 12; + if (max < month || month < min) { + return 0; + } + + if (month == 2) { + final int nonLeapYear = 28; + final int leapYear = 29; + return isLeapYear ? leapYear : nonLeapYear; + } + + final int daysThirtyOne = 31; + final int daysThirty = 30; + final int half = 7; + + boolean thirtyOne = month <= half + ? (month % 2) == 1 : ((month + 1) % 2) == 1; + + return thirtyOne ? daysThirtyOne : daysThirty; + } + + /** + * isValidDate validates date. + * @param day int + * @param month int + * @param year int + * @return boolean + */ + public static boolean isDateValid(int day, int month, int year) { + boolean validYear = isYearValid(year); + boolean leapYear = isLeapYear(year); + boolean validMonth = isMonthValid(month); + boolean validDay = day <= daysInMonth(month, leapYear) + && day >= 1; + + return validYear && validMonth && validDay; + } + + private static int readValidInt(String message, Scanner scan) { + boolean validInt = false; + int data = 0; + do { + System.out.print(message); + + if (!scan.hasNextInt()) { + System.out.println(scan.next() + " is not a valid integer.\n"); + continue; + } + + data = scan.nextInt(); + validInt = true; + + } while (!validInt); + + return data; + } + + /** + * main program entry. + * @param args unused + */ + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int day = readValidInt("Enter a day (int): ", scan); + int month = readValidInt("Enter a month (int): ", scan); + int year = readValidInt("Enter a year (int): ", scan); + scan.close(); + + System.out.println(); + + boolean validYear = Date.isYearValid(year); + boolean validMonth = Date.isMonthValid(month); + boolean validDay = Date.isDateValid(day, month, year); + + boolean leapYear = Date.isLeapYear(year); + + int daysInMonth = Date.daysInMonth(month, leapYear); + + System.out.println("Valid year: " + validYear); + System.out.println("Is Leap Year: " + leapYear); + System.out.println("Valid month: " + validMonth); + System.out.println("# of Days: " + daysInMonth); + System.out.println("Valid day: " + validDay); + + Date newDate = new Date(day, month, year); + } +} diff --git a/src/ca/bcit/comp1510/lab08/Games.java b/src/ca/bcit/comp1510/lab08/Games.java new file mode 100644 index 0000000..ecbd671 --- /dev/null +++ b/src/ca/bcit/comp1510/lab08/Games.java @@ -0,0 +1,157 @@ +package ca.bcit.comp1510.lab08; + +import java.util.Scanner; +import java.util.random.RandomGenerator; + +/** + * Games game stuff. + * @author Braeden Sowinski + * @version 1.0.0 + */ +public class Games { + + /** score holds user score. */ + private int score; + + /** scan scans stuff. */ + private Scanner scan; + + /** rand generates random stuff. */ + private RandomGenerator rand; + + /** Games constructor. */ + public Games() { + score = 0; + scan = new Scanner(System.in); + rand = RandomGenerator.getDefault(); + } + + /** guessANumber is a minigame. */ + public void guessANumber() { + System.out.println("I've picked a random number between 0 and 100"); + System.out.println("Can you guess it?"); + System.out.println("Guess the number"); + + final int maxNum = 101; + int randomNumber = rand.nextInt(maxNum); + + final int max = 5; + int guess = -1; + for (int i = 0; i < max; i++) { + if (guess != -1) { + String diff = guess > randomNumber ? "high" : "low"; + System.out.println("Too " + diff + ", guess again!"); + } + + guess = scan.nextInt(); + if (guess == randomNumber) { + System.out.println("RIGHT!"); + System.out.println("5 points!\n"); + final int won = 5; + score += won; + return; + } + } + + System.out.println("You failed!"); + System.out.println("No points!\n"); + } + + /** valid validates user input for rps. + * @param in String input + * @return valid boolean + * */ + private static boolean valid(String in) { + return ( + in.toUpperCase().equals("ROCK") + || in.toUpperCase().equals("PAPER") + || in.toUpperCase().equals("SCISSORS") + ); + } + + /** rockPaperScissors is a fun game. */ + public void rockPaperScissors() { + final int maxNum = 3; + final int rock = 0; + final int paper = 1; + int handOpt = rand.nextInt(maxNum); + + String hand = handOpt == rock ? "ROCK" + : handOpt == paper ? "PAPER" + : "SCISSORS"; + + System.out.println("I've picked on of ROCK, PAPER, and SCISSORS"); + System.out.println("Which one do you choose?"); + + String input = ""; + do { + System.out.print("> "); + input = scan.next(); + if (!valid(input)) { + System.out.println(input + " is not valid."); + } + } while (!valid(input)); + + input = input.toUpperCase(); + + System.out.println(input + " vs " + hand); + + if ( + input.equals("ROCK") && hand.equals("SCISSORS") + || input.equals("SCISSORS") && hand.equals("PAPER") + || input.equals("PAPER") && hand.equals("ROCK") + ) { + System.out.println("You win!"); + System.out.println("5 points!\n"); + final int win = 5; + score += win; + } else if (input.equals(hand)) { + System.out.println("Tie!"); + System.out.println("No points gained or losed."); + } else { + System.out.println("You lost!"); + System.out.println("Minus 3 points!\n"); + final int loss = -3; + score += loss; + } + } + + /** + * play starts the game. + */ + public void play() { + System.out.println("Welcome to the Games program!"); + + final int scoreOpt = 1; + final int guessOpt = 2; + final int rpsOpt = 3; + final int quitOpt = 4; + + int option = -1; + do { + System.out.println("Make your choice (enter a number):"); + System.out.println("1. See your score"); + System.out.println("2. Guess a number"); + System.out.println("3. Play Rock, Paper, Scissors"); + System.out.println("4. Quit"); + System.out.print("> "); + + if (!scan.hasNextInt()) { + System.out.println(scan.next() + " is not a valid option.\n"); + continue; + } + + option = scan.nextInt(); + + switch (option) { + case scoreOpt -> System.out.println("Score: " + score + "\n"); + case guessOpt -> guessANumber(); + case rpsOpt -> rockPaperScissors(); + case quitOpt -> System.out.println("Quiting..."); + default -> System.out.println(option + + " is not a valid option.\n"); + } + + } while (option != quitOpt); + } +} diff --git a/src/ca/bcit/comp1510/lab08/GamesDriver.java b/src/ca/bcit/comp1510/lab08/GamesDriver.java new file mode 100644 index 0000000..f9fc418 --- /dev/null +++ b/src/ca/bcit/comp1510/lab08/GamesDriver.java @@ -0,0 +1,18 @@ +package ca.bcit.comp1510.lab08; + +/** + * GamesDriver drives games. + * @author Braeden Sowinski + * @version 1.0.0 + */ +public class GamesDriver { + + /** + * main program entry. + * @param args unused. + */ + public static void main(String[] args) { + Games game = new Games(); + game.play(); + } +} diff --git a/test/ca/bcit/comp1510/lab07/TestThisTest.java b/test/ca/bcit/comp1510/lab07/TestThisTest.java new file mode 100644 index 0000000..6e9123d --- /dev/null +++ b/test/ca/bcit/comp1510/lab07/TestThisTest.java @@ -0,0 +1,62 @@ +package ca.bcit.comp1510.lab07; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +class TestThisTest { + + TestThis test = new TestThis(); + + @Test + void testLargestIntIntInt1() { + int result = test.largest(3, 2, 1); + assertEquals(3, result); + } + + @Test + void testLargestIntIntInt2() { + int result = test.largest(4, 6, 5); + assertEquals(6, result); + } + + @Test + void testLargestIntIntInt3() { + int result = test.largest(9, 8, 12); + assertEquals(12, result); + } + + @Test + void testLargestListOfInteger1() { + ArrayList list = new ArrayList(); + list.add(3); + + int result = test.largest(list); + assertEquals(3, result); + } + + @Test + void testLargestListOfInteger2() { + ArrayList list = new ArrayList(); + list.add(4); + list.add(6); + list.add(5); + + int result = test.largest(list); + assertEquals(6, result); + } + + + @Test + void testLargestListOfInteger3() { + ArrayList list = new ArrayList(); + list.add(9); + list.add(8); + list.add(12); + list.add(4); + + int result = test.largest(list); + assertEquals(12, result); + } +} diff --git a/test/ca/bcit/comp1510/lab08/DateTest.java b/test/ca/bcit/comp1510/lab08/DateTest.java new file mode 100644 index 0000000..71ce3f7 --- /dev/null +++ b/test/ca/bcit/comp1510/lab08/DateTest.java @@ -0,0 +1,163 @@ +/** + * + */ +package ca.bcit.comp1510.lab08; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +/** + * + */ +class DateTest { + + @Test + void testIsMonthValid() { + int month = 6; + boolean valid = Date.isMonthValid(month); + assertEquals(true, valid); + } + + @Test + void testIsMonthValid1() { + int month = 21; + boolean valid = Date.isMonthValid(month); + assertEquals(false, valid); + } + + @Test + void testIsYearValid() { + int year = 2005; + boolean valid = Date.isYearValid(year); + assertEquals(true, valid); + } + + @Test + void testIsYearValid1() { + int year = 1450; + boolean valid = Date.isYearValid(year); + assertEquals(false, valid); + } + + @Test + void testIsYearValid2() { + int year = 3821; + boolean valid = Date.isYearValid(year); + assertEquals(false, valid); + } + + @Test + void testIsLeapYear() { + int year = 2005; + boolean leap = Date.isLeapYear(year); + assertEquals(false, leap); + } + + @Test + void testIsLeapYear1() { + int year = 2004; + boolean leap = Date.isLeapYear(year); + assertEquals(true, leap); + } + + @Test + void testIsLeapYear2() { + int year = 1700; + boolean leap = Date.isLeapYear(year); + assertEquals(false, leap); + } + + @Test + void testIsLeapYear3() { + int year = 2100; + boolean leap = Date.isLeapYear(year); + assertEquals(false, leap); + } + + @Test + void testDaysInMonth() { + int month = 4; + boolean leapYear = false; + int days = Date.daysInMonth(month, leapYear); + assertEquals(30, days); + } + + @Test + void testDaysInMonth1() { + int month = 5; + boolean leapYear = false; + int days = Date.daysInMonth(month, leapYear); + assertEquals(31, days); + } + + @Test + void testDaysInMonth2() { + int month = 2; + boolean leapYear = false; + int days = Date.daysInMonth(month, leapYear); + assertEquals(28, days); + } + + @Test + void testDaysInMonth3() { + int month = 2; + boolean leapYear = true; + int days = Date.daysInMonth(month, leapYear); + assertEquals(29, days); + } + + @Test + void testDaysInMonth4() { + int month = 16; + boolean leapYear = true; + int days = Date.daysInMonth(month, leapYear); + assertEquals(0, days); + } + + @Test + void testIsDateValid() { + int day = 26; + int month = 4; + int year = 2005; + boolean valid = Date.isDateValid(day, month, year); + assertEquals(true, valid); + } + + @Test + void testIsDateValid1() { + int day = 33; + int month = 8; + int year = 1999; + boolean valid = Date.isDateValid(day, month, year); + assertEquals(false, valid); + } + + @Test + void testConstructor() { + int day = 26; + int month = 4; + int year = 2005; + + try { + Date _date = new Date(day, month, year); + assertEquals(true, true); + } catch (IllegalArgumentException e) { + fail("IllegalArgumentException on valid date creation"); + } + } + + @Test + void testConstructor1() { + int day = 33; + int month = 8; + int year = 1999; + + try { + Date _date = new Date(day, month, year); + fail("Date created with invalid date"); + } catch (IllegalArgumentException e) { + assertEquals(true, true); + } + } +}