程序填空:分析代码将程序补充完整,实现计算器功能,能正常完成加减乘除运算。 _____(1)______ Computer { int computer(int n, int m); } class Add ________(2)___________ { public int computer(int n, int m) { System.out.print(n+"+"+m+"="); return n+m; } } class Sub implements Computer { public int computer (int n, int m) { ______________(3)_________________ return n-m; } } class Mul implements Computer { public int computer (int n, int m) { System.out.print(n+"*"+m+"="); return n*m; } } class Div implements Computer { public int computer (int n, int m) { System.out.print(n+"/"+m+"="); return n/m; } } class UseComputer { public void useCom(Computer com, int one, int two) { System.out.println (____________(4)______________); } } public class Test { public static void main (String[] args) { _______________(5)______________________ uc.useCom(new Add(), 10, 20); uc.useCom(new Sub(), 15, 10); uc.useCom(new Mul(), 10, 20); uc.useCom(new Div(), 15, 5); } }
时间: 2024-01-21 08:05:13 浏览: 70
实现计算器加减乘除的功能
代码如下:
```
(1) interface
Computer {
int computer(int n, int m);
}
class Add implements Computer {
public int computer(int n, int m) {
System.out.print(n + "+" + m + "=");
return n + m;
}
}
class Sub implements Computer {
public int computer(int n, int m) {
System.out.print(n + "-" + m + "=");
return n - m;
}
}
class Mul implements Computer {
public int computer(int n, int m) {
System.out.print(n + "*" + m + "=");
return n * m;
}
}
class Div implements Computer {
public int computer(int n, int m) {
System.out.print(n + "/" + m + "=");
return n / m;
}
}
class UseComputer {
public void useCom(Computer com, int one, int two) {
System.out.println(com.computer(one, two));
}
}
public class Test {
public static void main(String[] args) {
UseComputer uc = new UseComputer();
uc.useCom(new Add(), 10, 20);
uc.useCom(new Sub(), 15, 10);
uc.useCom(new Mul(), 10, 20);
uc.useCom(new Div(), 15, 5);
}
}
```
阅读全文