Add oving3

This commit is contained in:
Andreas Omholt Olsen
2026-01-22 22:01:08 +01:00
parent 0586d24f49
commit 26163d3029
12 changed files with 684 additions and 0 deletions

View 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) {
}
}

View 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;
}
}

View 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();
}
}