Add oving3
This commit is contained in:
64
src/main/java/oving3/card/Card.java
Normal file
64
src/main/java/oving3/card/Card.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package oving3.card;
|
||||
|
||||
/**
|
||||
* The {@code Card} class is a so-called value-based class, which is coded in
|
||||
* such a way that its
|
||||
* objects cannot be modified after they are created. A {@code Card} object has
|
||||
* a suit and a face.
|
||||
*/
|
||||
public class Card {
|
||||
|
||||
// TODO: Add fields here
|
||||
|
||||
/**
|
||||
* The constructor of the {@code Card} class initializes the suit and face of
|
||||
* the card with the
|
||||
* first and second arguments, respectively.
|
||||
*
|
||||
* @param suit the suit of the card, one of {@code 'S'} (spades), {@code 'H'}
|
||||
* (hearts),
|
||||
* {@code 'D'} (diamonds), or {@code 'C'} (clubs)
|
||||
* @param face the face of the card, an integer between {@code 1} (ace) and
|
||||
* {@code 13} (king)
|
||||
* (both inclusive)
|
||||
* @throws IllegalArgumentException if the suit or face is illegal
|
||||
*
|
||||
* @see CardTest#testConstructor()
|
||||
*/
|
||||
public Card(char suit, int face) {
|
||||
// TODO: Implement this constructor
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suit of the card
|
||||
*/
|
||||
public char getSuit() {
|
||||
// TODO: Implement this method
|
||||
return '\0';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the face of the card
|
||||
*/
|
||||
public int getFace() {
|
||||
// TODO: Implement this method
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the value of the card of the form {@code <suit><face>}. For example,
|
||||
* the ace of
|
||||
* spades should return {@code "S1"}
|
||||
*
|
||||
* @see CardTest#testToString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
// TODO: Implement this method
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
59
src/main/java/oving3/debugging/CoffeeCup.java
Normal file
59
src/main/java/oving3/debugging/CoffeeCup.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package oving3.debugging;
|
||||
|
||||
public class CoffeeCup {
|
||||
|
||||
private double capacity;
|
||||
private double currentVolume;
|
||||
|
||||
public CoffeeCup() {
|
||||
this.capacity = 0.0;
|
||||
this.currentVolume = 0.0;
|
||||
}
|
||||
|
||||
public CoffeeCup(double capacity, double currentVolume) {
|
||||
if (!CoffeeCup.isValidCapacity(capacity)) {
|
||||
throw new IllegalArgumentException("Illegal capacity given.");
|
||||
}
|
||||
this.capacity = capacity;
|
||||
|
||||
if (!this.isValidVolume(currentVolume)) {
|
||||
throw new IllegalArgumentException("Illegal volume given.");
|
||||
}
|
||||
this.currentVolume = currentVolume;
|
||||
}
|
||||
|
||||
private static boolean isValidCapacity(double capacity) {
|
||||
return capacity >= 0.0;
|
||||
}
|
||||
|
||||
public void increaseCupSize(double biggerCapacity) {
|
||||
if (CoffeeCup.isValidCapacity(biggerCapacity)) {
|
||||
this.capacity += biggerCapacity;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValidVolume(double volume) {
|
||||
return volume <= this.capacity && volume >= 0.0;
|
||||
}
|
||||
|
||||
private boolean canDrink(double volume) {
|
||||
return volume <= this.currentVolume;
|
||||
}
|
||||
|
||||
public void drinkCoffee(double volume) {
|
||||
if (!this.isValidVolume(volume) || !this.canDrink(volume)) {
|
||||
throw new IllegalArgumentException("You cannot drink that much coffee!");
|
||||
}
|
||||
|
||||
this.currentVolume -= volume;
|
||||
}
|
||||
|
||||
public void fillCoffee(double volume) {
|
||||
if (!this.isValidVolume(this.currentVolume + volume)) {
|
||||
throw new IllegalArgumentException(
|
||||
"You just poured coffee all over the table. Good job.");
|
||||
}
|
||||
|
||||
this.currentVolume += volume;
|
||||
}
|
||||
}
|
||||
53
src/main/java/oving3/debugging/CoffeeCupProgram.java
Normal file
53
src/main/java/oving3/debugging/CoffeeCupProgram.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package oving3.debugging;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class CoffeeCupProgram {
|
||||
|
||||
private CoffeeCup cup;
|
||||
private Random r;
|
||||
|
||||
public void run() {
|
||||
this.part1();
|
||||
this.part2();
|
||||
}
|
||||
|
||||
private void part1() {
|
||||
this.cup = new CoffeeCup();
|
||||
this.r = new Random(123_456_789L);
|
||||
this.cup.increaseCupSize(40.0);
|
||||
this.cup.fillCoffee(20.5);
|
||||
this.cup.drinkCoffee(Math.floor(this.r.nextDouble() * 20.5));
|
||||
this.cup.fillCoffee(32.5);
|
||||
this.cup.drinkCoffee(Math.ceil(this.r.nextDouble() * 38.9));
|
||||
this.cup.drinkCoffee(Math.ceil(this.r.nextDouble() * 42));
|
||||
this.cup.increaseCupSize(17);
|
||||
this.cup.drinkCoffee(40);
|
||||
this.cup.drinkCoffee(Math.ceil(this.r.nextDouble() * 42));
|
||||
this.cup.drinkCoffee(Math.floor(this.r.nextDouble() * 20.5));
|
||||
this.cup.fillCoffee(32.5);
|
||||
this.cup.drinkCoffee(Math.ceil(this.r.nextDouble() * 38.9));
|
||||
this.cup.drinkCoffee(Math.ceil(this.r.nextDouble() * 42));
|
||||
this.cup.increaseCupSize(17);
|
||||
}
|
||||
|
||||
private void part2() {
|
||||
this.cup = new CoffeeCup(40.0, 20.5);
|
||||
this.r = new Random(987_654_321L);
|
||||
this.cup.drinkCoffee(Math.floor(this.r.nextDouble() * 20.5));
|
||||
this.cup.fillCoffee(Math.floor(this.r.nextDouble() * 30));
|
||||
this.cup.drinkCoffee(Math.ceil(this.r.nextDouble() * 38.9));
|
||||
this.cup.drinkCoffee(Math.ceil(this.r.nextDouble() * 42));
|
||||
this.cup.increaseCupSize(Math.floor(this.r.nextDouble() * 26));
|
||||
this.cup.fillCoffee(Math.ceil(this.r.nextDouble() * 59));
|
||||
this.cup.drinkCoffee(Math.ceil(this.r.nextDouble() * 42));
|
||||
this.cup.increaseCupSize(Math.floor(this.r.nextDouble() * 35));
|
||||
this.cup.fillCoffee(Math.floor(this.r.nextDouble() * 30));
|
||||
this.cup.increaseCupSize(Math.floor(this.r.nextDouble() * 26));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
CoffeeCupProgram program = new CoffeeCupProgram();
|
||||
program.run();
|
||||
}
|
||||
}
|
||||
81
src/test/java/oving3/NimTest.java
Normal file
81
src/test/java/oving3/NimTest.java
Normal file
@@ -0,0 +1,81 @@
|
||||
package oving3;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class NimTest {
|
||||
|
||||
private Nim nim;
|
||||
|
||||
private static boolean checkValidMove(Nim game, int pieces, boolean legal) {
|
||||
return (legal == game.isValidMove(pieces, 0) && (legal == game.isValidMove(pieces, 1))
|
||||
&& (legal == game.isValidMove(pieces, 2)));
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
nim = new Nim(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Constructor")
|
||||
public void testConstructor() {
|
||||
assertEquals(5, nim.getPile(0));
|
||||
assertEquals(5, nim.getPile(1));
|
||||
assertEquals(5, nim.getPile(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Remove pieces")
|
||||
public void testRemovePieces() {
|
||||
nim.removePieces(3, 0);
|
||||
nim.removePieces(2, 1);
|
||||
nim.removePieces(1, 2);
|
||||
assertEquals(2, nim.getPile(0));
|
||||
assertEquals(3, nim.getPile(1));
|
||||
assertEquals(4, nim.getPile(2));
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
nim.removePieces(-1, 0);
|
||||
}, "Cannot remove negative number of pieces");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
nim.removePieces(0, 0);
|
||||
}, "Cannot remove too few pieces");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
nim.removePieces(6, 0);
|
||||
}, "Cannot remove too many pieces");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Game over")
|
||||
public void testGameOver() {
|
||||
assertFalse(nim.isGameOver());
|
||||
|
||||
nim.removePieces(5, 0);
|
||||
assertEquals(0, nim.getPile(0));
|
||||
assertTrue(nim.isGameOver());
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
nim.removePieces(5, 0);
|
||||
}, "Cannot remove pieces when game is over");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Valid moves")
|
||||
public void testIsValidMove() {
|
||||
assertTrue(NimTest.checkValidMove(nim, 2, true));
|
||||
assertTrue(NimTest.checkValidMove(nim, -2, false));
|
||||
assertTrue(NimTest.checkValidMove(nim, 0, false));
|
||||
assertTrue(NimTest.checkValidMove(nim, 6, false));
|
||||
|
||||
nim.removePieces(5, 0);
|
||||
assertTrue(NimTest.checkValidMove(nim, 2, false));
|
||||
}
|
||||
}
|
||||
123
src/test/java/oving3/RPNCalcTest.java
Normal file
123
src/test/java/oving3/RPNCalcTest.java
Normal file
@@ -0,0 +1,123 @@
|
||||
package oving3;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class RPNCalcTest {
|
||||
|
||||
private RPNCalc calc;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
calc = new RPNCalc();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Push")
|
||||
public void testPush() {
|
||||
calc.push(1.0);
|
||||
assertEquals(1.0, calc.peek(0));
|
||||
|
||||
calc.push(2.0);
|
||||
assertEquals(2.0, calc.peek(0));
|
||||
|
||||
calc.push(3.0);
|
||||
assertEquals(3.0, calc.peek(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Pop")
|
||||
public void testPop() {
|
||||
calc.push(1.0);
|
||||
calc.push(2.0);
|
||||
calc.push(3.0);
|
||||
assertEquals(3.0, calc.peek(0));
|
||||
assertEquals(3.0, calc.pop());
|
||||
|
||||
assertEquals(2.0, calc.peek(0));
|
||||
assertEquals(2.0, calc.pop());
|
||||
|
||||
assertEquals(1.0, calc.peek(0));
|
||||
|
||||
calc.push(2.0);
|
||||
assertEquals(2.0, calc.peek(0));
|
||||
assertEquals(2.0, calc.pop());
|
||||
|
||||
assertEquals(1.0, calc.peek(0));
|
||||
assertEquals(1.0, calc.pop());
|
||||
|
||||
assertEquals(0, calc.getSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Peek")
|
||||
public void testPeek() {
|
||||
calc.push(0.0);
|
||||
calc.push(1.0);
|
||||
calc.push(2.0);
|
||||
assertEquals(2.0, calc.peek(0));
|
||||
assertEquals(1.0, calc.peek(1));
|
||||
assertEquals(0.0, calc.peek(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Empty stack")
|
||||
public void testEmptyStack() {
|
||||
assertEquals(Double.NaN, calc.peek(3));
|
||||
assertEquals(Double.NaN, calc.peek(-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Size")
|
||||
public void testGetSize() {
|
||||
assertEquals(0, calc.getSize());
|
||||
|
||||
calc.push(1.0);
|
||||
assertEquals(1, calc.getSize());
|
||||
|
||||
calc.push(2.0);
|
||||
assertEquals(2, calc.getSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Addition")
|
||||
public void testAddOperation() {
|
||||
calc.push(3.0);
|
||||
calc.push(4.0);
|
||||
calc.performOperation('+');
|
||||
assertEquals(1, calc.getSize());
|
||||
assertEquals(7.0, calc.peek(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Subtraction")
|
||||
public void testSubOperation() {
|
||||
calc.push(7.0);
|
||||
calc.push(2.0);
|
||||
calc.performOperation('-');
|
||||
assertEquals(1, calc.getSize());
|
||||
assertEquals(5.0, calc.peek(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Multiplication")
|
||||
public void testMultOperation() {
|
||||
calc.push(5.0);
|
||||
calc.push(2.0);
|
||||
calc.performOperation('*');
|
||||
assertEquals(1, calc.getSize());
|
||||
assertEquals(10.0, calc.peek(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Division")
|
||||
public void testDivOperation() {
|
||||
calc.push(10.0);
|
||||
calc.push(4.0);
|
||||
calc.performOperation('/');
|
||||
assertEquals(1, calc.getSize());
|
||||
assertEquals(2.5, calc.peek(0));
|
||||
}
|
||||
}
|
||||
42
src/test/java/oving3/card/CardDeckTest.java
Normal file
42
src/test/java/oving3/card/CardDeckTest.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package oving3.card;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CardDeckTest {
|
||||
|
||||
private static void checkDeck(CardDeck deck, List<String> expected) {
|
||||
assertEquals(expected.size(), deck.getCardCount(), "Wrong number of cards in deck");
|
||||
|
||||
for (int i = 0; i < expected.size(); i++) {
|
||||
String expectedCard = expected.get(i);
|
||||
Card actual = deck.getCard(i);
|
||||
|
||||
String cardString = String.valueOf(actual.getSuit()) + actual.getFace();
|
||||
assertEquals(expectedCard, cardString);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Constructor")
|
||||
public void testConstructor() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new CardDeck(-1));
|
||||
assertThrows(IllegalArgumentException.class, () -> new CardDeck(14));
|
||||
|
||||
CardDeckTest.checkDeck(new CardDeck(0), Collections.emptyList());
|
||||
CardDeckTest.checkDeck(new CardDeck(2),
|
||||
List.of("S1", "S2", "H1", "H2", "D1", "D2", "C1", "C2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("#shufflePerfectly()")
|
||||
public void testShufflePerfectly() {
|
||||
CardDeck cardDeck = new CardDeck(2);
|
||||
cardDeck.shufflePerfectly();
|
||||
CardDeckTest.checkDeck(cardDeck, List.of("S1", "D1", "S2", "D2", "H1", "C1", "H2", "C2"));
|
||||
}
|
||||
}
|
||||
46
src/test/java/oving3/card/CardTest.java
Normal file
46
src/test/java/oving3/card/CardTest.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package oving3.card;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CardTest {
|
||||
|
||||
private static boolean checkState(Card card, char suit, int face) {
|
||||
return card.getSuit() == suit && card.getFace() == face;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Constructor")
|
||||
public void testConstructor() {
|
||||
assertTrue(CardTest.checkState(new Card('S', 1), 'S', 1));
|
||||
assertTrue(CardTest.checkState(new Card('S', 13), 'S', 13));
|
||||
assertTrue(CardTest.checkState(new Card('H', 1), 'H', 1));
|
||||
assertTrue(CardTest.checkState(new Card('H', 13), 'H', 13));
|
||||
assertTrue(CardTest.checkState(new Card('D', 1), 'D', 1));
|
||||
assertTrue(CardTest.checkState(new Card('D', 13), 'D', 13));
|
||||
assertTrue(CardTest.checkState(new Card('C', 1), 'C', 1));
|
||||
assertTrue(CardTest.checkState(new Card('C', 13), 'C', 13));
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Card('X', 1);
|
||||
});
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Card('S', 0);
|
||||
});
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Card('C', 14);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("#toString()")
|
||||
public void testToString() {
|
||||
assertEquals("S1", new Card('S', 1).toString());
|
||||
assertEquals("H13", new Card('H', 13).toString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user