-2

Hi I know you can easily find the error here, and i need help to fix this out I'm very tired to find what's wrong with my code,I'm trying to fix it out but it becomes complicate when im trying to change something

here's my currently code:

 package copro;

public class QuizInherit {
    public static void main(String[] args){

        students persons[] = new students[3];
        persons[0] = new students("Vincent","Dimalanta",90,85,80);
        persons[1] = new students("Daryl", "De Guzman", 95, 92, 91);
        persons[2] = new students("Jayson", "Elizaga", 88, 87, 86);

        for (students person : persons) {
            person.print();
        }
    }

    public class students extends Calculations {

        private String firstname;
        private String lastname;

        public students(String firstname, String lastname, int Grade1,
                int Grade2, int Grade3) {
            super(Grade1, Grade2, Grade3);
            this.firstname = firstname;
            this.lastname = lastname;
        }

        public void print() {
            System.out.println("FirstName: " + firstname);
            this.print();
            System.out.println("LastName: " + lastname);
            this.print();
            this.getAve();
        }
    }

    public class Calculations {

        private int Grade1;
        private int Grade2;
        private int Grade3;
        private double ave;

        public Calculations(int Grade1, int Grade2, int Grade3) {
            this.Grade1 = Grade1;
            this.Grade2 = Grade2;
            this.Grade3 = Grade3;
            this.ave = ave;
        }

        public void getAve() {
            ave = (Grade1 + Grade2 + Grade3) / 3;
        }

        public void print() {
            System.out.println("Student Details: ");
            this.print();
            System.out.println("Grade1: " + Grade1);
            System.out.println("Grade2: " + Grade2);
            System.out.println("Grade3: " + Grade3);
            System.out.println("The average is: " + ave);
        }

    }
}

The error is here:

persons[0] = new students("Vincent","Dimalanta",90,85,80);

I hope you will help me, I know this is very easy question but Its hard for me because I'm currently beginner to the Programming World

Thank you

  • 2
    The best way to find errors is to read the error messages. By not posting these exact and complete error messages, you're making it much harder for us. That said, follow these simple rules for now: one class per Java file. Do not define classes inside other classes. You'll learn about inner classes later. – JB Nizet Feb 08 '15 at 17:49
  • 2
    For one, you are recursively calling the print() method. There's a stack overflow right there. – TEK Feb 08 '15 at 17:49
  • this.ave = ave; is pointless since ave is zero. A method name like "getAve" should be used for something returning a field. The average could be computed in the constructor. – laune Feb 08 '15 at 18:11

1 Answers1

0
for (students person : persons) {
        person.print();
    }

The aforementioned code calls,

`public void print() {
     System.out.println("FirstName: " + firstname);
     this.print();
     System.out.println("LastName: " + lastname);
     this.print();
     this.getAve();
 }`

which recursively calls print() through this.print(). This will likely create stackoverflow

Ankur Piyush
  • 308
  • 1
  • 9