Commit 39ae788
Changed files (5)
src
src/Q10/Station.java
@@ -1,3 +1,11 @@
+/**
+ * Assignment 2, COMP268 Class: TrainTimeTable.java
+ *
+ * @description Provides a class to manage a train schedule.
+ * @author: mo khan Student ID: 3431709
+ * @date August 5, 2019
+ * @version 1.0
+ */
package Q10;
import java.text.*;
src/Q10/TrainTimeTable.java
@@ -72,9 +72,7 @@ public class TrainTimeTable {
return null;
}
- /**
- * This is the main entry point to the console application.
- */
+ /** This is the main entry point to the console application. */
public static void main(String[] args) {
System.out.println("=== Question 10 ===");
LinkedList<Station> stations =
src/Q2/README.md
@@ -3,118 +3,117 @@ Learning Profile for Assignment #2, And Question #2
Name: Mo Khan
Student ID: 3431709
-1. Problem Statement:
-
-```text
-Write a program that plays the Rock-Paper-Scissors-Lizard-Spock game.
-Refer to http://en.wikipedia.org/wiki/Rock-paper-scissors-lizard-Spock for more information.
-Normally, one player is a human and the other is the computer program.
-However, in this exercise, the program will generate two players who play against each other.
-The play continues until either of the computer-generated players wins four consecutive times.
-In this game, two random integers are generated in the range of [1 to 5], one per player.
-1 refers to Rock, 2 refers to Paper, 3 refers to Scissors, 4 refers to Lizard, and 5 refers to Spock.
-
-For example, if the computer randomly generates integers 2 and 5 in the first iteration,
-2 is for the first player and 5 is for the second player.
-Based on Rule 8 in the following 10 rules, Paper (2) disproves Spock (5), so Player 1 wins.
-Repeat it to generate one more pair and determine who wins that iteration.
-Continue the iterations until one player wins four consecutive times.
-
-Rule 1: Scissors cut paper
-Rule 2: Paper covers rock
-Rule 3: Rock crushes lizard
-Rule 4: Lizard poisons Spock
-Rule 5: Spock smashes (or melts) scissors
-Rule 6: Scissors decapitate lizard
-Rule 7: Lizard eats paper
-Rule 8: Paper disproves Spock
-Rule 9: Spock vaporizes rock
-Rule 10: Rock breaks scissors
-```
-
-2. Description of the Code:
-
-The main game loop can be found in `main`. This will loop until a winner
-has won at least 4 consecutive times.
-
-The `Random` class is used to generate a pseudo random number between
-0 - 4. To keep the numbers between 1 - 5, I have incremented this value
-by 1.
-
-The `play` method prints out the choice for both players, then
-determines the winner and adjusts the last winner and consecutive wins
-fields based on the winner.
-
-3. Errors and Warnings:
-
-When a tie occurs, I assume the previous winner remains the current
-winner.
-
-This program does not require any user input so the possibilities
-remains low. A possible condition that can occur is an infinite loop due
-to the random number generator producing results that does not allow a
-single player to win 4 consecutive times. One way to handle this
-scenario, is to set a maximum number of rounds that can be played. I
-chose not to implement this.
-
-4. Sample Input and Output:
-
-An example of a short game:
-
-```bash
-Starting a new game of Rock, paper, scissors, lizard, spock...
-
-Round: 1
---------------------------------------
-Player 1: scissor
-Player 2: paper
-Player 1 has 1 consecutive wins.
-
-Round: 2
---------------------------------------
-Player 1: scissor
-Player 2: lizard
-Player 1 has 2 consecutive wins.
-
-Round: 3
---------------------------------------
-Player 1: rock
-Player 2: lizard
-Player 1 has 3 consecutive wins.
-
-Round: 4
---------------------------------------
-Player 1: paper
-Player 2: rock
-Player 1 has 4 consecutive wins.
-
-**************************************
- The winner is player 1!
-**************************************
-```
-
-An example of a longer game:
-```bash
-...
-Round: 72
---------------------------------------
-Player 1: paper
-Player 2: paper
-Player 1 has 3 consecutive wins.
-
-Round: 73
---------------------------------------
-Player 1: rock
-Player 2: scissor
-Player 1 has 4 consecutive wins.
-
-**************************************
- The winner is player 1!
-**************************************
-```
-
-5. Discussion:
-
-I chose to print additional information to the console to make it
-easier to see how many rounds were played and to ensure that the
-correct winner is determined.
+1. Problem Statement
+
+ Write a program that plays the Rock-Paper-Scissors-Lizard-Spock game.
+ Refer to http://en.wikipedia.org/wiki/Rock-paper-scissors-lizard-Spock for more information.
+ Normally, one player is a human and the other is the computer program.
+ However, in this exercise, the program will generate two players who play against each other.
+ The play continues until either of the computer-generated players wins four consecutive times.
+ In this game, two random integers are generated in the range of [1 to 5], one per player.
+ 1 refers to Rock, 2 refers to Paper, 3 refers to Scissors, 4 refers to Lizard, and 5 refers to Spock.
+
+ For example, if the computer randomly generates integers 2 and 5 in the first iteration,
+ 2 is for the first player and 5 is for the second player.
+ Based on Rule 8 in the following 10 rules, Paper (2) disproves Spock (5), so Player 1 wins.
+ Repeat it to generate one more pair and determine who wins that iteration.
+ Continue the iterations until one player wins four consecutive times.
+
+ 1. Scissors cut paper
+ 1. Paper covers rock
+ 1. Rock crushes lizard
+ 1. Lizard poisons Spock
+ 1. Spock smashes (or melts) scissors
+ 1. Scissors decapitate lizard
+ 1. Lizard eats paper
+ 1. Paper disproves Spock
+ 1. Spock vaporizes rock
+ 1. Rock breaks scissors
+
+1. Description of the Code
+
+ The main game loop can be found in `main`. This will loop until a winner
+ has won at least 4 consecutive times.
+
+ The `Random` class is used to generate a pseudo random number between
+ 0 - 4. To keep the numbers between 1 - 5, I have incremented this value
+ by 1.
+
+ The `play` method prints out the choice for both players, then
+ determines the winner and adjusts the last winner and consecutive wins
+ fields based on the winner.
+
+1. Errors and Warnings
+
+ When a tie occurs, I assume the previous winner remains the current
+ winner.
+
+ This program does not require any user input so the possibilities
+ remains low. A possible condition that can occur is an infinite loop due
+ to the random number generator producing results that does not allow a
+ single player to win 4 consecutive times. One way to handle this
+ scenario, is to set a maximum number of rounds that can be played. I
+ chose not to implement this.
+
+1. Sample Input and Output
+
+ 1. An example of a short game:
+
+ ```bash
+ Starting a new game of Rock, paper, scissors, lizard, spock...
+
+ Round: 1
+ --------------------------------------
+ Player 1: scissor
+ Player 2: paper
+ Player 1 has 1 consecutive wins.
+
+ Round: 2
+ --------------------------------------
+ Player 1: scissor
+ Player 2: lizard
+ Player 1 has 2 consecutive wins.
+
+ Round: 3
+ --------------------------------------
+ Player 1: rock
+ Player 2: lizard
+ Player 1 has 3 consecutive wins.
+
+ Round: 4
+ --------------------------------------
+ Player 1: paper
+ Player 2: rock
+ Player 1 has 4 consecutive wins.
+
+ **************************************
+ The winner is player 1!
+ **************************************
+ ```
+
+ 1. An example of a longer game:
+
+ ```bash
+ ...
+ Round: 72
+ --------------------------------------
+ Player 1: paper
+ Player 2: paper
+ Player 1 has 3 consecutive wins.
+
+ Round: 73
+ --------------------------------------
+ Player 1: rock
+ Player 2: scissor
+ Player 1 has 4 consecutive wins.
+
+ **************************************
+ The winner is player 1!
+ **************************************
+ ```
+
+1. Discussion
+
+ I chose to print additional information to the console to make it
+ easier to see how many rounds were played and to ensure that the
+ correct winner is determined.
src/Q3/README.md
@@ -5,107 +5,105 @@ Student ID: 3431709
1. Problem Statement:
-```text
-Credit card numbers follow certain patterns.
-A credit card number must have between 13 and 16 digits.
-It must start with 4 for Visa cards, 5 for Master cards, 37 for American Express cards, and 6 for Discover cards.
-In 1954, Hans Luhn of IBM proposed the following algorithm for validating credit card numbers:
-* a. Double every second digit from right to left (e.g., if number is 3 => 3 * 2 => 6) and add them together.
-* b. If this doubling results in a two-digit number, then add the two digits to get a single-digit number (e.g., if number is 5 => 5 * 2 => 10 => 1+0 => 1).
+ Credit card numbers follow certain patterns.
+ A credit card number must have between 13 and 16 digits.
+ It must start with 4 for Visa cards, 5 for Master cards, 37 for American Express cards, and 6 for Discover cards.
+ In 1954, Hans Luhn of IBM proposed the following algorithm for validating credit card numbers:
-So, for the credit card number 4388576018402626, doubling all second digits from the right results in
+ 1. Double every second digit from right to left (e.g., if number is 3 => 3 * 2 => 6) and add them together.
+ 1. If this doubling results in a two-digit number, then add the two digits to get a single-digit number (e.g., if number is 5 => 5 * 2 => 10 => 1+0 => 1).
-```text
- (2 * 2 = 4) + (2 * 2 = 4) + (4 * 2 = 8) + (1 * 2 = 2) + (6 * 2 = 12 = 1 + 2 = 3) + (5 * 2 = 10 = 1 + 0 = 1) + (8 * 2 = 16 = 1 + 6 = 7) + (4 * 2 = 8).
-```
+ So, for the credit card number 4388576018402626, doubling all second digits from the right results in
-This totals to 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37.
+ (2 * 2 = 4) + (2 * 2 = 4) + (4 * 2 = 8) + (1 * 2 = 2) + (6 * 2 = 12 = 1 + 2 = 3) + (5 * 2 = 10 = 1 + 0 = 1) + (8 * 2 = 16 = 1 + 6 = 7) + (4 * 2 = 8).
-Add all digits in the odd places from right to left.
-The leftmost digit of the credit card number is at index 0;
+ This totals to 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37.
-6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38.
+ Add all digits in the odd places from right to left.
+ The leftmost digit of the credit card number is at index 0;
-Add results from steps (a) and (b) and see if divisible by 10.
-If it is, then the card number is valid; otherwise invalid.
+ 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38.
-37 + 38 = 75 is not divisible by 10, so it is an invalid credit card number.
+ Add results from steps (a) and (b) and see if divisible by 10.
+ If it is, then the card number is valid; otherwise invalid.
-Implement Luhn’s algorithm in a program to determine whether a given credit card number is valid or not.
-You must test if the number of digits in the input is in the valid range (13 to 16), run Luhn’s algorithm to test its validity,
-and if it is valid, print the name of the company that offers that credit card number.
-```
+ 37 + 38 = 75 is not divisible by 10, so it is an invalid credit card number.
-2. Description of the Code:
+ Implement Luhn’s algorithm in a program to determine whether a given credit card number is valid or not.
+ You must test if the number of digits in the input is in the valid range (13 to 16), run Luhn’s algorithm to test its validity,
+ and if it is valid, print the name of the company that offers that credit card number.
-The code implements the expected API as per the class diagram provided
-in the assignment. I also included some additional private methods to
-try to increase the readability of the code. The bulk of the logic for
-the `Luhn` algorithm takes place in the `calculateSums` method.
+1. Description of the Code:
-This method reverses the credit card number then loops through each
-character. As it loops through each character it checks to see if the
-current index is even or odd and applys the appropriate summing rules
-for that index.
+ The code implements the expected API as per the class diagram provided
+ in the assignment. I also included some additional private methods to
+ try to increase the readability of the code. The bulk of the logic for
+ the `Luhn` algorithm takes place in the `calculateSums` method.
-3. Errors and Warnings:
+ This method reverses the credit card number then loops through each
+ character. As it loops through each character it checks to see if the
+ current index is even or odd and applys the appropriate summing rules
+ for that index.
-Length is too short:
+1. Errors and Warnings:
-```bash
-java -cp target/assignment2*.jar ca.mokhan.comp268.App 3
-Enter credit card #:
-1334
-1334 in invalid
-```
+ 1. Length is too short:
-Length is too long:
+ ```bash
+ java -cp target/assignment2*.jar ca.mokhan.comp268.App 3
+ Enter credit card #:
+ 1334
+ 1334 in invalid
+ ```
-```bash
-Enter credit card #:
-41111111111111111
-41111111111111111 in invalid
-````
+ 1. Length is too long:
-Invalid card type:
+ ```bash
+ Enter credit card #:
+ 41111111111111111
+ 41111111111111111 in invalid
+ ````
-```bash
-Enter credit card #:
-1234567890123456
-1234567890123456 in invalid
-```
+ 1. Invalid card type:
-4. Sample Input and Output:
+ ```bash
+ Enter credit card #:
+ 1234567890123456
+ 1234567890123456 in invalid
+ ```
-Valid AMEX card #:
+1. Sample Input and Output
-```bash
-Enter credit card #:
-378282246310005
-378282246310005 is offerred by American Express
-```
+ 1. Valid AMEX card #:
-Valid Discover card #:
+ ```bash
+ Enter credit card #:
+ 378282246310005
+ 378282246310005 is offerred by American Express
+ ```
-```bash
-Enter credit card #:
-6011111111111117
-6011111111111117 is offerred by Discover
-```
+ 1. Valid Discover card #:
-Valid MasterCard #:
+ ```bash
+ Enter credit card #:
+ 6011111111111117
+ 6011111111111117 is offerred by Discover
+ ```
-```bash
-Enter credit card #:
-5555555555554444
-5555555555554444 is offerred by MasterCard
-```
+ 1. Valid MasterCard #:
-Valid Visa card #:
+ ```bash
+ Enter credit card #:
+ 5555555555554444
+ 5555555555554444 is offerred by MasterCard
+ ```
-```bash
-Enter credit card #:
-4012888888881881
-4012888888881881 is offerred by Visa
-```
-5. Discussion:
+ 1. Valid Visa card #:
+
+ ```bash
+ Enter credit card #:
+ 4012888888881881
+ 4012888888881881 is offerred by Visa
+ ```
+
+1. Discussion:
src/Q4/README.md
@@ -5,76 +5,167 @@ Student ID: 3431709
1. Problem Statement:
-```text
-Craps is a dice game where two dice are rolled.
-Each die has six faces representing values:
-
-1, 2, 3, 4, 5, or 6.
-
-1. If the sum is 2, 3, or 12 (called craps), you lose;
-2. If the sum is 7 or 11 (called natural), you win;
-3. If the sum is any other value (4, 5, 6, 8, 9, or 10), a value point is established, and you continue to roll until you either roll a sum of the value point or a 7.
-If the sum of the new roll is equal to the value point, then you win; if the sum of the new roll is equal to 7, then you lose.
-Remember, in option (III), you continue to roll until you get a 7 or the value point.
-
-Sample runs:
-* You rolled 5 + 6 = 11; you win
-* You rolled 1 + 2 = 3; you lose
-* You rolled 2 + 2 = 4; you establish the value point 4;
- – Roll again 2 + 3 = 5; roll
- – Roll again 2 + 1 = 3; roll
- – Roll again 2 + 2 = 4; you win
-* You rolled 2 + 6 = 8; you establish the value point 8;
- – Roll again 4 + 4 = 8; you win
-* You rolled 3 + 2 = 5; you establish the value point 5;
- – Roll again 1 + 1 = 2; roll
- – Roll again 2 + 2 = 4; roll
-* Roll again 1 + 1 = 2; roll
-* Roll again 3 + 4 = 7; you lose
-
-Develop a program that plays craps with a player three times.
-At the end, the program prints the number of times the player won and the number of times the player lost.
-```
-
-2. Description of the Code:
-
-I solved this problem by creating a class called `RandomSumGame` as per
-the class diagram. I added two instance variables named `wins` and
-`losses` to keep track of how many times the player won or lost.
-
-To try to make the API of this class more testable, I chose to pass the
-`PrintStream` in as a parameter to the constructor. This example of
-dependency injection, made it possible to write unit tests to ensure the
-proper output is printed to the stream.
-
-To simplify the problem, I split it up into two types of game play. The
-rules for the initial roll is slightly different for the rules for
-subsequent rolls. I created two methods named `firstPlay` and `subsequentPlay`.
-This made it easy to focus on the rules for the initial roll in the
-`firstPlay` method and the rules for the subsequent roles in the
-`subsequentPlay` method.
-
-To roll the dice, I extracted a method called `roll` that returns a
-random number between 1 - 6.
-
-To keep track of the wins/losses, I delegating to the `win` or `lose`
-methods to print a message to the screen and increment a win/loss
-counter.
-
-In the `main` method, I added a header for the game, then created an
-instace of the game, ran 3 rounds of the game and printed the final
-results afterwards.
-
-There were some unneccessary instance variables, but I kept them to
-ensure that I satisfy the desired API described in the class diagram.
-In a few cases, local variables and recursion was more than enough.
-
-I used `recursion` to handle subsequent rolls when a value point is
-established. With any recursion it's important to have a solid base
-case. In this case the base case was either rolling a 7 or the value
-point. These values are randomly generated, so it is possible to produce
-an infinte loop due to randomness.
-
-3. Errors and Warnings:
-4. Sample Input and Output:
-5. Discussion:
+ Craps is a dice game where two dice are rolled.
+ Each die has six faces representing values:
+
+ 1, 2, 3, 4, 5, or 6.
+
+ 1. If the sum is 2, 3, or 12 (called craps), you lose;
+ 1. If the sum is 7 or 11 (called natural), you win;
+ 1. If the sum is any other value (4, 5, 6, 8, 9, or 10), a value point is established, and you continue to roll until you either roll a sum of the value point or a 7.
+ If the sum of the new roll is equal to the value point, then you win; if the sum of the new roll is equal to 7, then you lose.
+ Remember, in option (III), you continue to roll until you get a 7 or the value point.
+
+ Sample runs:
+
+ * You rolled 5 + 6 = 11; you win
+ * You rolled 1 + 2 = 3; you lose
+ * You rolled 2 + 2 = 4; you establish the value point 4;
+ – Roll again 2 + 3 = 5; roll
+ – Roll again 2 + 1 = 3; roll
+ – Roll again 2 + 2 = 4; you win
+ * You rolled 2 + 6 = 8; you establish the value point 8;
+ – Roll again 4 + 4 = 8; you win
+ * You rolled 3 + 2 = 5; you establish the value point 5;
+ – Roll again 1 + 1 = 2; roll
+ – Roll again 2 + 2 = 4; roll
+ * Roll again 1 + 1 = 2; roll
+ * Roll again 3 + 4 = 7; you lose
+
+ Develop a program that plays craps with a player three times.
+ At the end, the program prints the number of times the player won and the number of times the player lost.
+
+1. Description of the Code:
+
+ I solved this problem by creating a class called `RandomSumGame` as per
+ the class diagram. I added two instance variables named `wins` and
+ `losses` to keep track of how many times the player won or lost.
+
+ To try to make the API of this class more testable, I chose to pass the
+ `PrintStream` in as a parameter to the constructor. This example of
+ dependency injection, made it possible to write unit tests to ensure the
+ proper output is printed to the stream.
+
+ To simplify the problem, I split it up into two types of game play. The
+ rules for the initial roll is slightly different for the rules for
+ subsequent rolls. I created two methods named `firstPlay` and `subsequentPlay`.
+ This made it easy to focus on the rules for the initial roll in the
+ `firstPlay` method and the rules for the subsequent roles in the
+ `subsequentPlay` method.
+
+ To roll the dice, I extracted a method called `roll` that returns a
+ random number between 1 - 6.
+
+ To keep track of the wins/losses, I delegating to the `win` or `lose`
+ methods to print a message to the screen and increment a win/loss
+ counter.
+
+ In the `main` method, I added a header for the game, then created an
+ instace of the game, ran 3 rounds of the game and printed the final
+ results afterwards.
+
+ There were some unneccessary instance variables, but I kept them to
+ ensure that I satisfy the desired API described in the class diagram.
+ In a few cases, local variables and recursion was more than enough.
+
+ I used `recursion` to handle subsequent rolls when a value point is
+ established. With any recursion it's important to have a solid base
+ case. In this case the base case was either rolling a 7 or the value
+ point. These values are randomly generated, so it is possible to produce
+ an infinte loop due to randomness.
+
+1. Errors and Warnings:
+1. Sample Input and Output:
+
+ 1. 3 losses
+
+ ```bash
+ Welcome to Craps
+ ================
+
+ Game 1
+ You rolled: 5
+ Value point established: 5
+ You rolled: 7
+ You lose.
+
+ Game 2
+ You rolled: 6
+ Value point established: 6
+ You rolled: 5
+ You rolled: 9
+ You rolled: 7
+ You lose.
+
+ Game 3
+ You rolled: 3
+ Craps! You lose.
+
+ ================
+ Wins: 0
+ Losses: 3
+ ```
+
+
+ 1. 3 wins
+
+ ```bash
+ java -cp target/assignment2*.jar ca.mokhan.comp268.App 4
+ Welcome to Craps
+ ================
+
+ Game 1
+ You rolled: 7
+ Natural! You win!
+
+ Game 2
+ You rolled: 7
+ Natural! You win!
+
+ Game 3
+ You rolled: 5
+ Value point established: 5
+ You rolled: 6
+ You rolled: 4
+ You rolled: 8
+ You rolled: 5
+ You win!
+
+ ================
+ Wins: 3
+ Losses: 0
+ ```
+
+ 1. 2 wins, 1 loss
+
+ ```bash
+ Welcome to Craps
+ ================
+
+ Game 1
+ You rolled: 4
+ Value point established: 4
+ You rolled: 3
+ You rolled: 9
+ You rolled: 7
+ You lose.
+
+ Game 2
+ You rolled: 8
+ Value point established: 8
+ You rolled: 8
+ You win!
+
+ Game 3
+ You rolled: 8
+ Value point established: 8
+ You rolled: 2
+ You rolled: 8
+ You win!
+
+ ================
+ Wins: 2
+ Losses: 1
+ ```
+
+1. Discussion: