+ 2

average of arrays method in java

I have two classes Grade and Course using composition I need to find the average grade in each course. that is my insert and average method where is the error in the last one? I need your help :(. //validation of number of lectures inserted public int insertGrade(Grade g) { for(int i=0; i<grades.length; i++){ if (grades[i] == null) { grades[i] = g; System.out.println("grade added! "); return 0; } System.out.println("you cant add more grades !"); return -1; } //public methods public int courseAverage (Grade g) { int sum=0; for (int i=0; i<grades.length; i++) { sum = sum + Grade[i]; } return sum / grades.length; }

24th Dec 2020, 7:11 PM
Manar Ksm
Manar Ksm - avatar
11 Respostas
+ 2
Yes. You declared it in Course class but not assigned values..! If you assigning it by grades[i] = g ; then here g is an object of grades class and in courseAverage method you are doing like sum = sum + grades[i] where grades[i] is an object and sum is Integer variable so those are incompatible for addition and assigning statement. So it gives error. So try sum = sum + grades[i].grade ; This may work I think.. If not work then better to post full code to find further solution. Manar Ksm Hope it helps.. You're welcome..
24th Dec 2020, 9:38 PM
Jayakrishna 🇮🇳
+ 2
You declared grades array in Course class and using it in Cousreavearage method so in which class you are using it? If it is not in Course class then how it is available? In that case you have to declare those as global array.. Or pass array to that class.. So pass grades array instead of Grade g object in to that method. And change sum = sum + grades[i] in Course Average method..
24th Dec 2020, 8:54 PM
Jayakrishna 🇮🇳
+ 1
What is Grade[i]? How you implemented Grade class? I think it should be grade[i] instead of Grade[i] in last method
24th Dec 2020, 7:23 PM
Jayakrishna 🇮🇳
+ 1
OK. Now with the above change, is still getting any problem?.. It depends on main class mainly now I think.. Better to show total code.. Manar Ksm
24th Dec 2020, 7:36 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 I declare the grade array in the course class by its grade class.. It is composition. Maybe you didn't get my point but am thankful for trying to help me🙏🏻
24th Dec 2020, 9:09 PM
Manar Ksm
Manar Ksm - avatar
+ 1
Jayakrishna🇮🇳 It helps.. The code works.. Thanks alot 🙏🏻
25th Dec 2020, 8:59 AM
Manar Ksm
Manar Ksm - avatar
0
@Jayakrishna🇮🇳 // MY GRADE CLASS public class Grade { private double grade; public Grade(double grade) { setGrade(grade); } public double getGrade() { return grade; } public void setGrade(double grade) { this.grade = (grade>=0 && grade <20)? grade : 10; } } //AND THAT IS HOW IT IS IMPLEMENTED IN THE COURSE CLASS public class Course extends Teacher { private String courseName; private int numberOfStudents; //composition private Grade grades[]; //constructors public Course(){ grades = new Grade[2]; } public Course(String courseName){ grades = new Grade[2]; this.courseName=courseName; }...
24th Dec 2020, 7:26 PM
Manar Ksm
Manar Ksm - avatar
0
where are the functions getting the grades variable? is the grades a global variable you havent shown?
24th Dec 2020, 8:06 PM
Logomonic Learning
Logomonic Learning - avatar
0
Logomonic Learning the functions are in the grade class above in the reply.. Do you think that there is something missing? 👀
24th Dec 2020, 8:11 PM
Manar Ksm
Manar Ksm - avatar
0
Jayakrishna🇮🇳 the code obtains 6 classes. 4 of them are good and working, just the 2 classes i added the course and Grade i have an error in it.. Because of the average method
24th Dec 2020, 8:29 PM
Manar Ksm
Manar Ksm - avatar
- 1
Hgffcjjddnmnffghk
26th Dec 2020, 9:54 AM
Prince Verma
Prince Verma - avatar