Skip to content

Commit

Permalink
chore: push
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex4386 committed Apr 6, 2024
1 parent 9b25a0b commit a35b1e3
Show file tree
Hide file tree
Showing 10 changed files with 207 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 = "day09";
public static String currentTarget = "day10";
public static boolean fallbackToLatest = true;

public static Map<String, Class<?>> getAvailableTargetClassNames() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.alex4386.gachon.sw14462.day10;

/**
Class of static methods to perform dimension conversions.
*/
public class DimensionConverter
{
public static final int INCHES_PER_FOOT = 12;
public static double convertFeetToInches(double feet)
{
return feet * INCHES_PER_FOOT;
}
public static double convertInchesToFeet(double inches)
{
return inches / INCHES_PER_FOOT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.alex4386.gachon.sw14462.day10;

import java.util.Scanner;

/**
Demonstration of using the class DimensionConverter.
*/
public class DimensionConverterDemo
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a measurement in inches: ");
double inches = keyboard.nextDouble();
double feet =
DimensionConverter.convertInchesToFeet(inches);
System.out.println(inches + " inches = " +
feet + " feet.");
System.out.print("Enter a measurement in feet: ");
feet = keyboard.nextDouble();
inches = DimensionConverter.convertFeetToInches(feet);
System.out.println(feet + " feet = " +
inches + " inches.");
}
}
18 changes: 18 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day10/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.alex4386.gachon.sw14462.day10;

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

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

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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.alex4386.gachon.sw14462.day10.ex6_1a;

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

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

public Time() {
this.hours = 0;
this.minute = 0;
}

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

public void setTime(int hours, int minute) {
if (Time.isValid(hours, minute)) {
this.hours = hours;
this.minute = minute;
}
}

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,32 @@
package me.alex4386.gachon.sw14462.day10.ex6_1a;

public class TimeTest {
public static void main(String[] args) {
Time time = new Time();

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: Setting time with setTime (05:49)");
time.setTime(5, 49);
System.out.println("Expected: 05:49");
System.out.println("Actual: " + time.toString());

System.out.println("Test scenario 5: Setting time with setTime2 (05:49 PM)");
time.setTime2(5, 49, true);
System.out.println("Expected: 17:49");
System.out.println("Actual: " + time.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.alex4386.gachon.sw14462.day10.ex6_1b;

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

public class RoomOccupancy {
private int roomNumber;
private int peopleInRoom;
private static int totalPeople = 0;

public RoomOccupancy(int roomNumber, int peopleInRoom) {
this.roomNumber = roomNumber;
this.peopleInRoom = peopleInRoom;
}

public void addOneToRoom() {
this.peopleInRoom++;
totalPeople++;
}

public void removeOneFromRoom() {
if (this.peopleInRoom > 0 && totalPeople > 0) {
this.peopleInRoom--;
totalPeople--;
}
}

public int getNumber() {
return this.roomNumber;
}

public static int getTotal() {
return totalPeople;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.alex4386.gachon.sw14462.day10.ex6_1b;

public class RoomTest {
public static void main(String[] args) {
RoomOccupancy room1 = new RoomOccupancy(101, 0);
RoomOccupancy room2 = new RoomOccupancy(102, 0);

System.out.println("Initial state:");
System.out.println("Room " + room1.getNumber() + " has " + room1.getTotal() + " people.");
System.out.println("Room " + room2.getNumber() + " has " + room2.getTotal() + " people.");

System.out.println("Adding 1 person to room1:");
room1.addOneToRoom();

System.out.println("Adding 2 people to room2:");
room2.addOneToRoom();
room2.addOneToRoom();

System.out.println("Expected behavior: since RoomOccupancy#getTotal() returns static variable totalPeople:");
System.out.println(" Both rooms should have 3 people.");

System.out.println("Room " + room1.getNumber() + " has " + room1.getTotal() + " people.");
System.out.println("Room " + room2.getNumber() + " has " + room2.getTotal() + " people.");

System.out.println("Removing 1 person from each room:");
room1.removeOneFromRoom();
room2.removeOneFromRoom();

System.out.println("Final state (after removing 1 person from each room):");
System.out.println("Room " + room1.getNumber() + " has " + room1.getTotal() + " people.");
System.out.println("Room " + room2.getNumber() + " has " + room2.getTotal() + " people.");
}
}

0 comments on commit a35b1e3

Please sign in to comment.