Compare commits

...

2 commits
nya ... task/9

Author SHA1 Message Date
b47f222dec
task 9 2024-06-13 22:48:56 +02:00
3ffc40596a
skele 2024-03-28 19:25:56 +01:00
5 changed files with 134 additions and 3 deletions

View file

@ -1,6 +1,5 @@
# PPJ # PPJ
This repository exists to archive PJATK tasks for the PPJ curriculum. This branch contains the solution for the **9th task**.
Each task can be found under its **corresponding branch**: `task/n`. [Go back to main](/femsci/ppj/src/branch/nya)
Example: `task/2`.

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