JAVA25. 实现抽象方法
描述
已知抽象类Base中定义了calculate方法,该方法的计算过程依赖于sum()和avg(),而后两个方法均为抽象方法。要求定义Base的子类Sub类,并实现父类的抽象方法,使得main函数中的运算逻辑得以正确执行。输入描述
两个整数输出描述
两个整数的和除以两个整数的平均值(平均值为int类型,不考虑小数问题)示例1
输入:
1 2
输出:
3
Java 解法, 执行用时: 13ms, 内存消耗: 9436KB, 提交时间: 2022-05-20
import java.util.Scanner; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args)throws IOException { // Sub是需要你定义的子类 Base base = new Sub(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String [] read = br.readLine().split(" "); int x = Integer.parseInt(read[0]); int y = Integer.parseInt(read[1]); base.setX(x); base.setY(y); System.out.println(base.calculate()); } } abstract class Base { private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int calculate() { if (avg() == 0) { return 0; } else { return sum() / avg(); } } /** * 返回x和y的和 */ public abstract int sum(); /** * 返回x和y的平均值 */ public abstract int avg(); } class Sub extends Base { //write your code here...... public int sum() { return super.getX() + super.getY(); } public int avg() { return sum() / 2; } }
Java 解法, 执行用时: 14ms, 内存消耗: 9380KB, 提交时间: 2022-04-27
import java.util.Scanner; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { // Sub是需要你定义的子类 Base base = new Sub(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String [] read =br.readLine().split(" "); int x = Integer.parseInt(read[0]); int y = Integer.parseInt(read[1]); base.setX(x); base.setY(y); System.out.println(base.calculate()); } } abstract class Base { private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int calculate() { if (avg() == 0) { return 0; } else { return sum() / avg(); } } /** * 返回x和y的和 */ public abstract int sum(); /** * 返回x和y的平均值 */ public abstract int avg(); } class Sub extends Base { public void setX(int x){ super.setX(x); } public void setY(int y){ super.setY(y); } public int sum(){ return super.getX()+super.getY(); } public int avg(){ return sum()/2; } }
Java 解法, 执行用时: 14ms, 内存消耗: 9556KB, 提交时间: 2022-04-29
import java.util.Scanner; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { // Sub是需要你定义的子类 Base base = new Sub(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String [] read = br.readLine().split(" "); int x = Integer.parseInt(read[0]); int y = Integer.parseInt(read[1]); base.setX(x); base.setY(y); System.out.println(base.calculate()); } } abstract class Base { private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int calculate() { if (avg() == 0) { return 0; } else { return sum() / avg(); } } public abstract int sum(); public abstract int avg(); } class Sub extends Base { public void setX(int x) { super.setX(x); } public void setY(int y) { super.setY(y); } public int sum() { return super.getX() + super.getY(); } public int avg() { return sum() / 2; } }
Java 解法, 执行用时: 26ms, 内存消耗: 10756KB, 提交时间: 2022-02-10
import java.util.Scanner; public class Main { public static void main(String[] args) { // Sub是需要你定义的子类 Base base = new Sub(); Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()) { int x = scanner.nextInt(); int y = scanner.nextInt(); base.setX(x); base.setY(y); System.out.println(base.calculate()); } } } abstract class Base { private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int calculate() { if (avg() == 0) { return 0; } else { return sum() / avg(); } } /** * 返回x和y的和 */ public abstract int sum(); /** * 返回x和y的平均值 */ public abstract int avg(); } class Sub extends Base { public int sum(){ return super.getX() + super.getY(); } public int avg(){ return sum()/2; } }
Java 解法, 执行用时: 27ms, 内存消耗: 10564KB, 提交时间: 2022-02-09
import java.util.Scanner; public class Main { public static void main(String[] args) { // Sub是需要你定义的子类 Base base = new Sub(); Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()) { int x = scanner.nextInt(); int y = scanner.nextInt(); base.setX(x); base.setY(y); System.out.println(base.calculate()); } } } abstract class Base { private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int calculate() { if (avg() == 0) { return 0; } else { return sum() / avg(); } } /** * 返回x和y的和 */ public abstract int sum(); /** * 返回x和y的平均值 */ public abstract int avg(); } class Sub extends Base { //write your code here...... public int sum(){ return super.getX() + super.getY(); } public int avg(){ return sum()/2; } }