-
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
6 changed files
with
201 additions
and
17 deletions.
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
25 changes: 25 additions & 0 deletions
25
src/main/java/me/alex4386/gachon/sw14462/day03/DigitParser.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.day03; | ||
|
||
public class DigitParser { | ||
protected int integer; | ||
protected int currentDigitPow; | ||
|
||
public DigitParser(int integer) { | ||
this.integer = integer; | ||
this.currentDigitPow = DigitParser.digitPow(integer) - 1; | ||
} | ||
|
||
public static int digitPow(int integer) { | ||
return String.valueOf(integer).length(); | ||
} | ||
|
||
public int nextDigit() { | ||
if (this.currentDigitPow < 0) { | ||
return -1; | ||
} | ||
|
||
int digit = (int) ((this.integer / Math.pow(10, this.currentDigitPow)) % 10); | ||
this.currentDigitPow--; | ||
return digit; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/me/alex4386/gachon/sw14462/day03/EggBasket.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,19 @@ | ||
package me.alex4386.gachon.sw14462.day03; | ||
|
||
public class EggBasket { | ||
// At the beginning of the section enclosed by curly braces `{}` | ||
public static void main(String[] args) { | ||
// A variable must be declared before it is used | ||
int numberOfBaskets, eggsPerBasket, totalEggs; // variable declaration | ||
|
||
numberOfBaskets = 20; // assignment statement | ||
eggsPerBasket = 18; | ||
|
||
totalEggs = numberOfBaskets * eggsPerBasket; | ||
|
||
System.out.println("If you have"); | ||
System.out.println(eggsPerBasket + " eggs per basket and"); | ||
System.out.println(numberOfBaskets + " baskets, then"); | ||
System.out.println("the total number of eggs is " + totalEggs); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/me/alex4386/gachon/sw14462/day03/FahrenheitConverter.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.day03; | ||
|
||
public class FahrenheitConverter { | ||
public static double toCelsius(double fahrenheit) { | ||
return (fahrenheit - 32) * 5 / 9; | ||
} | ||
} |
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,40 @@ | ||
package me.alex4386.gachon.sw14462.day03; | ||
|
||
import java.util.Scanner; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
System.out.println("Exercise 2_1. Read a four-digit integer and display one digit per line"); | ||
Main.exercise2_1(); | ||
|
||
System.out.println(""); | ||
System.out.println("Exercise 2_2. Convert Fahrenheit to Celsius"); | ||
Main.exercise2_2(); | ||
} | ||
|
||
public static void exercise2_1() { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
System.out.print("Enter a four-digit integer: "); | ||
int integer = scanner.nextInt(); | ||
|
||
DigitParser parser = new DigitParser(integer); | ||
while (true) { | ||
int digit = parser.nextDigit(); | ||
if (digit == -1) { | ||
break; | ||
} | ||
System.out.println(digit); | ||
} | ||
} | ||
|
||
public static void exercise2_2() { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
System.out.print("Enter a temperature in Fahrenheit: "); | ||
double fahrenheit = scanner.nextDouble(); | ||
|
||
double celsius = FahrenheitConverter.toCelsius(fahrenheit); | ||
System.out.printf("The temperature in Celsius is %.02f°C.\n", celsius); | ||
} | ||
} |