Skip to content

Commit

Permalink
chore: add homeworks
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex4386 committed Apr 8, 2024
1 parent a35b1e3 commit b3ad2fe
Show file tree
Hide file tree
Showing 16 changed files with 565 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 = "day10";
public static String currentTarget = "day11";
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/day11/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.alex4386.gachon.sw14462.day11;

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

public class Main {
public static String chainloadTarget = "ex6_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;
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day11/ex6_2a/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.alex4386.gachon.sw14462.day11.ex6_2a;

public class Main {
public static void main(String args[]) {
Suit cardSuit = Suit.SPADES;
System.out.println("cardSuit = Suit.SPADES");

System.out.println("cardSuit.ordinal() = " + cardSuit.ordinal());
System.out.println("cardSuit.equals(Suit.CLUBS) = " + cardSuit.equals(Suit.CLUBS));
System.out.println("cardSuit.compareTo(Suit.CLUBS) = " + cardSuit.compareTo(Suit.CLUBS));

System.out.println("Suit.valueOf(\"CLUBS\") = " + Suit.valueOf("CLUBS"));
System.out.println("Suit.valueOf(cardSuit.toString()) = " + Suit.valueOf(cardSuit.toString()));

System.out.println("cardSuit.getColor() = " + cardSuit.getColor());
System.out.println("cardSuit.toString() = " + cardSuit.toString());
}
}
15 changes: 15 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day11/ex6_2a/Suit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.alex4386.gachon.sw14462.day11.ex6_2a;

public enum Suit {
CLUBS("black"), DIAMONDS("red"), HEARTS("red"), SPADES("black");

private final String color;

private Suit(String suitColor) {
this.color = suitColor;
}

public String getColor() {
return this.color;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.alex4386.gachon.sw14462.day11.ex6_2b;

public class Main {
public static void main(String[] args) {
TimeTest.main(args);
}
}
57 changes: 57 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day11/ex6_2b/Time.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package me.alex4386.gachon.sw14462.day11.ex6_2b;

public class Time {
private int hours;
private int minute;

public Time() {
this(0,0);
}

public Time(int hours, int minute) {
if (Time.isValid(hours, minute)) {
this.hours = hours;
this.minute = minute;
} else {
throw new IllegalArgumentException("Invalid time");
}
}

public Time(int hours, int minute, boolean isPM) throws IllegalArgumentException {
this(hours + (isPM ? 12 : 0), minute);
}

public static boolean isValid(int hours, int minute) {
return (hours >= 0 && hours <= 23) &&
(minute >= 0 && minute <= 59);
}

public String getTime24() {
return String.format("%02d%02d", this.hours, this.minute);
}

public String getTime12() {
return String.format("%d:%02d %s", this.hours % 12, this.minute, this.hours >= 12 ? "PM" : "AM");
}

public void setTime(int hours, int minute) {
if (Time.isValid(hours, minute)) {
this.hours = hours;
this.minute = minute;
} else {
throw new IllegalArgumentException("Invalid time");
}
}

public void setTime2(int hours, int minute, boolean isPM) {
// seems not intended when PM=true and hours=12,
// but it's not mentioned in the requirement
int hours24 = hours + (isPM ? 12 : 0);
this.setTime(hours24, minute);
}

@Override
public String toString() {
return String.format("%02d:%02d", this.hours, this.minute);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package me.alex4386.gachon.sw14462.day11.ex6_2b;

public class TimeTest {
public static void main(String[] args) {
System.out.println("Test scenario 1: valid time (12:49)");
boolean result = Time.isValid(12, 49);
System.out.println("Expected: true");
System.out.println("Actual: " + result);

System.out.println("Test scenario 2: invalid time (24:00)");
result = Time.isValid(24, 0);
System.out.println("Expected: false");
System.out.println("Actual: " + result);

System.out.println("Test scenario 3: invalid time (12:60)");
result = Time.isValid(12, 60);
System.out.println("Expected: false");
System.out.println("Actual: " + result);

System.out.println("Test scenario 4: Initialize Time to (05:49)");
Time time = new Time(5, 49);
System.out.println("Expected: 05:49");
System.out.println("Actual: " + time.toString());

System.out.println("Test scenario 5: Initialize Time with alternative constructor to 05:49 PM (17:49)");
time = new Time(5, 49, true);
System.out.println("Expected: 17:49");
System.out.println("Actual: " + time.toString());

System.out.println("Test scenario 6: Test time.getTime24() is 17:49");
System.out.println("Expected: 1749");
System.out.println("Actual: " + time.getTime24());

System.out.println("Test scenario 7: Test time.getTime12() is 5:49 PM");
System.out.println("Expected: 5:49 PM");
System.out.println("Actual: " + time.getTime12());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package me.alex4386.gachon.sw14462.day11.ex6_4;

import java.util.Scanner;

public class Characteristic {

private String description;
private int rating;

public Characteristic(String description) {
this.description = description;
this.rating = 0;
}

private boolean isValid(int aRating) {
// range check
return aRating >= 1 && aRating <= 10;
}

public void setRating(int aRating) {
if (this.isValid(aRating)) {
this.rating = aRating;
} else {
throw new IllegalArgumentException("Invalid rating");
}
}

public void setRating() {
Scanner scanner = new Scanner(System.in);

System.out.println("Characteristic: "+this.description);
System.out.print("Enter a rating: ");
int aRating = scanner.nextInt();

try {
this.setRating(aRating);
} catch (IllegalArgumentException e) {
System.err.println("Invalid Rating! Please enter a rating between 1 and 10.");
}
}


public String getDescription() {
return this.description;
}

public int getRating() {
return this.rating;
}

private double getCompatibilityMeasure(Characteristic otherRating) {
// early return to catch if it hasn't rated yet.
if (this.rating == 0 || otherRating.rating == 0) {
return 0.0;
}

return 1.0 - (Math.pow(this.rating - otherRating.rating, 2) / 81.0);
}

private boolean isMatch(Characteristic otherRating) {
return this.description.equals(otherRating.description);
}

public double getCompatibility(Characteristic otherRating) {
return this.isMatch(otherRating) ?
this.getCompatibilityMeasure(otherRating) :
0.0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.alex4386.gachon.sw14462.day11.ex6_4;

public class CharacteristicTest {
public static void main(String[] args) {
Characteristic character1 = new Characteristic("Sample Description for Characteristic 1, 3 and 4");
Characteristic character2 = new Characteristic("Sample Description for Characteristic 2");
Characteristic character3 = new Characteristic("Sample Description for Characteristic 1, 3 and 4");
Characteristic character4 = new Characteristic("Sample Description for Characteristic 1, 3 and 4");

System.out.println("character1.getDescription() = " + character1.getDescription());
System.out.println("character2.getDescription() = " + character2.getDescription());
System.out.println("character3.getDescription() = " + character3.getDescription());
System.out.println("character4.getDescription() = " + character4.getDescription());

System.out.println("Requesting rating for character1...");
character1.setRating();
System.out.println("character1.getRating() = " + character1.getRating());

if (character1.getRating() == 0) {
System.out.println("Due to invalid rating input, character1's rating is still 0. Provisioning instance's rating to 5.");
character1.setRating(5);
}

System.out.println("Setting rating for character2 to "+character1.getRating());
character2.setRating(character1.getRating());
System.out.println("character2.getRating() = " + character2.getRating());

System.out.println("Setting rating for character3 to "+character1.getRating());
character3.setRating(character1.getRating());
System.out.println("character3.getRating() = " + character2.getRating());

System.out.println("Setting rating for character4 to 9");
character4.setRating(1);
System.out.println("character4.getRating() = " + character4.getRating());

System.out.println("character1.getCompatibility(character2) = " + character1.getCompatibility(character2));
System.out.println("character1.getCompatibility(character3) = " + character1.getCompatibility(character3));
System.out.println("character1.getCompatibility(character4) = " + character1.getCompatibility(character4));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.alex4386.gachon.sw14462.day11.ex6_4;

public class Main {

public static void main(String[] args) {
CharacteristicTest.main(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.alex4386.gachon.sw14462.day11.ex6_6;

public class Main {
public static void main(String[] args) {
PersonTest.main(args);
}
}
93 changes: 93 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day11/ex6_6/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package me.alex4386.gachon.sw14462.day11.ex6_6;

public class Person
{
private String name;
private int age;

/* Start of Exercise 10 */
public Person() {
this("No name yet", 0);
}

public Person(String name, int age) {
this.name = name;
this.age = age;
}


public static Person createAdult() {
return new Person("An adult", 21);
}
/* End of Exercise 10 */

/* From the Self-Test Question 16 */

/*
// Commented-out since due to duplicate requirement
// from Programming Project 6
public void setName(String newName)
{
name = newName;
}
public void setAge(int newAge)
{
if (newAge >= 0)
age = newAge;
else
{
System.out.println("ERROR: Age is " +
"negative.");
System.exit(0);
}
}
*/

public void setPerson(String newName, int newAge)
{
setName(newName);
setAge(newAge);
}
/* End of Self-Test Question 16 */


/* Start of Programming projects 6 */
public String getName() {
return this.name;
}

public int getAge() {
return this.age;
}

public void setName(String first, String last) {
this.name = first + " " + last;
}

public void setName(String name) {
this.name = name;
}

public void setAge(int age) {
if (this.age < 0) throw new IllegalArgumentException("Age cannot be negative");
this.age = age;
}

public static Person createToddler() {
return new Person("A toddler", 2);
}

public static Person createPreschooler() {
return new Person("A preschooler", 5);
}

public static Person createAdolescent() {
return new Person("An adolescent", 9);
}

public static Person createTeenager() {
return new Person("A teenager", 15);
}
}
Loading

0 comments on commit b3ad2fe

Please sign in to comment.