I'm trying to create a class that prints out Student
details and I wish to know how to take an int
array
and calculate the average of all their marks.
Here's what I have so far:
public class Student {
private int id;
private String name;
private String course;
private int[] marks;
Student (int id, String name, String course, int [] marks) {
// constructor which creates a student according to the specified parameters
this.id = id;
this.name = name;
this.course = course;
this.marks = marks;
}
int average () {
// calculates and returns the average mark for the student
return marks / 5; // error: the operator / is undefined for the argument type int[]
}
void print () {
// prints student details
System.out.println("Student ID: "+id+"\n");
System.out.println("Student Name: "+name+"\n");
System.out.println("Course enrolled on: "+course+"\n");
System.out.println("Student mark: "+marks+"\n"); // This prints a hashcode for some reason
}
}
My question, specifically, is how do I return the average of the int[]
marks in the "int average()" method (without changing the header or parameters in the parenthesis)?
public class T3Main {
public static void main(String[] args) {
Student s1 = new Student(1234, "Joe Bloggs", "Computer Studies", new int[] {67, 55, 78, 72, 50});
Student s2 = new Student(2341, "Sue White", "Computer Science", new int[] {57, 85, 58, 49, 61});
Student s3 = new Student(3412, "Ben Black", "Software Engineering", new int[] {71, 45, 66, 70, 51});
s1.print();
s2.print();
s3.print();
}
}
You can not use operator with array, thus marks / 5
is incorrect. You need to do sum of all array items and store it in separate variable and then divide it with 5
(length of array). You may need to be careful about empty or null array.
double sum = 0d;
for(int item : marks) {
sum += item;
}
return sum / marks.length;
Moreover following statement print string representation of array and not it's content.
System.out.println("Student mark: "+marks+"\n");
You can use following way to print array,
System.out.println("Student mark: "+ Arrays.toString(marks) +"\n");
With Java 8:
Arrays.stream(marks).average().getAsDouble();
And you can convert to int (rounded down) with (int)
(int)Arrays.stream(marks).average().getAsDouble();
import java.util.stream.*;
It's in the package java.util.stream
Example:
int[] marks = {10,20,30,40,50};
int sum = IntStream.of(marks).sum();
System.out.println("The sum is " + sum);
Since int[]
is an object and contains an array with your marks, the /
operator is not defined for this type. You will have to divide the total sum of the marks to their number to find out the average:
double average () {
double markSum = 0;
double average;
int i;
for (i = 0; i < marks.length; i++) {
markSum = markSum + marks[i];
}
average = markSum / marks.length;
return average;
}
Also, please note that usually the correct data type to use here is double
, because int
might get you incorrect (rounded) results: in case marks = [1, 2, 3, 4]
the average is 2.5
, but by using int average()
you will get 2
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.