public class App3 {
public static void main(String[] args) {
// Win + R -> calc
SciCalc scicalc = new SciCalc();
int i = scicalc.square(3);
System.out.println(i);
Calc calc = new Calc();
i = calc.add(1,2);
System.out.println(i);
//calc.square(); 실행안됨
ProgCalc progcalc = new ProgCalc();
i = progcalc.sub(2,1);
System.out.println(i);
System.out.println();
// 'Ctrl + /'
calc.sub(4,2);
scicalc.add(1,5);
progcalc.add(4,4);
// System.out.println(calc.recent);
// System.out.println(scicalc.recent);
// System.out.println(progcalc.recent);
// SciCalcMk2 scicalcmk2 = new SciCalcMk2();
// i = scicalcmk2.add(3,5);
// System.out.println(i);
// i = scicalcmk2.square(5);
// System.out.println(i);
progcalc.add(4, 3.5f);
calc.recent(0);
scicalc.recent(0);
progcalc.recent(0);
calc.recent(1);
scicalc.recent(1);
progcalc.recent(1);
System.out.println(calc.numbering);
System.out.println(scicalc.numbering);
}
}
// Scientific Calculator is Calculator.
// Calculator has Memory
// Calc --> count = 2
// SciCalc --> count = 2
// Calc calc = new Calc()
// SciCalc scicalc = new SciCalc()
class Calc { //<-- 클래스
static int count =0;
int numbering;
Calc(){
count++;
numbering = count;
}
// int recent = -1;
Memory memory = new Memory();
int add(int a, int b){
String s = "" + a + "+" + b + "=" + (a+b);
memory.update(s);
return a+b;
}
// Overload
float add(float a, float b){
String s = "" + a + " + " + b + " = " + (a+b);
memory.update(s);
return a + b;
}
int sub(int a, int b){
String s = "" + a + "-" + b + "=" + (a-b);
memory.update(s);
return a-b;
}
void recent(int i){
System.out.println(memory.recent[i]);
}
}
class SciCalc extends Calc{
int square(int a){
String s = "" + a + "^2=" + (a*a);
memory.update(s);
return a*a;
}
}
class SciCalcMk2 extends SciCalc{
}
class ProgCalc extends Calc{
// Override
@Override
int add(int a, int b){
String s = "" + a + " 더하기 " + b + " 는" + (a+b);
memory.update(s);
return a+b;
}
// Overload
float add(int a ,float b){
String s = "" + a + " 더하기 " + b + " 는" + (a+b);
memory.update(s);
return a+b;
}
}
// 5, 3, 4
// 6
// 6, 5, 3
class Memory {
String[] recent = new String[3];
void update(String s){
recent[2] = recent[1];
recent[1] = recent[0];
recent[0] = s;
}
}