javaSE21/10/11

时间:2021-10-11
本文章向大家介绍javaSE21/10/11,主要包括javaSE21/10/11使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

面向对象流水线

有理数的类封装

public class Rational {
    int numerator = 1;//分子分母初始值都设1
    int denominator =1;
    //设置分子的值,化简的过程
    void setNumerator(int a){//a为暂时的分子
        int c=f(Math.abs(a),denominator);
        numerator = a/c;
        denominator=denominator/c;
        if (numerator<0&&denominator<0){
            numerator = -numerator;
            denominator = -denominator;
        }
    }
    //设置分母,化简的过程
    void setDenominator(int b){
        int c=f(numerator,Math.abs(b));
        numerator = numerator/c;
        denominator=b/c;
        if (numerator<0&&denominator<0){
            numerator = -numerator;
            denominator = -denominator;
        }
    }
    int getNumerator(){
        return numerator;
    }
    int getDenominator(){
        return denominator;
    }
    //求最大公约数
    int f(int a,int b){
        if (a==0){
            return 1;
        }
        if (a<b){
            int t = 0;
            t = a;
            a = b;
            b = t;
        }
        int r = a%b;
         while (r!=0){
             a = b;
             b = r;
             r = a%b;
         }
        return b;
    }
    //加法运算
    Rational add(Rational r){
        int a = r.getNumerator();//获取所算的分数的分子
        int b = r.getDenominator();//获取分母
        int newNumerator = numerator*b+denominator*a;
        int newDenominator = denominator*b;
        Rational result = new Rational();//新建一个结果
        result.setNumerator(newNumerator);
        result.setDenominator(newDenominator);
        return result;
    }
    Rational sub(Rational r){
        int a = r.getNumerator();//获取所算的分数的分子
        int b = r.getDenominator();//获取分母
        int newNumerator = numerator*b-denominator*a;
        int newDenominator = denominator*b;
        Rational result = new Rational();//新建一个结果
        result.setNumerator(newNumerator);
        result.setDenominator(newDenominator);
        return result;
    }
    Rational muti(Rational r){
        int a = r.getNumerator();//获取所算的分数的分子
        int b = r.getDenominator();//获取分母
        int newNumerator = numerator*a;
        int newDenominator = denominator*b;
        Rational result = new Rational();//新建一个结果
        result.setNumerator(newNumerator);
        result.setDenominator(newDenominator);
        return result;
    }
    Rational div(Rational r){
        int a = r.getNumerator();//获取所算的分数的分子
        int b = r.getDenominator();//获取分母
        int newNumerator = numerator*b;
        int newDenominator = denominator*a;
        Rational result = new Rational();//新建一个结果
        result.setNumerator(newNumerator);
        result.setDenominator(newDenominator);
        return result;
    }
}
public class RationalTest {
    public static void main(String[] args) {
        Rational r1 = new Rational();
        Rational r2 = new Rational();
        r1.setNumerator(3);
        r1.setDenominator(5);
        r2.setNumerator(1);
        r2.setDenominator(6);
        Rational result = r1.add(r2);
        int a = result.getNumerator();
        int b = result.getDenominator();
        System.out.println("3/5 + 1/6 = "+a+"/"+b);
    }
}
  • Shift+Tab代码整体向前移,Tab代码整体向后移

搭建流水线

public class SingGame {
    public static void main(String[] args) {
        Line line = new Line();
        line.giveScore();
    }
}
public class Line {
    InputScore inputScore;//先定义对象,不分配内存空间
    DelScore delScore;
    ComputerAver computer;

    Line() {//创建流水线对象后自动给各个类分配空间
        computer = new ComputerAver();
        delScore = new DelScore(computer);//b对象含有c对象的引用
        inputScore = new InputScore(delScore);//a对象含有b的引用,
    }
    public void giveScore(){
        inputScore.inputScore();
    }
}
public class InputScore { //录入分数,a对象
    DelScore delScore;//定义b对象
    InputScore(DelScore delScore) {
        this.delScore = delScore;//构造方法的参数是b,
    }

    public void inputScore(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入评委数:");
        int count = scanner.nextInt();
        System.out.println("请输入分数:");
        double[] a = new double[count];
        for (int i = 0; i <count ; i++) {
            a[i] = scanner.nextDouble();
        }
        delScore.doDelete(a);//调用b对象的方法,在执行完a后自动执行b
    }

}
public class DelScore {//去掉一个最高分和最低分,b对象
    ComputerAver computer;

    DelScore(ComputerAver computer) {
        this.computer = computer;
    }
    public void doDelete(double[] a){
        Arrays.sort(a);
        System.out.println("去掉一个最高分:"+a[a.length-1]);
        System.out.println("去掉一个最低分:"+a[0]);
        double[] b = new double[a.length-2];
        for (int i = 0; i < b.length; i++) {
            b[i]=a[i+1];
        }
        computer.giveAver(b);
    }
}
public class ComputerAver {//求平均分,c对象

    public void giveAver(double[] b){
        double sum = 0;
        for (int i = 0; i < b.length; i++) {
            sum+=b[i];
        }
        double aver = sum/b.length;
        System.out.println("选手最后得分:"+aver);
    }
}

原文地址:https://www.cnblogs.com/wpxz1005/p/15395050.html