From 88155782113822713722dbd554f3cc6ee23c88c8 Mon Sep 17 00:00:00 2001 From: Alex4386 Date: Wed, 8 May 2024 10:51:48 +0900 Subject: [PATCH] chore: implement exercise 10 and today's homework --- .../java/me/alex4386/gachon/sw14462/Main.java | 2 +- .../alex4386/gachon/sw14462/day20/Main.java | 18 ++++++ .../gachon/sw14462/day20/ex10_1a/Circle.java | 22 +++++++ .../gachon/sw14462/day20/ex10_1a/Main.java | 16 +++++ .../sw14462/day20/ex10_1a/Rectangle.java | 25 ++++++++ .../sw14462/day20/ex10_1a/RightTriangle.java | 25 ++++++++ .../gachon/sw14462/day20/ex10_1a/Shape.java | 7 +++ .../gachon/sw14462/day20/ex10_1b/Circle.java | 22 +++++++ .../gachon/sw14462/day20/ex10_1b/Main.java | 17 ++++++ .../sw14462/day20/ex10_1b/Rectangle.java | 25 ++++++++ .../sw14462/day20/ex10_1b/RightTriangle.java | 25 ++++++++ .../gachon/sw14462/day20/ex10_1b/Shape.java | 5 ++ .../sw14462/day20/ex10_1c/HazardPay.java | 12 ++++ .../gachon/sw14462/day20/ex10_1c/Main.java | 11 ++++ .../sw14462/day20/ex10_1c/PayCalculator.java | 10 ++++ .../sw14462/day20/ex10_1c/RegularPay.java | 7 +++ .../gachon/sw14462/day20/ex10_1d/Main.java | 15 +++++ .../sw14462/day20/ex10_1d/MessageEncoder.java | 5 ++ .../sw14462/day20/ex10_1d/ShuffleCipher.java | 49 ++++++++++++++++ .../day20/ex10_1d/SubstitutionCipher.java | 32 ++++++++++ .../gachon/sw14462/day20/ex10_7/Main.java | 39 +++++++++++++ .../gachon/sw14462/day20/ex10_7/Person.java | 35 +++++++++++ .../gachon/sw14462/day20/ex10_7/Student.java | 58 +++++++++++++++++++ .../sw14462/day20/ex10_7/StudentLexical.java | 17 ++++++ 24 files changed, 498 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Circle.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Rectangle.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/RightTriangle.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Shape.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Circle.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Rectangle.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/RightTriangle.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Shape.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/HazardPay.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/PayCalculator.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/RegularPay.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/MessageEncoder.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/ShuffleCipher.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/SubstitutionCipher.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/Person.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/Student.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/StudentLexical.java diff --git a/src/main/java/me/alex4386/gachon/sw14462/Main.java b/src/main/java/me/alex4386/gachon/sw14462/Main.java index 1744a5d..02af3f1 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 = "day18"; + public static String currentTarget = "day20"; public static boolean fallbackToLatest = true; public static Map> getAvailableTargetClassNames() { diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day20/Main.java new file mode 100644 index 0000000..1568b88 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/Main.java @@ -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; + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Circle.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Circle.java new file mode 100644 index 0000000..cc6c88d --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Circle.java @@ -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; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Main.java new file mode 100644 index 0000000..5fa3dc8 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Main.java @@ -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(); + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Rectangle.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Rectangle.java new file mode 100644 index 0000000..d277ed5 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Rectangle.java @@ -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; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/RightTriangle.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/RightTriangle.java new file mode 100644 index 0000000..47464e0 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/RightTriangle.java @@ -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; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Shape.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Shape.java new file mode 100644 index 0000000..2610740 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1a/Shape.java @@ -0,0 +1,7 @@ +package me.alex4386.gachon.sw14462.day20.ex10_1a; + +public abstract class Shape { + public double getArea() { + return 0.0; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Circle.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Circle.java new file mode 100644 index 0000000..043f4e8 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Circle.java @@ -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; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Main.java new file mode 100644 index 0000000..340f110 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Main.java @@ -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(); + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Rectangle.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Rectangle.java new file mode 100644 index 0000000..1a03c24 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Rectangle.java @@ -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; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/RightTriangle.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/RightTriangle.java new file mode 100644 index 0000000..f978f3f --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/RightTriangle.java @@ -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; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Shape.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Shape.java new file mode 100644 index 0000000..0655575 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1b/Shape.java @@ -0,0 +1,5 @@ +package me.alex4386.gachon.sw14462.day20.ex10_1b; + +public interface Shape { + public double getArea(); +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/HazardPay.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/HazardPay.java new file mode 100644 index 0000000..a1ccbde --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/HazardPay.java @@ -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; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/Main.java new file mode 100644 index 0000000..fd69834 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/Main.java @@ -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)); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/PayCalculator.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/PayCalculator.java new file mode 100644 index 0000000..38e39e4 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/PayCalculator.java @@ -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; + } + +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/RegularPay.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/RegularPay.java new file mode 100644 index 0000000..5b7423b --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1c/RegularPay.java @@ -0,0 +1,7 @@ +package me.alex4386.gachon.sw14462.day20.ex10_1c; + +public class RegularPay extends PayCalculator { + public RegularPay(double payRate) { + this.payRate = payRate; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/Main.java new file mode 100644 index 0000000..509cad2 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/Main.java @@ -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)); + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/MessageEncoder.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/MessageEncoder.java new file mode 100644 index 0000000..5fc8810 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/MessageEncoder.java @@ -0,0 +1,5 @@ +package me.alex4386.gachon.sw14462.day20.ex10_1d; + +public interface MessageEncoder { + public String encode(String plainText); +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/ShuffleCipher.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/ShuffleCipher.java new file mode 100644 index 0000000..87d2151 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/ShuffleCipher.java @@ -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; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/SubstitutionCipher.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/SubstitutionCipher.java new file mode 100644 index 0000000..f32107a --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_1d/SubstitutionCipher.java @@ -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(); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/Main.java new file mode 100644 index 0000000..2c39bac --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/Main.java @@ -0,0 +1,39 @@ +package me.alex4386.gachon.sw14462.day20.ex10_7; + +import java.util.Arrays; + +public class Main { + + private static void printSorting (T[] arr) { + System.out.println("Before Sorting:"); + for (T t : arr) { + System.out.println(t); + } + + System.out.println(); + Arrays.sort(arr); + + System.out.println("After Sorting:"); + for (T t : arr) { + System.out.println(t); + } + System.out.println(); + } + + public static void main(String[] args) { + Student[] students = new Student[3]; + students[0] = new Student("Bob", 2); + students[1] = new Student("Alex", 3); + students[2] = new Student("Charlie", 1); + + printSorting(students); + + System.out.println("Converting to StudentLexical for Lexical Sorting"); + StudentLexical[] studentsLexical = new StudentLexical[3]; + for (int i = 0; i < students.length; i++) { + studentsLexical[i] = new StudentLexical(students[i]); + } + + printSorting(studentsLexical); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/Person.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/Person.java new file mode 100644 index 0000000..1f3cbc9 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/Person.java @@ -0,0 +1,35 @@ +package me.alex4386.gachon.sw14462.day20.ex10_7; + +public class Person +{ + private String name; + public Person() + { + name = "No name yet"; + } + + public Person(String initialName) + { + name = initialName; + } + + public void setName(String newName) + { + name = newName; + } + + public String getName() + { + return name; + } + + public void writeOutput() + { + System.out.println("Name: " + name); + } + + public boolean hasSameName(Person otherPerson) + { + return this.name.equalsIgnoreCase(otherPerson.name); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/Student.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/Student.java new file mode 100644 index 0000000..aeabedc --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/Student.java @@ -0,0 +1,58 @@ +package me.alex4386.gachon.sw14462.day20.ex10_7; + +public class Student extends Person implements Comparable +{ + private int studentNumber; + public Student() + { + super(); + studentNumber = 0;//Indicating no number yet + } + public Student(String initialName, int initialStudentNumber) + { + super(initialName); + studentNumber = initialStudentNumber; + } + public void reset(String newName, int newStudentNumber) + { + setName(newName); + studentNumber = newStudentNumber; + } + public int getStudentNumber() + { + return studentNumber; + } + public void setStudentNumber(int newStudentNumber) + { + studentNumber = newStudentNumber; + } + public void writeOutput() + { + System.out.println("Name: " + getName()); + System.out.println("Student Number: " + studentNumber); + } + public boolean equals(Student otherStudent) + { + return this.hasSameName(otherStudent) && + (this.studentNumber == otherStudent.studentNumber); + } + + @Override + public int compareTo(Object o) { + Student otherStudent = (Student) o; + if (this.studentNumber < otherStudent.studentNumber) { + return -1; + } else if (this.studentNumber == otherStudent.studentNumber) { + return 0; + } else { + return 1; + } + } + + @Override + public String toString() { + return "Name: " + getName() + ", Student Number: " + studentNumber; + } +} + + diff --git a/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/StudentLexical.java b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/StudentLexical.java new file mode 100644 index 0000000..5727ae2 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day20/ex10_7/StudentLexical.java @@ -0,0 +1,17 @@ +package me.alex4386.gachon.sw14462.day20.ex10_7; + +public class StudentLexical extends Student implements Comparable +{ + + public StudentLexical(Student student) { + super(student.getName(), student.getStudentNumber()); + } + + @Override + public int compareTo(Object o) { + StudentLexical otherStudent = (StudentLexical) o; + return this.getName().compareTo(otherStudent.getName()); + } +} + +