程序填空:分析代码将程序补充完整,实现计算器功能,能正常完成加减乘除送 int computer ( int n , int m ):323240086 class Add 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 ) 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 )class UseComputer public void useCom ( Computer com , int one , int two ) public class Test public static void main ( String [] args ) UC . UseCom ( new Add ),10,20); uc . useCom ( new Sub ),15,10); uc . useCom ( new Mul0,10,20); uc . useCom ( new Div0,15,5)
时间: 2024-02-07 18:03:21 浏览: 176
程序补充完整后如下:
```
interface Computer {
int compute(int n, int m);
}
class Add implements Computer {
public int compute(int n, int m) {
System.out.print(n + " + " + m + " = ");
return n + m;
}
}
class Sub implements Computer {
public int compute(int n, int m) {
System.out.print(n + " - " + m + " = ");
return n - m;
}
}
class Mul implements Computer {
public int compute(int n, int m) {
System.out.print(n + " * " + m + " = ");
return n * m;
}
}
class Div implements Computer {
public int compute(int n, int m) {
System.out.print(n + " / " + m + " = ");
return n / m;
}
}
class UseComputer {
public void useCom(Computer com, int one, int two) {
int result = com.compute(one, two);
System.out.println(result);
}
}
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);
}
}
```
程序中定义了一个 `Computer` 接口,然后分别实现了 `Add`、`Sub`、`Mul` 和 `Div` 四个类,它们都实现了 `Computer` 接口,并实现了 `compute` 方法来完成加减乘除运算。最终在 `Test` 类的 `main` 方法中,创建了一个 `UseComputer` 对象,然后调用了 `useCom` 方法,传入不同的 `Computer` 子类对象和两个整数参数,来完成不同的运算。程序输出了每次运算的算式和结果。
阅读全文