From 8e08785db06772ceef837e0ef236b8e77b8ed7ac Mon Sep 17 00:00:00 2001 From: Vegard Bieker Matthey Date: Mon, 9 Feb 2026 17:03:32 +0100 Subject: [PATCH] =?UTF-8?q?=C3=B8ving3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/oving3/RPNCalc.java | 63 ++++++++++++ src/main/java/oving3/card/Card.java | 99 ++++++++++--------- src/main/java/oving3/card/CardDeck.java | 43 ++++++++ src/main/java/oving3/debugging/CoffeeCup.java | 4 +- .../oving3/debugging/CoffeeCupProgram.java | 7 +- src/main/java/oving3/teori.md | 20 ++++ 6 files changed, 187 insertions(+), 49 deletions(-) create mode 100644 src/main/java/oving3/RPNCalc.java create mode 100644 src/main/java/oving3/card/CardDeck.java create mode 100644 src/main/java/oving3/teori.md diff --git a/src/main/java/oving3/RPNCalc.java b/src/main/java/oving3/RPNCalc.java new file mode 100644 index 0000000..0affd23 --- /dev/null +++ b/src/main/java/oving3/RPNCalc.java @@ -0,0 +1,63 @@ +package oving3; + +import java.util.Stack; + +public class RPNCalc { + + private Stack stack = new Stack<>(); + + RPNCalc() { + } + + void push(double n) { + stack.push(n); + } + + double pop() { + if (stack.isEmpty()) { + return Double.NaN; + } + return stack.pop(); + } + + double peek(int i) { + int id = stack.size() - i; + if (id < 0 || id >= stack.size()) { + return Double.NaN; + } + return stack.get(id); + } + + int getSize() { + return stack.size(); + } + + void performOperation(char c) { + if (stack.size() <= 1) { + return; + } + double a = stack.pop(); + double b = stack.pop(); + switch (c) { + case '+': + stack.push(a + b); + break; + case '-': + stack.push(a - b); + break; + case '*': + stack.push(a * b); + break; + case '/': + stack.push(a / b); + break; + default: + throw new IllegalArgumentException("Invalid operation"); + } + } + + @Override + public String toString() { + return stack.toString(); + } +} diff --git a/src/main/java/oving3/card/Card.java b/src/main/java/oving3/card/Card.java index 361a7fb..52e9050 100644 --- a/src/main/java/oving3/card/Card.java +++ b/src/main/java/oving3/card/Card.java @@ -8,57 +8,62 @@ package oving3.card; */ public class Card { - // TODO: Add fields here + private char suit; + private int face; - /** - * 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 - } + /** + * 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) { + if (suit != 'S' && suit != 'H' && suit != 'D' && suit != 'C') { + throw new IllegalArgumentException("Illegal value for suit"); + } + if (face < 1 || face > 13) { + throw new IllegalArgumentException("Illegal value for face"); + } + this.suit = suit; + this.face = face; + } - /** - * @return the suit of the card - */ - public char getSuit() { - // TODO: Implement this method - return '\0'; - } + /** + * @return the suit of the card + */ + public char getSuit() { + return suit; + } - /** - * @return the face of the card - */ - public int getFace() { - // TODO: Implement this method - return 0; - } + /** + * @return the face of the card + */ + public int getFace() { + return face; + } - /** - * @return the value of the card of the form {@code }. For example, - * the ace of - * spades should return {@code "S1"} - * - * @see CardTest#testToString() - */ - @Override - public String toString() { - // TODO: Implement this method - return null; - } + /** + * @return the value of the card of the form {@code }. For example, + * the ace of + * spades should return {@code "S1"} + * + * @see CardTest#testToString() + */ + @Override + public String toString() { + return suit + Integer.toString(face); + } - public static void main(String[] args) { + public static void main(String[] args) { - } + } } diff --git a/src/main/java/oving3/card/CardDeck.java b/src/main/java/oving3/card/CardDeck.java new file mode 100644 index 0000000..41637dd --- /dev/null +++ b/src/main/java/oving3/card/CardDeck.java @@ -0,0 +1,43 @@ +package oving3.card; + +import java.util.ArrayList; + +public class CardDeck { + private ArrayList deck = new ArrayList<>(); + + CardDeck(int n) { + if (n < 0 || n > 13) { + throw new IllegalArgumentException("value of n needs to be >= 1 and <= 13"); + } + char[] suits = { 'S', 'H', 'D', 'C' }; + for (char suit : suits) { + for (int face = 1; face <= n; face++) { + deck.add(new Card(suit, face)); + } + } + } + + int getCardCount() { + return deck.size(); + } + + Card getCard(int n) { + return new Card('S', 1); + } + + void shufflePerfectly() { + int n = deck.size(); + if (n < 2) { + return; + } + ArrayList shuffledDeck = new ArrayList<>(); + for (int i = 0; i < n / 2; i++) { + shuffledDeck.add(deck.get(i)); + shuffledDeck.add(deck.get(n / 2 + i)); + } + if (n % 2 != 0) { + shuffledDeck.add(deck.get(n - 1)); + } + deck = shuffledDeck; + } +} diff --git a/src/main/java/oving3/debugging/CoffeeCup.java b/src/main/java/oving3/debugging/CoffeeCup.java index b52fd56..85a8303 100644 --- a/src/main/java/oving3/debugging/CoffeeCup.java +++ b/src/main/java/oving3/debugging/CoffeeCup.java @@ -42,7 +42,9 @@ public class CoffeeCup { public void drinkCoffee(double volume) { if (!this.isValidVolume(volume) || !this.canDrink(volume)) { - throw new IllegalArgumentException("You cannot drink that much coffee!"); + throw new IllegalArgumentException( + "You cannot drink that much coffee! Trying to drink: " + volume + ", cup can hold: " + capacity + + ", cup has: " + currentVolume); } this.currentVolume -= volume; diff --git a/src/main/java/oving3/debugging/CoffeeCupProgram.java b/src/main/java/oving3/debugging/CoffeeCupProgram.java index 26ddb6b..1202a6d 100644 --- a/src/main/java/oving3/debugging/CoffeeCupProgram.java +++ b/src/main/java/oving3/debugging/CoffeeCupProgram.java @@ -21,10 +21,13 @@ public class CoffeeCupProgram { 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.increaseCupSize(17 + 300); + this.cup.fillCoffee(35); this.cup.drinkCoffee(40); + this.cup.fillCoffee(42 + 20.5); this.cup.drinkCoffee(Math.ceil(this.r.nextDouble() * 42)); this.cup.drinkCoffee(Math.floor(this.r.nextDouble() * 20.5)); + this.cup.increaseCupSize(170); this.cup.fillCoffee(32.5); this.cup.drinkCoffee(Math.ceil(this.r.nextDouble() * 38.9)); this.cup.drinkCoffee(Math.ceil(this.r.nextDouble() * 42)); @@ -34,6 +37,8 @@ public class CoffeeCupProgram { private void part2() { this.cup = new CoffeeCup(40.0, 20.5); this.r = new Random(987_654_321L); + this.cup.increaseCupSize(10000); + this.cup.fillCoffee(9000); 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)); diff --git a/src/main/java/oving3/teori.md b/src/main/java/oving3/teori.md new file mode 100644 index 0000000..02c598d --- /dev/null +++ b/src/main/java/oving3/teori.md @@ -0,0 +1,20 @@ +# Teori + +## RPN +### Del 2 + + Hvilken type unntak vil det være naturlig å bruke? + IllegalStateException + + Hvilke fordeler og ulemper ser du for dette alternativet? + Resistans til at programmet krasjer, men problemet er at alternativet er dårlig data. + + Hva vil tilsvarende verdi for manglende operand for *-operasjonen (multiplikasjon) være? Hva med for / (divisjon)? + 1 og 1. Det vil føre til ingen endring. + + Hvordan kan du endre (evt. har du endret) grensesnittet for stack-operasjonene for å gjøre implementasjonen av disse enklere? + Ta inn en default. + + Også her er et alternativ å utløse unntak. Hva tror du om det? + IllegalStateException kan brukes, men det er avhengig om man hvordan man ønsker at grensesnittet skal fungere, og hvor strengt det skal være. Det viktigste er at man er konsekvent. +