java 我想把它变成通用的

dwbf0jvd  于 2023-02-14  发布在  Java
关注(0)|答案(1)|浏览(132)

所以我已经写了这段代码,它可以很好地处理整数。现在我想让它成为泛型(extends Number),但要保持它是一个数组。

import java.util.Arrays;

public class StudentGrades {
    private int grades[];
    private int pointer;
    private int max_grade;
    private int min_grade;
    private double average_grade;
    private  int better_than_average;
    private  int worse_than_average;
    private int excelent_count;
    private int good_count;
    private int enaught_count;

    public int getMax_grade() {
        return max_grade;
    }

    public int getMin_grade() {
        return min_grade;
    }

    public double getAverage_grade() {
        return average_grade;
    }

    public int getBetter_than_average() {
        return better_than_average;
    }

    public int getWorse_than_average() {
        return worse_than_average;
    }

    public int getExcelent_count() {
        return excelent_count;
    }

    public int getGood_count() {
        return good_count;
    }

    public int getEnough_count() {
        return enaught_count;
    }

    StudentGrades()
    {
        pointer=-1;
        grades=new int[1000];
        average_grade=0;
        better_than_average=0;
        worse_than_average=0;
        excelent_count=0;
        good_count=0;
        enaught_count=0;
        max_grade=0;
        min_grade=0;
    }

    private void calculate()
    {
        average_grade=0;
        better_than_average=0;
        worse_than_average=0;
        excelent_count=0;
        good_count=0;
        enaught_count=0;
        max_grade=0;
        min_grade=0;
        if (pointer<0)return;
        double sum=0;
        max_grade=grades[0];
        min_grade=grades[0];
        for (int i=0; i<=pointer;i++)
        {
            sum+=grades[i];
            if(grades[i]>max_grade)
                max_grade=grades[i];
            if(grades[i]<min_grade)
                min_grade=grades[i];
            if(grades[i]>=91)
                excelent_count++;
            else if(grades[i]>=71)
                good_count++;
            else if (grades[i]>=60)
                enaught_count++;
        }
        average_grade=sum/(double) (pointer+1);

        for(int i=0; i<=pointer;i++)
        {
            if(grades[i]>average_grade)
                better_than_average++;
            else
                worse_than_average++;
        }
    }

    private boolean is_valid(int n)
    {
        return (n>0 && n<100);
    }

    public boolean setter( int marks[])
    {
        for(int i=0; i<marks.length; i++)
        {
            if(!is_valid(marks[i]))
                return false;
        }
        grades=marks;
        pointer=marks.length-1;
        calculate();
        return true;
    }

    public boolean adder(int new_element)
    {
        if(!is_valid(new_element))
            return false;
        if(grades.length==pointer+1)
        {
            int[] new_size_array =new int[grades.length*2];
            for(int i=0; i<=pointer;i++) new_size_array[i]=grades[i];
            grades=new_size_array;
        }
        pointer++;
        grades[pointer]=new_element;
        calculate();
        return true;
    }

    public int[] getter()
    {
        int[] result = new int[pointer+1];
        for (int i=0; i<=pointer;i++)
            result[i]=grades[i];
        return  result;
    }
    public void to_string(){
        System.out.println(Arrays.toString(getter()));
    }


}

我已经这样做了,但我强烈认为这是完全错误的,我正在朝着错误的方向前进。我相对来说是新的编程,所以请尽可能详细地解释。谢谢!

import java.util.Arrays;
import java.lang.reflect.Array;

public class StudentGradesGeneric <E extends Comparable<E>> extends Number{

    public StudentGradesGeneric(Class<E> clazz, int capacity) {
        grades = (E[]) Array.newInstance(clazz, capacity);
    }

    private E[] grades;
    private int pointer;
    private E max_grade;
    private E min_grade;
    private E average_grade;
    private  E better_than_average;
    private  E worse_than_average;
    private E excelent_count;
    private E good_count;
    private E enaught_count;

    public E getMax_grade() {
        return max_grade;
    }

    public E getMin_grade() {
        return min_grade;
    }

    public E getAverage_grade() {
        return average_grade;
    }

    public E getBetter_than_average() {
        return better_than_average;
    }

    public E getWorse_than_average() {
        return worse_than_average;
    }

    public E getExcelent_count() {
        return excelent_count;
    }

    public E getGood_count() {
        return good_count;
    }

    public E getEnough_count() {
        return enaught_count;
    }

    StudentGradesGeneric()
    {
        pointer=-1;
        grades= (E[]) new Comparable[1000];
        average_grade=null;
        better_than_average=null;
        worse_than_average=null;
        excelent_count=null;
        good_count=null;
        enaught_count=null;
        max_grade=null;
        min_grade=null;

    }

    @Override
    public int intValue() {
        return 0;
    }

    @Override
    public long longValue() {
        return 0;
    }

    @Override
    public float floatValue() {
        return 0;
    }

    @Override
    public double doubleValue() {
        return 0;
    }

    private <E> void calculate()
    {
        average_grade=null;
        better_than_average=null;
        worse_than_average=null;
        excelent_count=null;
        good_count=null;
        enaught_count=null;
        max_grade=null;
        min_grade=null;
        if (pointer<0)return;
        E sum=null;
        max_grade=grades[0];
        min_grade=grades[0];
        for (int i=0; i<=pointer;i++)
        {
            sum =grades[i] + sum;
            if ( grades[i].compareTo(max_grade) > 0)
                max_grade=grades[i];
            if(grades[i].compareTo(min_grade) < 0)
                min_grade=grades[i];
            if(grades[i].compareTo(91) >= 0)
                excelent_count++;
            else if(grades[i]>=71)
                good_count++;
            else if (grades[i]>=60)
                enaught_count++;
        }
        average_grade=sum/ (pointer+1);

        for(int i=0; i<=pointer;i++)
        {
            if(grades[i]>average_grade)
                better_than_average++;
            else
                worse_than_average++;
        }
    }

    private boolean is_valid(E n)
    {
        return (n>0 && n<100);
    }

    public boolean setter( E marks[])
    {
        for(int i=0; i<marks.length; i++)
        {
            if(!is_valid(marks[i]))
                return false;
        }
        grades=marks;
        pointer=marks.length-1;
        calculate();
        return true;
    }

    public boolean adder(E new_element)
    {
        if(!is_valid(new_element))
            return false;
        if(grades.length==pointer+1)
        {
            E[] new_size_array =(E[]) new Comparable[grades.length*2];
            for(int i=0; i<=pointer;i++)
                new_size_array[i]=grades[i];
            grades=new_size_array;
        }
        pointer++;
        grades[pointer]=new_element;
        calculate();
        return true;
    }

    public E[] getter()
    {
        E[] result = (E[]) new Comparable[pointer+1];
        for (int i=0; i<=pointer;i++)
            result[i]=grades[i];
        return result;
    }
    public void to_string(){
        System.out.println(Arrays.toString(getter()));
    }

}
cyvaqqii

cyvaqqii1#

几乎可以肯定,问题出在这个定义上:

public class StudentGradesGeneric <E extends Comparable<E>> extends Number

这定义了一个名为StudentGradesGeneric的新类,该类具有扩展Comparable<E>所需的名为E的类型参数。StudentGradesGeneric还扩展Number
最后一部分是错的:StudentGradesGeneric本身没有理由扩展Number(可以想象,有人会希望这样做,但这是不可能的,而且您的问题中没有任何内容表明您希望这样做)。
您需要的是E来扩展Number

public class StudentGradesGeneric <E extends Number & Comparable<E>>

我还没有检查您的其余代码,我想您会发现Number并不像您希望的那样有用,因为没有办法,例如,添加两个泛型Number对象并获得相同类型的对象。

相关问题