Skip to content

Commit 92f3b92

Browse files
committed
Build the class CalculatorClass.
1 parent d6f6ba7 commit 92f3b92

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

src/practice/CalculatorClass.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package practice;
2+
3+
import java.util.Scanner;
4+
5+
public class CalculatorClass {
6+
7+
// 두 매개변수를 더한다
8+
private int plus(int x, int y) {
9+
return x + y;
10+
}
11+
12+
// 두 매개변수를 뺀다
13+
private int minus(int x, int y) {
14+
return x - y;
15+
}
16+
17+
// 두 매개변수를 곱한다
18+
private int multiply(int x, int y) {
19+
return x * y;
20+
}
21+
22+
// 두 매개변수를 나눈다
23+
private int divide(int x, int y) {
24+
return x / y;
25+
}
26+
27+
// 숫자 3자리마다 ,(comma)로 구분한다
28+
public String printComma(long value) {
29+
30+
String result = "";
31+
String temp = String.valueOf(value);
32+
int len = temp.length();
33+
int header = len % 3; // 첫번째 콤마 위치
34+
char comma = ',';
35+
36+
// comma로 구분할 필요가 없을 때
37+
if (len < 4) {
38+
return temp;
39+
}
40+
41+
// len이 3의 배수라면 처음의 comma를 찍지 않는다
42+
if (header != 0) {
43+
result += temp.substring(0, header);
44+
result += comma;
45+
}
46+
47+
// 3자리마다 구분한다
48+
result += temp.substring(header, header + 3);
49+
for (int i=3; header+i+3<=len; i=i+3) {
50+
result += comma;
51+
result += temp.substring(header + i, header + i + 3);
52+
}
53+
54+
return result;
55+
}
56+
57+
// 계산을 수행한다
58+
public void calculate() {
59+
60+
Scanner scanner = new Scanner(System.in);
61+
CalculatorClass cal = new CalculatorClass();
62+
63+
String func = "";
64+
int x;
65+
int y;
66+
int result = 0;
67+
boolean isOperator = false;
68+
69+
// 사칙연산기호가 올바른지 검사한다
70+
while ( !isOperator ){
71+
System.out.print("사칙연산을 선택하세요.\n> ");
72+
func = scanner.nextLine();
73+
74+
isOperator = func.equals("+") || func.equals("-") || func.equals("*") || func.equals("/");
75+
}
76+
77+
// 계산할 매개변수를 입력 받는다
78+
System.out.print("x값을 입력하세요.\n> ");
79+
x = scanner.nextInt();
80+
System.out.print("y값을 입력하세요.\n> ");
81+
y = scanner.nextInt();
82+
83+
// 입력받은 계산을 수행한다
84+
switch (func) {
85+
case "+" : result = cal.plus(x, y); break;
86+
case "-" : result = cal.minus(x, y); break;
87+
case "*" : result = cal.multiply(x, y); break;
88+
case "/" : result = cal.divide(x, y); break;
89+
}
90+
91+
System.out.println("result: " + result);
92+
scanner.close();
93+
}
94+
95+
}

src/practice/MainClass.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package practice;
2+
3+
import static java.lang.System.out;
4+
5+
//import java.math.BigInteger;
6+
//import java.math.BigDecimal;
7+
import java.text.DecimalFormat;
8+
9+
public class MainClass {
10+
11+
public static void main(String[] args) {
12+
// BigInteger num = new BigInteger("123456789012345678901234567890");
13+
// BigDecimal number = new BigDecimal(num);
14+
CalculatorClass cal = new CalculatorClass();
15+
long l = (long)Math.pow(2, 62.999999999999997);
16+
String result = cal.printComma(l);
17+
18+
out.println(result);
19+
20+
DecimalFormat formatter = new DecimalFormat("#,###.00");
21+
out.println(formatter.format(l));
22+
23+
out.printf("%,d", l);
24+
out.println();
25+
out.println(String.format("%,d", l));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)