øving3
This commit is contained in:
63
src/main/java/oving3/RPNCalc.java
Normal file
63
src/main/java/oving3/RPNCalc.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package oving3;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class RPNCalc {
|
||||
|
||||
private Stack<Double> 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();
|
||||
}
|
||||
}
|
||||
@@ -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 <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;
|
||||
}
|
||||
/**
|
||||
* @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() {
|
||||
return suit + Integer.toString(face);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
43
src/main/java/oving3/card/CardDeck.java
Normal file
43
src/main/java/oving3/card/CardDeck.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package oving3.card;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CardDeck {
|
||||
private ArrayList<Card> 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<Card> 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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));
|
||||
|
||||
20
src/main/java/oving3/teori.md
Normal file
20
src/main/java/oving3/teori.md
Normal file
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user