Got a test coming up and I need some help understanding arrays....Below is the code I wrote to match the question which follows underneath that...

``````class Student{
String id;//this student's ID
int midScore;//this student's midterm score
int finalScore;//this student's final score

public Student(String id){
this.id = id;
}
public void setMidScore(int midScore){
this.midScore = midScore;
}
public void setFinalScore(int finalScore){
this.finalScore = finalScore;
}
public String getID(){
return id;
}
public int getMidScore(){
return midScore;
}
public int getFinalScore(){
return finalScore;
}
public String getInfo(){
return "ID: " + id + ", midterm: "+midScore + ", final: "+finalScore;
}
}``````

``````class StudentArray{
public static void main(String[] args){
//Part1
/*
1) Create an array of Student objects for the following students:
student 1 - ID: 9901, midterm score: 80, final score: 65
student 2 - ID: 9940, midterm score : 55, final score: 89
student 3 - ID: 9938, midterm score : 72, final score: 66

2) Display information of each students (I attempted this below)
3) Change second student's final score to 90.
4) Compute average of midterm scores
*/

Student [] array= new Student [3];
array[0]= new Student("9901");
array[1]= new Student("9940");
array[2]=new Student ("9938");
array[0].setMidScore(80);
array[0].setFinalScore(65);
array[1].setMidScore(55);

//Part2
Student[] st = new Student[100];

//All 100 Student objects are created in the following
for (int i=0; i<st.length; i++){
int studentID = 1000+i;
st[i] = new Student(studentID+"");
st[i].setMidScore((int)(Math.random()*101));
st[i].setFinalScore((int)(Math.random()*101));
}

/*Compute the following
1) midterm average,
2) final average,
3) the student's ID that has the highest score in midterm + final
4) the number of students who passed (average score in midterm and final is at least 60.0
*/

}
}``````
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by moutanna

for the part 1 the code should look like;

``````//Part1
/*
1) Create an array of Student objects for the following students:
student 1 - ID: 9901, midterm score: 80, final score: 65
student 2 - ID: 9940, midterm score : 55, final score: 89
student 3 - ID: 9938, midterm score : 72, final score: 66

2) Display information of each students (I attempted this below)
3) Change second student's final score to 90.
4) Compute average of midterm scores
*/
//1) Create an array of Student objects for the following students:

Student[] array = new Student[3];
array[0] = new Student("9901");
array[0].setMidScore(80);
array[0].setFinalScore(65);
// the same thing with the other students
array[1] = new Student("9940");
array[1].setMidScore(55);
array[1].setFinalScore(89);

array[2] = new Student("9938");
array[2].setMidScore(72);
array[2].setFinalScore(66);
//2) Display information of each students (I attempted this below)

for (int i = 0; i < array.length; ++i) {
System.out.println(array[i].getInfo());
}

//3) Change second student's final score to 90.
array[1].setFinalScore(90);``````

Hope it helps

the code of the average follows:

``````//4) Compute average of midterm scores
int sum = 0;
for (int i = 0; i < array.length; ++i) {
sum = sum+array[i].getMidScore();
}
double average = sum / (array.length);``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.