实现抽象方法、实现接口 题目

JAVA25 实现抽象方法

描述
已知抽象类Base中定义了calculate方法,该方法的计算过程依赖于sum()和avg(),而后两个方法均为抽象方法。要求定义Base的子类Sub类,并实现父类的抽象方法,使得main函数中的运算逻辑得以正确执行。
 
输入描述:
两个整数
 
输出描述:
两个整数的和除以两个整数的平均值(平均值为int类型,不考虑小数问题)

示例:

输入:1 2
输出:3

 

分析:

​ 1.先把两个抽象方法实现。

​ 2.根据两个抽象方法的用法,编写方法的内容。

 

代码:

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......
    @Override
    public int sum() {
        return super.getY()+super.getX();
    }

    @Override
    public int avg() {
        return sum()/2;
    }

  


}

 
 

Sub类继承于Base类,故Sub类有getX()和getY()方法,super可以省略。

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......
    @Override
    public int sum() {
        return super.getY()+super.getX();
    }

    @Override
    public int avg() {
        return sum()/2;
    }

  


}

 

 

 

JAVA26 实现接口

描述
已知接口Comparator,内部定义了max函数,用于返回两个整数中的最大值。请定义该接口的实现类,使得main方法中的比较逻辑可以正确执行,要求实现类的名称为ComparatorImpl。
 
输入描述:
两个整数
 
输出描述:
两个整数中的最大值

示例:

输入:1 3
输出:3

 

分析:

​ 1.编写ComparatorImpl类实现(实现关键字:implements)接口Comparator。

​ 2.实现max函数。

​ 3.编写max函数具体代码。

 

代码:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Comparator comparator = new ComparatorImpl();

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            System.out.println(comparator.max(x, y));
        }
    }

}

interface Comparator {
    /**
     * 返回两个整数中的最大值
     */
    int max(int x, int y);
}

//write your code here......

 class ComparatorImpl implements Comparator{

     @Override
     public int max(int x, int y) {
         if(x>y)
             return x;
         else 
             return y;
        
     }
 }

 

简化代码

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Comparator comparator = new ComparatorImpl();

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            System.out.println(comparator.max(x, y));
        }
    }

}

interface Comparator {
    /**
     * 返回两个整数中的最大值
     */
    int max(int x, int y);
}

//write your code here......

 class ComparatorImpl implements Comparator{

     @Override
     public int max(int x, int y) {
        return x>y?x:y;
        
     }
 }
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值