英文:
I want to make this generic
问题
现在我已经写了这段可以处理整数的代码。现在我想要使其变成泛型(扩展Number),但保持它作为一个数组。
以下是旨在实现泛型的代码,但我强烈认为完全错误,并且我可能在错误的方向上进行。我对编程相对陌生,请尽可能详细地解释。谢谢!
英文:
So I have written this code that works just fine with integers. Now I want to make it generic (extends Number), but keep it an array.
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()));
}
}
I have done this, but I strongly believe it is completely wrong and Im moving in the wrong direction. Im relatively new to programming, so please make your explanations as detailed as possible. Thamks!
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()));
}
}
答案1
得分: 2
问题几乎肯定出在这个定义上:
public class StudentGradesGeneric <E extends Comparable<E>> extends Number
这定义了一个名为 StudentGradesGeneric
的新类,其中有一个名为 E
的类型参数,要求它扩展 Comparable<E>
。StudentGradesGeneric
还扩展了 Number
。
最后一部分是错误的:StudentGradesGeneric
本身没有理由扩展 Number
(虽然有人可能需要这样做,但这不太可能,而且你的问题中没有任何迹象表明你需要这样做)。
你想要的是 E
扩展 Number
:
public class StudentGradesGeneric <E extends Number & Comparable<E>>
我没有检查你的代码的其余部分,我认为你会发现 Number
不如你希望的那样有用,因为无法像添加两个泛型 Number
对象并获得相同类型的对象。
英文:
The problem is almost certainly this definition:
public class StudentGradesGeneric <E extends Comparable<E>> extends Number
This defines a new class named StudentGradesGeneric
with a type argument named E
that is required to extend Comparable<E>
. StudentGradesGeneric
also extends Number
.
That last part is wrong: there's no reason for StudentGradesGeneric
itself to extend Number
(it's conceivable that someone would want that, but it'd be unlikely and nothing in your question indicates you do).
What you want is E
to extend Number
:
public class StudentGradesGeneric <E extends Number & Comparable<E>>
I've not checked the rest of your code and I think you'll find that Number
is not as useful a base type as you'd wish, as there's no way, for example to add two generic Number
objects and get an object of the same type back.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论