-
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
5 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/me/alex4386/gachon/sw14462/day06/CheckService.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,20 @@ | ||
package me.alex4386.gachon.sw14462.day06; | ||
|
||
public class CheckService { | ||
public static double acceptCheck(double checkAmount) { | ||
double fee = 0; | ||
if (checkAmount < 10) { | ||
fee = 1; | ||
} else if (checkAmount < 100) { | ||
fee = 0.1 * checkAmount; | ||
} else if (checkAmount < 1000) { | ||
fee = 5; | ||
fee += 0.05 * checkAmount; | ||
} else { | ||
fee = 40; | ||
fee += 0.01 * checkAmount; | ||
} | ||
|
||
return fee; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/me/alex4386/gachon/sw14462/day06/GradeConverter.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,27 @@ | ||
package me.alex4386.gachon.sw14462.day06; | ||
|
||
public class GradeConverter { | ||
public static double letterGradeToGradePoint(char grade) { | ||
double point = 0.0; | ||
switch (grade) { | ||
case 'A': | ||
point = 4.0; | ||
break; | ||
case 'B': | ||
point = 3.0; | ||
break; | ||
case 'C': | ||
point = 2.0; | ||
break; | ||
case 'D': | ||
point = 1.0; | ||
break; | ||
case 'F': | ||
default: | ||
point = 0.0; | ||
break; | ||
} | ||
|
||
return point; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/me/alex4386/gachon/sw14462/day06/GradeStatistics.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,65 @@ | ||
package me.alex4386.gachon.sw14462.day06; | ||
|
||
public class GradeStatistics extends IntegerStatistics { | ||
|
||
int aCount = 0; | ||
int bCount = 0; | ||
int cCount = 0; | ||
int dCount = 0; | ||
int fCount = 0; | ||
|
||
public GradeStatistics() { | ||
total = 0; | ||
count = 0; | ||
} | ||
|
||
@Override | ||
public boolean add(int value) throws IllegalArgumentException { | ||
if (value > 100) { | ||
throw new IllegalArgumentException("Grade must be between 0 and 100"); | ||
} | ||
|
||
boolean isValid = super.add(value); | ||
if (isValid) { | ||
switch (value / 10) { | ||
case 10: | ||
case 9: | ||
aCount++; | ||
break; | ||
case 8: | ||
bCount++; | ||
break; | ||
case 7: | ||
cCount++; | ||
break; | ||
case 6: | ||
dCount++; | ||
break; | ||
default: | ||
fCount++; | ||
break; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public int getACount() { | ||
return aCount; | ||
} | ||
|
||
public int getBCount() { | ||
return bCount; | ||
} | ||
|
||
public int getCCount() { | ||
return cCount; | ||
} | ||
|
||
public int getDCount() { | ||
return dCount; | ||
} | ||
|
||
public int getFCount() { | ||
return fCount; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/me/alex4386/gachon/sw14462/day06/IntegerStatistics.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,53 @@ | ||
package me.alex4386.gachon.sw14462.day06; | ||
|
||
public class IntegerStatistics { | ||
long total; | ||
int count; | ||
|
||
int max = Integer.MIN_VALUE; | ||
int min = Integer.MAX_VALUE; | ||
|
||
boolean finalized = false; | ||
|
||
public IntegerStatistics() { | ||
total = 0; | ||
count = 0; | ||
} | ||
|
||
public boolean add(int value) { | ||
if (finalized) return true; | ||
|
||
if (value < 0) { | ||
this.finalized = true; | ||
return true; | ||
} | ||
|
||
if (value < min) min = value; | ||
if (value > max) max = value; | ||
|
||
total += value; | ||
count++; | ||
|
||
return false; | ||
} | ||
|
||
public boolean hasFinalized() { | ||
return this.finalized; | ||
} | ||
|
||
public int getMin() { | ||
return this.min; | ||
} | ||
|
||
public int getMax() { | ||
return this.max; | ||
} | ||
|
||
public int getCount() { | ||
return this.count; | ||
} | ||
|
||
public double getAverage() { | ||
return ((double) total) / count; | ||
} | ||
} |
144 changes: 144 additions & 0 deletions
144
src/main/java/me/alex4386/gachon/sw14462/day06/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,144 @@ | ||
package me.alex4386.gachon.sw14462.day06; | ||
|
||
import me.alex4386.gachon.sw14462.day03.CoinPayment; | ||
import me.alex4386.gachon.sw14462.day03.coins.CoinImpl; | ||
import me.alex4386.gachon.sw14462.day05.Ex3_3; | ||
import me.alex4386.gachon.sw14462.day05.Ex3_6; | ||
import me.alex4386.gachon.sw14462.day05.Ex3_7; | ||
|
||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public class Main { | ||
|
||
static Scanner scanner = null; | ||
|
||
public static Scanner getScanner() { | ||
if (scanner == null) { | ||
scanner = new Scanner(System.in); | ||
} | ||
|
||
return scanner; | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println("Practice 3.1"); | ||
System.out.println("Practice 3.1a. Write a cashing program"); | ||
Main.exercise3_1a(); | ||
System.out.println(""); | ||
|
||
System.out.println("Practice 3.1b. Extend the existing get charge coins"); | ||
Main.exercise3_1b(); | ||
System.out.println(""); | ||
|
||
System.out.println("Practice 3.1c. Write a letter grade converter using switch statement"); | ||
Main.exercise3_1c(); | ||
System.out.println(""); | ||
System.out.println(""); | ||
|
||
System.out.println("Practice 3.2"); | ||
System.out.println("Practice 3.2a. Write an integer collection/statistics program"); | ||
Main.exercise3_2a(); | ||
|
||
System.out.println("Practice 3.2b. Extend collection/statistics program for grading"); | ||
Main.exercise3_2b(); | ||
|
||
System.out.println(""); | ||
System.out.println(""); | ||
|
||
System.out.println("Textbook Programming Project ----"); | ||
System.out.println("Exercise 3.3. Write a program that reads a bank account balance and an interest rate and displays the value of the account in ten years."); | ||
System.out.println("ClassPath: "+ Ex3_3.class.getCanonicalName()); | ||
Ex3_3.main(args); | ||
System.out.println(""); | ||
|
||
System.out.println("Exercise 3.6. Write a program that asks the user to enter the size of a triangle."); | ||
System.out.println("ClassPath: "+ Ex3_6.class.getCanonicalName()); | ||
Ex3_6.main(args); | ||
System.out.println(""); | ||
|
||
System.out.println("Exercise 3.7. Write a program that simulates a bouncing ball by computing its height in feet at each second as time passes on a simulated clock."); | ||
System.out.println("ClassPath: "+ Ex3_7.class.getCanonicalName()); | ||
Ex3_7.main(args); | ||
System.out.println(""); | ||
} | ||
|
||
public static void exercise3_1a() { | ||
System.out.print("Enter the amount of check: "); | ||
double checkAmount = getScanner().nextDouble(); | ||
|
||
double charge = CheckService.acceptCheck(checkAmount); | ||
System.out.println("The charge is $"+charge); | ||
} | ||
|
||
public static void exercise3_1b() { | ||
System.out.print("Enter the price (in cents): "); | ||
int price = getScanner().nextInt(); | ||
|
||
if (price < 25 || price % 5 != 0 || price > 100) { | ||
System.err.println("Invalid input. Please enter a number between 25 and 100, divisible by 5."); | ||
} else { | ||
int payCharge = 100 - price; | ||
|
||
try { | ||
CoinPayment chargePayment = new CoinPayment(payCharge); | ||
List<CoinImpl> paidCoins = chargePayment.pay(); | ||
|
||
System.out.println("Charge coins:"); | ||
for (CoinImpl coin : CoinPayment.coins) { | ||
int count = 0; | ||
for (CoinImpl paidCoin : paidCoins) { | ||
if (paidCoin.getClass().equals(coin.getClass())) { | ||
count++; | ||
} | ||
} | ||
System.out.println(coin.toString() + ": " + count); | ||
} | ||
} catch (IllegalArgumentException e) { | ||
System.err.println(e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
public static void exercise3_1c() { | ||
System.out.print("Enter the letter based grade: "); | ||
char character = getScanner().next().charAt(0); | ||
|
||
double gradePoint = GradeConverter.letterGradeToGradePoint(character); | ||
System.out.println("The letter grade is "+gradePoint+"."); | ||
} | ||
|
||
public static void exercise3_2a() { | ||
IntegerStatistics stats = new IntegerStatistics(); | ||
|
||
while (!stats.hasFinalized()) { | ||
System.out.print("Enter the integer: "); | ||
int integer = getScanner().nextInt(); | ||
|
||
stats.add(integer); | ||
} | ||
|
||
System.out.println("Max: "+stats.getMax()); | ||
System.out.println("Min: "+stats.getMin()); | ||
System.out.println("Avg: "+stats.getAverage()); | ||
} | ||
|
||
public static void exercise3_2b() { | ||
GradeStatistics stats = new GradeStatistics(); | ||
|
||
while (!stats.hasFinalized()) { | ||
System.out.print("Enter the score: "); | ||
int score = getScanner().nextInt(); | ||
|
||
stats.add(score); | ||
} | ||
|
||
System.out.println("total count: "+stats.getCount()); | ||
System.out.print("A count: "+stats.getACount()+" "); | ||
System.out.print("B count: "+stats.getBCount()+" "); | ||
System.out.print("C count: "+stats.getCCount()+" "); | ||
System.out.print("D count: "+stats.getDCount()+" "); | ||
System.out.print("F count: "+stats.getFCount()+""); | ||
System.out.println(""); | ||
} | ||
} |