diff --git a/src/main/java/me/alex4386/gachon/sw14462/Main.java b/src/main/java/me/alex4386/gachon/sw14462/Main.java index 37ac1e5..c67ee88 100644 --- a/src/main/java/me/alex4386/gachon/sw14462/Main.java +++ b/src/main/java/me/alex4386/gachon/sw14462/Main.java @@ -5,7 +5,7 @@ import java.util.*; public class Main { - public static String currentTarget = "day08"; + public static String currentTarget = "day09"; public static boolean fallbackToLatest = true; public static Map> getAvailableTargetClassNames() { diff --git a/src/main/java/me/alex4386/gachon/sw14462/day09/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day09/Main.java new file mode 100644 index 0000000..f77a7d7 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day09/Main.java @@ -0,0 +1,18 @@ +package me.alex4386.gachon.sw14462.day09; + +import me.alex4386.gachon.sw14462.utils.Chainloader; + +public class Main { + public static String chainloadTarget = "ex5_10"; + + public static void main(String[] args) throws Throwable { + String packageName = Main.class.getPackage().getName(); + String chainLoadTargetClass = packageName + "." + chainloadTarget + ".Main"; + + try { + Chainloader.chainloadTarget(chainLoadTargetClass, args); + } catch (Exception e) { + throw e; + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_1/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_1/Main.java new file mode 100644 index 0000000..1fa773e --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_1/Main.java @@ -0,0 +1,7 @@ +package me.alex4386.gachon.sw14462.day09.ex5_1; + +public class Main { + public static void main(String[] args) { + MotorBoatTest.main(args); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_1/MotorBoat.java b/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_1/MotorBoat.java new file mode 100644 index 0000000..254f20c --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_1/MotorBoat.java @@ -0,0 +1,73 @@ +package me.alex4386.gachon.sw14462.day09.ex5_1; + +public class MotorBoat { + public static final double tankCapacity = 60.0; + private static final double maximumSpeed = 80.0; + private double fuelInTank; + private double currentSpeed = 0; + private double fuelEfficiency; + + // === GETTER & SETTER === + public double getFuelInTank() { + return this.fuelInTank; + } + + public void setFuelInTank(double fuelInTank) { + if (fuelInTank > tankCapacity) { + throw new IllegalArgumentException("Fuel in tank cannot exceed tank capacity"); + } + + this.fuelInTank = fuelInTank; + } + + public double getMaximumSpeed() { + return this.maximumSpeed; + } + + public double getCurrentSpeed() { + return this.currentSpeed; + } + + public void setCurrentSpeed(double currentSpeed) { + this.currentSpeed = currentSpeed; + } + + public double getFuelEfficiency() { + return this.fuelEfficiency; + } + + public void setFuelEfficiency(double fuelEfficiency) { + this.fuelEfficiency = fuelEfficiency; + } + + + + public void printFuelUsage(double t) { + System.out.println("Maximum speed fuel usage: "+this.getFuelUsage(t, this.maximumSpeed)); + System.out.println("Current speed fuel usage: "+this.getFuelUsage(t)); + } + + public double getFuelUsage(double time) { + return this.getFuelUsage(time, this.currentSpeed); + } + + public double getFuelUsage(double time, double speed) { + return this.fuelEfficiency * Math.pow(speed, 2) * time; + } + + public double getTravelledDistance(double time) { + return this.currentSpeed * time; + } + + public double getAvailableDistance() { + return this.fuelInTank / (this.fuelEfficiency * this.currentSpeed); + } + + + // === EQUALS === + public boolean equals(MotorBoat other) { + return this.fuelInTank == other.fuelInTank && + this.currentSpeed == other.currentSpeed && + this.fuelEfficiency == other.fuelEfficiency; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_1/MotorBoatTest.java b/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_1/MotorBoatTest.java new file mode 100644 index 0000000..27d2366 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_1/MotorBoatTest.java @@ -0,0 +1,41 @@ +package me.alex4386.gachon.sw14462.day09.ex5_1; +public class MotorBoatTest { + public static void main(String[] args) { + MotorBoat boat1 = new MotorBoat(); + boat1.setFuelInTank(50); + boat1.setCurrentSpeed(50); + boat1.setFuelEfficiency(0.1); + + MotorBoat boat2 = new MotorBoat(); + boat2.setFuelInTank(50); + boat2.setCurrentSpeed(50); + boat2.setFuelEfficiency(0.1); + + MotorBoat boat3 = new MotorBoat(); + boat3.setFuelInTank(50); + boat3.setCurrentSpeed(30); + boat3.setFuelEfficiency(0.2); + + System.out.println("Boat 1: "); + System.out.println("Fuel in tank: "+boat1.getFuelInTank()); + System.out.println("Maximum speed: "+boat1.getMaximumSpeed()); + System.out.println("Current speed: "+boat1.getCurrentSpeed()); + System.out.println("Fuel efficiency: "+boat1.getFuelEfficiency()); + System.out.println(""); + System.out.println("Boat 2:"); + System.out.println("Fuel in tank: "+boat2.getFuelInTank()); + System.out.println("Maximum speed: "+boat2.getMaximumSpeed()); + System.out.println("Current speed: "+boat2.getCurrentSpeed()); + System.out.println("Fuel efficiency: "+boat2.getFuelEfficiency()); + System.out.println(""); + System.out.println("Boat 3:"); + System.out.println("Fuel in tank: "+boat3.getFuelInTank()); + System.out.println("Maximum speed: "+boat3.getMaximumSpeed()); + System.out.println("Current speed: "+boat3.getCurrentSpeed()); + System.out.println("Fuel efficiency: "+boat3.getFuelEfficiency()); + + System.out.println(""); + System.out.println("Is Boat 1 and Boat 2 equal? "+(boat1.equals(boat2) ? "Yes" : "No")); + System.out.println("Is Boat 1 and Boat 3 equal? "+(boat1.equals(boat3) ? "Yes" : "No")); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_10/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_10/Main.java new file mode 100644 index 0000000..d502f2e --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_10/Main.java @@ -0,0 +1,7 @@ +package me.alex4386.gachon.sw14462.day09.ex5_10; + +public class Main { + public static void main(String[] args) { + MovieTest.main(args); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_10/Movie.java b/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_10/Movie.java new file mode 100644 index 0000000..2e10a7d --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_10/Movie.java @@ -0,0 +1,92 @@ +package me.alex4386.gachon.sw14462.day09.ex5_10; + +public class Movie { + private String name; + private MovieMPAARating rating; + + private int usersRatedTerrible; + private int usersRatedBad; + private int usersRatedOK; + private int usersRatedGood; + private int usersRatedGreat; + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public MovieMPAARating getMPAARating() { + return this.rating; + } + + public void setMPAARating(MovieMPAARating rating) { + this.rating = rating; + } + + public void addRating(int rating) { + switch (rating) { + case 1: + this.usersRatedTerrible++; + break; + case 2: + this.usersRatedBad++; + break; + case 3: + this.usersRatedOK++; + break; + case 4: + this.usersRatedGood++; + break; + case 5: + this.usersRatedGreat++; + break; + default: + break; + } + } + + public double getAverage() { + return (double) ( + (this.usersRatedTerrible * 1) + + (this.usersRatedBad * 2) + + (this.usersRatedOK * 3) + + (this.usersRatedGood * 4) + + (this.usersRatedGreat * 5) + ) / ( + this.usersRatedTerrible + + this.usersRatedBad + + this.usersRatedOK + + this.usersRatedGood + + this.usersRatedGreat + ); + } + + // TODO: Implement equals method and test it. + public boolean equals(Movie movie) { + return + this.name.equals(movie.getName()) && + this.rating.equals(movie.getMPAARating()) && + this.usersRatedTerrible == movie.usersRatedTerrible && + this.usersRatedBad == movie.usersRatedBad && + this.usersRatedOK == movie.usersRatedOK && + this.usersRatedGood == movie.usersRatedGood && + this.usersRatedGreat == movie.usersRatedGreat; + } +} + +enum MovieMPAARating { + G(1), PG(2), PG13(3), R(4), NC17(5); + + private final int ratingId; + MovieMPAARating(int ratingId) { + this.ratingId = ratingId; + } + + public int getRatingId() { + return this.ratingId; + } +} + diff --git a/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_10/MovieTest.java b/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_10/MovieTest.java new file mode 100644 index 0000000..d1ed2db --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day09/ex5_10/MovieTest.java @@ -0,0 +1,23 @@ +package me.alex4386.gachon.sw14462.day09.ex5_10; + +public class MovieTest { + public static void main(String[] args) { + Movie movie1 = new Movie(); + movie1.setName("The Lion King"); + movie1.setMPAARating(MovieMPAARating.G); + movie1.addRating(5); + + Movie movie2 = new Movie(); + movie2.setName("The Lion King"); + movie2.setMPAARating(MovieMPAARating.G); + movie2.addRating(5); + + Movie movie3 = new Movie(); + movie3.setName("Finding Nemo"); + movie3.setMPAARating(MovieMPAARating.G); + movie3.addRating(4); + + System.out.println("Comparing movie1 and movie2 (both are \"The Lion King\"): " + movie1.equals(movie2)); + System.out.println("Comparing movie1 and movie3 (\"The Lion King\" and \"Finding Nemo\"): " + movie1.equals(movie3)); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/utils/Chainloader.java b/src/main/java/me/alex4386/gachon/sw14462/utils/Chainloader.java new file mode 100644 index 0000000..175534e --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/utils/Chainloader.java @@ -0,0 +1,24 @@ +package me.alex4386.gachon.sw14462.utils; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class Chainloader { + public static void chainloadTarget(String targetClass, String[] args) throws Throwable { + try { + Class klass = Class.forName(targetClass); + Method mainMethod = klass.getMethod("main", String[].class); + + mainMethod.invoke(null, (Object) args); + } catch (ClassNotFoundException e) { + System.err.println("Failed to find the chainload target class."); + e.printStackTrace(); + } catch (InvocationTargetException e) { + throw e.getTargetException(); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } +}