Skip to content

Commit

Permalink
chore: include bunch of homeworks
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex4386 committed Mar 28, 2024
1 parent d338c0f commit 0c6843b
Show file tree
Hide file tree
Showing 18 changed files with 607 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/me/alex4386/gachon/sw14462/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.*;

public class Main {
public static String currentTarget = "day06";
public static String currentTarget = "day08";
public static boolean fallbackToLatest = true;

public static Map<String, Class<?>> getAvailableTargetClassNames() {
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day08/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.alex4386.gachon.sw14462.day08;

import java.lang.reflect.InvocationTargetException;

public class Main {
public static String chainloadTarget = "ex4_7";

public static void main(String[] args) {
String packageName = Main.class.getPackage().getName();
String chainLoadTargetClass = packageName + "." + chainloadTarget + ".Main";

try {
Class<?> klass = Class.forName(chainLoadTargetClass);
klass.getMethod("main", String[].class).invoke(null, (Object) args);
} catch (ClassNotFoundException e) {
System.err.println("Failed to find the chainload target class.");
e.printStackTrace();
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package me.alex4386.gachon.sw14462.day08.ex4_1;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
MotorBoatTest.main(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.alex4386.gachon.sw14462.day08.ex4_1;

public class MotorBoat {
public double tankCapacity;
public double fuelInTank;
public double maximumSpeed;
public double currentSpeed = 0;
public double 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);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package me.alex4386.gachon.sw14462.day08.ex4_1;

public class MotorBoatTest {
public static void main(String[] args) {
MotorBoat boat = new MotorBoat();

// provision boat instance - since I can not use constructor "yet"
boat.tankCapacity = 100;
boat.fuelInTank = 50;
boat.maximumSpeed = 100;
boat.currentSpeed = 50;
boat.fuelEfficiency = 0.1;

// test section
boat.printFuelUsage(10);
System.out.println("Travelled distance: "+boat.getTravelledDistance(10));
System.out.println("Available distance: "+boat.getAvailableDistance());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.alex4386.gachon.sw14462.day08.ex4_2a;

public class Main {
public static void main(String[] args) {
MotorBoatTest.main(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package me.alex4386.gachon.sw14462.day08.ex4_2a;

public class MotorBoat {
public static final double tankCapacity = 60.0;
private double fuelInTank;
private double maximumSpeed;
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 void setMaximumSpeed(double maximumSpeed) {
this.maximumSpeed = 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);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.alex4386.gachon.sw14462.day08.ex4_2a;

public class MotorBoatTest {
public static void main(String[] args) {
MotorBoat boat = new MotorBoat();
boat.setFuelInTank(50);
boat.setCurrentSpeed(50);
boat.setFuelEfficiency(0.1);

boat.printFuelUsage(5);
System.out.println("Maximum speed fuel usage: "+boat.getFuelUsage(10, boat.getMaximumSpeed()));
System.out.println("Current speed fuel usage: "+boat.getFuelUsage(10));
System.out.println("Travelled distance: "+boat.getTravelledDistance(10));
System.out.println("Available distance: "+boat.getAvailableDistance());
}
}
26 changes: 26 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day08/ex4_2b/Counter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.alex4386.gachon.sw14462.day08.ex4_2b;

public class Counter {
private int count = 0;

public void resetCounter() {
this.count = 0;
}

public void increase() {
count++;
}

public void decrease() {
if (count <= 0) {
throw new RuntimeException("Count cannot be negative");
}
count--;
}

public int getCount() {
return count;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.alex4386.gachon.sw14462.day08.ex4_2b;

public class CounterTest {
public static void main(String[] args) {
Counter counter = new Counter();

System.out.println("Increasing twice");
counter.increase();
counter.increase();

assert counter.getCount() == 2;

System.out.println("Decreasing once");
counter.decrease();

assert counter.getCount() == 1;

System.out.println("Resetting counter");
counter.resetCounter();

assert counter.getCount() == 0;

System.out.println("Test complete.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.alex4386.gachon.sw14462.day08.ex4_2b;

public class Main {
public static void main(String[] args) {
CounterTest.main(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package me.alex4386.gachon.sw14462.day08.ex4_3;

public class GradeDistribution {
final char grades[] = {'A', 'B', 'C', 'D', 'F'};
int[] gradeNums = new int[5];

public void setGradeNums(char letter, int num) {
switch (letter) {
case 'A':
gradeNums[0] = num;
break;
case 'B':
gradeNums[1] = num;
break;
case 'C':
gradeNums[2] = num;
break;
case 'D':
gradeNums[3] = num;
break;
case 'F':
gradeNums[4] = num;
break;
}
}

public int getGradeNums(char letter) {
switch (letter) {
case 'A':
return gradeNums[0];
case 'B':
return gradeNums[1];
case 'C':
return gradeNums[2];
case 'D':
return gradeNums[3];
case 'F':
return gradeNums[4];
default:
return 0;
}
}

public int totalGradeNums() {
int total = 0;
for (int i = 0; i < gradeNums.length; i++) {
total += gradeNums[i];
}
return total;
}

public double gradePercentage(char letter) {
return ((double) getGradeNums(letter) / totalGradeNums()) * 100.0;
}

public void drawBarGraph() {
int columns = 50;
for (int i = 0; i < columns; i++) {
System.out.print("*");
}
System.out.println();

for (int i = 0; i < gradeNums.length; i++) {
double multiplier = (double) columns / 100.0;
int counts = (int) Math.round(this.gradePercentage(grades[i]) * multiplier);
for (int j = 0; j < counts; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day08/ex4_3/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.alex4386.gachon.sw14462.day08.ex4_3;

public class Main {
public static void main(String[] args) {
GradeDistribution gradeDistribution = new GradeDistribution();

gradeDistribution.setGradeNums('A', 1);
gradeDistribution.setGradeNums('B', 4);
gradeDistribution.setGradeNums('C', 6);
gradeDistribution.setGradeNums('D', 2);
gradeDistribution.setGradeNums('F', 1);

System.out.println("Grade Distribution");
System.out.println("A: " + gradeDistribution.getGradeNums('A') + " (" + gradeDistribution.gradePercentage('A') + "%)");
System.out.println("B: " + gradeDistribution.getGradeNums('B') + " (" + gradeDistribution.gradePercentage('B') + "%)");
System.out.println("C: " + gradeDistribution.getGradeNums('C') + " (" + gradeDistribution.gradePercentage('C') + "%)");
System.out.println("D: " + gradeDistribution.getGradeNums('D') + " (" + gradeDistribution.gradePercentage('D') + "%)");
System.out.println("F: " + gradeDistribution.getGradeNums('F') + " (" + gradeDistribution.gradePercentage('F') + "%)");

gradeDistribution.drawBarGraph();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package me.alex4386.gachon.sw14462.day08.ex4_4;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
PurchaseDemo.main(args);
}
}
Loading

0 comments on commit 0c6843b

Please sign in to comment.