定义一个父类base中的方法calculate(),该方法用于计算两个数的乘积(X*Y)。定义一个base类的子类Sub,在Sub中重写calculate()方法,将计算逻辑由乘法改为除法(X/Y)。注意,当分母为0时输出“Error”。
注意:类中的属性必须私有,部分代码已给出,这次请交博客链接,不然打回重做!!!
输入描述:
两个整数
输出描述:
两个整数的积和商(int类型,不考虑小数情况)
示例1
输入:
6 2
输出:
3 12
示例2
输入:
1 0
输出: Error 0
import java.util.Scanner;
class Sub{
private int x,y;
public Sub() {
}
public Sub(int x, int y) {
this.x = x;
this.y = y;
}
public void calculate(int x,int y){
System.out.println(x * y);
}
}
class base extends Sub{
private int x,y;
public base() {
}
public base(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void calculate(int x, int y) {
if (y == 0){
System.out.print("Error ");
}else{
System.out.print(x / y + " ");
}
}
}
class Day02{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNextInt()){
int x = scan.nextInt();
int y = scan.nextInt();
Sub sub = new Sub(x, y);
base base = new base(x, y);
base.calculate(x,y);
sub.calculate(x,y);
}
}
}


