-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: implemented in-class exercises
- Loading branch information
Showing
9 changed files
with
286 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/me/alex4386/gachon/sw14462/day09/ex5_1/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package me.alex4386.gachon.sw14462.day09.ex5_1; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
MotorBoatTest.main(args); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/me/alex4386/gachon/sw14462/day09/ex5_1/MotorBoat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/me/alex4386/gachon/sw14462/day09/ex5_1/MotorBoatTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/me/alex4386/gachon/sw14462/day09/ex5_10/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package me.alex4386.gachon.sw14462.day09.ex5_10; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
MovieTest.main(args); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
src/main/java/me/alex4386/gachon/sw14462/day09/ex5_10/Movie.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
23 changes: 23 additions & 0 deletions
23
src/main/java/me/alex4386/gachon/sw14462/day09/ex5_10/MovieTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/me/alex4386/gachon/sw14462/utils/Chainloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |