Skip to content

Commit

Permalink
chore: implement exercise 10 and today's homework
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex4386 committed May 8, 2024
1 parent a301966 commit 8815578
Show file tree
Hide file tree
Showing 24 changed files with 498 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 = "day18";
public static String currentTarget = "day20";
public static boolean fallbackToLatest = true;

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

import me.alex4386.gachon.sw14462.utils.Chainloader;

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

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;
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.alex4386.gachon.sw14462.day20.ex10_1a;

public class Circle extends Shape {
private double radius;

public Circle(double radius) {
this.radius = radius;
}

@Override
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}

public String toString() {
return "Circle: radius=" + radius;
}

public boolean equals(Circle obj) {
return this.radius == obj.radius;
}
}
16 changes: 16 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.alex4386.gachon.sw14462.day20.ex10_1a;
public class Main {
public static void main(String[] args) {
Shape[] shapes = new Shape[4];
shapes[0] = new Rectangle(10, 20);
shapes[1] = new Circle(10);
shapes[2] = new RightTriangle(10, 20);
shapes[3] = new Circle(6);

for (Shape s : shapes) {
System.out.println(s);
System.out.println("Area: " + s.getArea());
System.out.println();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.alex4386.gachon.sw14462.day20.ex10_1a;

public class Rectangle extends Shape {
private double width;
private double height;

public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}

@Override
public double getArea() {
return width * height;
}

@Override
public String toString() {
return "Rectangle: width=" + width + ", height=" + height;
}

public boolean equals(Rectangle obj) {
return this.width == obj.width && this.height == obj.height;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.alex4386.gachon.sw14462.day20.ex10_1a;

public class RightTriangle extends Shape {
private double width;
private double height;

public RightTriangle(double width, double height) {
this.width = width;
this.height = height;
}

@Override
public double getArea() {
return width * height / 2;
}

@Override
public String toString() {
return "RightTriangle: width=" + width + ", height=" + height;
}

public boolean equals(RightTriangle obj) {
return this.width == obj.width && this.height == obj.height;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.alex4386.gachon.sw14462.day20.ex10_1a;

public abstract class Shape {
public double getArea() {
return 0.0;
}
}
22 changes: 22 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.alex4386.gachon.sw14462.day20.ex10_1b;

public class Circle implements Shape {
private double radius;

public Circle(double radius) {
this.radius = radius;
}

@Override
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}

public String toString() {
return "Circle: radius=" + radius;
}

public boolean equals(Circle obj) {
return this.radius == obj.radius;
}
}
17 changes: 17 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.alex4386.gachon.sw14462.day20.ex10_1b;

public class Main {
public static void main(String[] args) {
Shape[] shapes = new Shape[4];
shapes[0] = new Rectangle(10, 20);
shapes[1] = new Circle(10);
shapes[2] = new RightTriangle(10, 20);
shapes[3] = new Circle(6);

for (Shape s : shapes) {
System.out.println(s);
System.out.println("Area: " + s.getArea());
System.out.println();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.alex4386.gachon.sw14462.day20.ex10_1b;

public class Rectangle implements Shape {
private double width;
private double height;

public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}

@Override
public double getArea() {
return width * height;
}

@Override
public String toString() {
return "Rectangle: width=" + width + ", height=" + height;
}

public boolean equals(Rectangle obj) {
return this.width == obj.width && this.height == obj.height;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.alex4386.gachon.sw14462.day20.ex10_1b;

public class RightTriangle implements Shape {
private double width;
private double height;

public RightTriangle(double width, double height) {
this.width = width;
this.height = height;
}

@Override
public double getArea() {
return width * height / 2;
}

@Override
public String toString() {
return "RightTriangle: width=" + width + ", height=" + height;
}

public boolean equals(RightTriangle obj) {
return this.width == obj.width && this.height == obj.height;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package me.alex4386.gachon.sw14462.day20.ex10_1b;

public interface Shape {
public double getArea();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package me.alex4386.gachon.sw14462.day20.ex10_1c;

public class HazardPay extends PayCalculator {
public HazardPay(double payRate) {
this.payRate = payRate;
}

@Override
public double computePay(double hours) {
return super.computePay(hours) * 1.5;
}
}
11 changes: 11 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.alex4386.gachon.sw14462.day20.ex10_1c;

public class Main {
public static void main(String[] args) {
PayCalculator regularPay = new RegularPay(10);
PayCalculator hazardPay = new HazardPay(10);

System.out.println("Regular Pay: " + regularPay.computePay(10));
System.out.println("Hazard Pay: " + hazardPay.computePay(10));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package me.alex4386.gachon.sw14462.day20.ex10_1c;

public abstract class PayCalculator {
protected double payRate;

public double computePay(double hours) {
return this.payRate * hours;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.alex4386.gachon.sw14462.day20.ex10_1c;

public class RegularPay extends PayCalculator {
public RegularPay(double payRate) {
this.payRate = payRate;
}
}
15 changes: 15 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.alex4386.gachon.sw14462.day20.ex10_1d;

public class Main {
public static void main(String[] args) {
MessageEncoder[] encoders = new MessageEncoder[2];
encoders[0] = new ShuffleCipher(1);
encoders[1] = new SubstitutionCipher(3);

String plainText = "All your base are belong to us.";
System.out.println("Original Message: " + plainText);
for (MessageEncoder encoder : encoders) {
System.out.println("Encoded Message ("+encoder.getClass().getName().substring(encoder.getClass().getName().lastIndexOf('.') + 1)+"): " + encoder.encode(plainText));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package me.alex4386.gachon.sw14462.day20.ex10_1d;

public interface MessageEncoder {
public String encode(String plainText);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package me.alex4386.gachon.sw14462.day20.ex10_1d;

public class ShuffleCipher implements MessageEncoder {
private int n;

public ShuffleCipher(int n) {
this.n = n;
}

@Override
public String encode(String plainText) {
char[] plainTextArray = plainText.toCharArray();
char[] encodedTextArray = new char[plainTextArray.length];

for (int i = 0; i < this.n; i++) {
encodedTextArray = shuffle(plainTextArray);
}

return new String(encodedTextArray);
}

private char[] shuffle(char[] plainTextArray) {
char[] encodedTextArray = new char[plainTextArray.length];

// split into two arrays
char[] firstHalf = new char[plainTextArray.length / 2 + 1];
char[] secondHalf = new char[plainTextArray.length - firstHalf.length];

// alternate between first and second half
for (int i = 0; i < plainTextArray.length; i++) {
if (i < firstHalf.length) {
firstHalf[i] = plainTextArray[i];
} else {
secondHalf[i - firstHalf.length] = plainTextArray[i];
}
}

// merge two arrays with alternating patterns
for (int i = 0; i < plainTextArray.length; i++) {
if (i % 2 == 0) {
encodedTextArray[i] = firstHalf[i / 2];
} else {
encodedTextArray[i] = secondHalf[i / 2];
}
}

return encodedTextArray;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package me.alex4386.gachon.sw14462.day20.ex10_1d;

public class SubstitutionCipher implements MessageEncoder {
private int shift;

public SubstitutionCipher(int shift) {
this.shift = shift;
}

@Override
public String encode(String plainText) {
StringBuilder encodedText = new StringBuilder();

for (int i = 0; i < plainText.length(); i++) {
char c = plainText.charAt(i);
if (c >= 'A' && c <= 'Z') {
c = (char) (c + shift);
if (c > 'Z') {
c = (char) (c - 26);
}
} else if (c >= 'a' && c <= 'z') {
c = (char) (c + shift);
if (c > 'z') {
c = (char) (c - 26);
}
}
encodedText.append(c);
}

return encodedText.toString();
}
}
Loading

0 comments on commit 8815578

Please sign in to comment.