This commit is contained in:
femsci 2024-06-13 22:48:56 +02:00
parent 3ffc40596a
commit b47f222dec
Signed by: femsci
GPG key ID: 08F7911F0E650C67
5 changed files with 133 additions and 1 deletions

View file

@ -1,5 +1,5 @@
# PPJ
This branch contains the solution for the **nth task**.
This branch contains the solution for the **9th task**.
[Go back to main](/femsci/ppj/src/branch/nya)

68
hai/hello/Car.java Normal file
View file

@ -0,0 +1,68 @@
/*
* TASK 9
* archived at https://git.femboy.science/femsci/ppj/src/branch/task/9
* by femsci
*/
package hai.hello;
import java.util.concurrent.ThreadLocalRandom;
// meow~
public class Car {
int fuel = 35, odometer = 0;
public void printFuelLevel() {
System.out.printf("after filling: %dL >.<\n", this.fuel);
}
public void printStats() {
System.out.printf("%dkm driven, %dl left\n", this.odometer, this.fuel);
}
public void fill() // >.<
{
ThreadLocalRandom r = ThreadLocalRandom.current();
if (r.nextInt(100) < 10) {
throw new ExplosionException(this);
}
this.fuel += r.nextInt(15, 36);
printFuelLevel();
}
// linear funky wunky?
// public void drive(int distance) throws NotEnoughGasException {
// }
public void drive100km() throws NotEnoughGasException {
if (this.fuel < 10) {
throw new NotEnoughGasException(this);
}
this.fuel -= 10;
this.odometer += 100;
printStats();
}
/*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
}

View file

@ -0,0 +1,21 @@
/*
* TASK 9
* archived at https://git.femboy.science/femsci/ppj/src/branch/task/9
* by femsci
*/
package hai.hello;
// pentaerythritol tetranitrate was detected in during subsequent spectrometric analysis
public class ExplosionException extends RuntimeException {
public ExplosionException(Car c) {
car = c;
}
private final Car car;
@Override
public String getMessage() {
return String.format("hai hello your car (%s) exploded >:3c", System.identityHashCode(car));
}
}

23
hai/hello/Kotauki.java Normal file
View file

@ -0,0 +1,23 @@
/*
* TASK 9
* archived at https://git.femboy.science/femsci/ppj/src/branch/task/9
* by femsci
*/
package hai.hello;
// meow meow~
public class Kotauki {
public static void main(String[] args) {
Car kitten = new Car(); // kitty car :3
while (true) {
try {
kitten.drive100km();
} catch (NotEnoughGasException ex) {
System.err.println(ex.getMessage());
kitten.fill();
}
}
}
}

View file

@ -0,0 +1,20 @@
/*
* TASK 9
* archived at https://git.femboy.science/femsci/ppj/src/branch/task/9
* by femsci
*/
package hai.hello;
public class NotEnoughGasException extends Exception {
public NotEnoughGasException(Car c) {
car = c;
}
private final Car car;
@Override
public String getMessage() {
return String.format("not enough gas: %dl left", car.fuel);
}
}