0
import java.util.Scanner;

class DataInput {
String name[];
int korean[], math[], english[];
int sum[];
double average[];
static int rank[];
int students;

public void save() {
    Scanner sc = new Scanner(System.in);

    System.out.println("Please input number of students");
    students = sc.nextInt();

    name = new String[students];
    korean = new int[students];
    math = new int[students];
    english = new int[students];
    sum = new int[students];
    average = new double[students];
    rank = new int[students];

    for (int i = 0; i < students; i++) {
        System.out.println("Name?");
        name[i] = sc.next();
        System.out.println("Korean Score :");
        korean[i] = sc.nextInt();
        System.out.println("Math Score :");
        math[i] = sc.nextInt();
        System.out.println("English Score :");
        english[i] = sc.nextInt();

        sum[i] = korean[i] + math[i] + english[i];
        average[i] = sum[i] / 3;
        }
  }
}

class DataOutput extends DataInput {

public void ranker() {
    for (int i = 0; i < students; i++){
        rank[i] = 1;
    }

    for (int i = 0; i < students; i++) {
        for (int j = i + 1; j < students; j++) {
            if (sum[i] < sum[j]) {
                rank[i] += 1;
            } else if(sum[i]>sum[j]){
                rank[j] += 1;
            }
        }
    }

}

}

public class Score {
public static void main(String[] args) {
    DataInput data = new DataInput();
    DataOutput out = new DataOutput();
    data.save();
    out.ranker();

    System.out.println();
    System.out.println("Name\t\tKorean  Math  English\tSum  Average  Rank");
    System.out
            .println("-------------------------------------------------------");

    for (int i = 0; i < data.students; i++) {
        System.out
                .println(data.name[i] + "\t\t" + data.korean[i] + "  "
                        + data.math[i] + "  " + data.english[i] + "\t"
                        + data.sum[i] + "  " + data.average[i] + "  "
                        + out.rank[i]);
    }
}

}

So, this is a little program I built. I think the logic is quite right, but when I run the program and type in all the variables, the ranks of students are all 0.

I can't figure out what's the problem. It would be really thankful if someone helps me.

Muon
  • 71
  • 4
  • well the object out has no means to get the values from the object data. So its zero. Put the ranker() function inside the DataInput class and call it. Or find ways to initialize the values in DataInput to DataOutput. – ray Dec 10 '13 at 04:58
  • I'm surprised this isn't throwing index out of bounds or null reference exceptions. `out.rank` has not been initialized at all. – recursive Dec 10 '13 at 05:05

4 Answers4

3

Instead of average[i] = sum[i] / 3; - which does integer math; I think you really need average[i] = sum[i] / 3.0; which should give you a non-zero result.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

When you call instantiate a new instance of Data output

    DataOutput out = new DataOutput();

it creates all new data. setting the number of students to zero.

Not Recommended

you should either pass in the number of students EDIT and other data

   DataOutput out = new DataOutput(students, otherData);

   //In Class Constructor

   DataOutput(int student, int ... otherData)
   { 
       this.student = student;
       for(int i : otherData)
         //code
   }

Recommended

or you should put the ranker method inside of the dataInput class

   class DataInput
   {
         //code
        public int ranker()
        { 
           //code
        }
   }
Tyler Davis
  • 2,420
  • 2
  • 23
  • 19
0

The data varible and out variable are "not linked". You call ranker on an "empty" class. Just because DataOutput extends DataInput doesn't mean those two instances are linked/related in anyway.

In the context of the code given: Move the ranker method into the DataInput class and just omit the DataOutput class. Then call ranker() on your data variable.

John3136
  • 28,809
  • 4
  • 51
  • 69
0

Use the same derived class object to hold the data on only one object. You are creating two objects. With DataInput object, data is stored. When you create object for DataOutput, it is fresh object with all default values '0' in all arrays. So use the derived class object for both data storage and output.

out.save();
out.ranker();

System.out.println();
System.out.println("Name\t\tKorean  Math  English\tSum  Average  Rank");
System.out
        .println("-------------------------------------------------------");

for (int i = 0; i < out.students; i++) {
    System.out
            .println(out.name[i] + "\t\t" + out.korean[i] + "  "
                    + out.math[i] + "  " + out.english[i] + "\t"
                    + out.sum[i] + "  " + out.average[i] + "  "
                    + out.rank[i]);
}
java geek
  • 1
  • 4