Write a class called Course that represents a course taken at a school. Use Arra
ID: 3724538 • Letter: W
Question
Write a class called Course that represents a course taken at a school. Use ArrayList in the course to store the students taking that course. The constructor of the course class should accept only the name of the course. Provide a method called addStudent that accepts one student parameters. Provide a method called average that computes and return the averages of all students test score averages. Provide a method called roll that prints all students in the course, adds several students,prints a roll and print the overall course test average Write a class called Course that represents a course taken at a school. Use ArrayList in the course to store the students taking that course. The constructor of the course class should accept only the name of the course. Provide a method called addStudent that accepts one student parameters. Provide a method called average that computes and return the averages of all students test score averages. Provide a method called roll that prints all students in the course, adds several students,prints a roll and print the overall course test averageExplanation / Answer
Hi.. I have written java program for the above.
Course.java
import java.util.ArrayList;
import java.util.List;
public class Course {
private String name;
private List<Student> total = new ArrayList<Student>();
public Course(String name) {
this.name = name;
}
public void addStudent(Student s){
total.add(s);
}
public void avergae(){
System.out.println("In average method");
int i=0;
int count=0;
for(i=0;i<total.size();i++){
count+=total.get(i).getScore();
}
double avg = count/i;
System.out.println("Average: "+avg);
}
public void roll(){
System.out.println("In print method");
System.out.println("Course Name:"+name);
for(Student s:total){
System.out.println(s.toString());
}
}
}
Student.java
import java.util.Arrays;
public class Student {
private String studentId;
private String Name;
private int score;
public Student(String studentId, String name, int score) {
super();
this.studentId = studentId;
Name = name;
this.score = score;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
@Override
public String toString() {
return "studentId=" + studentId + ", Name=" + Name + ", score=" + score + "";
}
}
TestDriver.java
public class TestDriver {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s = new Student("1","Abc",78);
Student s1 = new Student("2","BBc",89);
Student s3 = new Student("3","Norm",59);
Course c1 = new Course("Maths");
c1.addStudent(s);
c1.addStudent(s1);
c1.avergae();
c1.roll();
c1.addStudent(s3);
c1.avergae();
c1.roll();
}
}
Output:
In average method
Average: 83.0
In print method
Course Name:Maths
studentId=1, Name=Abc, score=78
studentId=2, Name=BBc, score=89
In average method
Average: 75.0
In print method
Course Name:Maths
studentId=1, Name=Abc, score=78
studentId=2, Name=BBc, score=89
studentId=3, Name=Norm, score=59
Please test the code and let me know any issues. Thank you. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.