-
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: implement exercise 10 and today's homework
- Loading branch information
Showing
24 changed files
with
498 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.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
22
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Circle.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,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
16
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/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,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(); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Rectangle.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,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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/RightTriangle.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,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; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Shape.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.day20.ex10_1a; | ||
|
||
public abstract class Shape { | ||
public double getArea() { | ||
return 0.0; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Circle.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,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
17
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/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,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(); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Rectangle.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,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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/RightTriangle.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,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; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Shape.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,5 @@ | ||
package me.alex4386.gachon.sw14462.day20.ex10_1b; | ||
|
||
public interface Shape { | ||
public double getArea(); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/HazardPay.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,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
11
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/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,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)); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/PayCalculator.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,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; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/RegularPay.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.day20.ex10_1c; | ||
|
||
public class RegularPay extends PayCalculator { | ||
public RegularPay(double payRate) { | ||
this.payRate = payRate; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/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,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)); | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/MessageEncoder.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,5 @@ | ||
package me.alex4386.gachon.sw14462.day20.ex10_1d; | ||
|
||
public interface MessageEncoder { | ||
public String encode(String plainText); | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/ShuffleCipher.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,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; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/SubstitutionCipher.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,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(); | ||
} | ||
} |
Oops, something went wrong.