-
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.
- Loading branch information
Showing
10 changed files
with
207 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
17 changes: 17 additions & 0 deletions
17
src/main/java/me/alex4386/gachon/sw14462/day10/DimensionConverter.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.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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/me/alex4386/gachon/sw14462/day10/DimensionConverterDemo.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.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."); | ||
} | ||
} |
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.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; | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/me/alex4386/gachon/sw14462/day10/ex6_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,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
35
src/main/java/me/alex4386/gachon/sw14462/day10/ex6_1a/Time.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,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); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/me/alex4386/gachon/sw14462/day10/ex6_1a/TimeTest.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.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()); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/me/alex4386/gachon/sw14462/day10/ex6_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,7 @@ | ||
package me.alex4386.gachon.sw14462.day10.ex6_1b; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
RoomTest.main(args); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/me/alex4386/gachon/sw14462/day10/ex6_1b/RoomOccupancy.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.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; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/me/alex4386/gachon/sw14462/day10/ex6_1b/RoomTest.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,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."); | ||
} | ||
} |