Click here to Skip to main content
15,741,818 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I have this Course.java

Java
import java.time.LocalDate;
import java.util.ArrayList;


public class Course {

    

private String name;
private LocalDate startDate;
private LocalDate endDate;
private ArrayList<Students> studentList;



public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public LocalDate getStartDate() {
    return startDate;
}
public void setStartDate(LocalDate startDate) {
    this.startDate = startDate;
}
public LocalDate getEndDate() {
    return endDate;
}
public void setEndDate(LocalDate endDate) {
    this.endDate = endDate;
}




public ArrayList<Students> getStudentList() {
    return studentList;
}
public void setStudentList(ArrayList<Students> studentList) {
    this.studentList = studentList;
}


public Course(String name, LocalDate startDate, LocalDate endDate) {
    this.studentList = new ArrayList<Students>();
    this.name = name;
    this.startDate = startDate;
    this.endDate = endDate;

    
}

public boolean addStudent(Students student){
    if (student==null || studentList.contains(student)) {
        return false;
    }
    studentList.add(student);
    return true;
}





public Course(){
    
}


@Override
public String toString() {
    return "Course [name=" + name + ", startDate=" + startDate + ", endDate=" + endDate + ", studentList="
            + studentList + "]";
}


And I have Students.java

Java
public class Students {
    private int studentId;
    private String name;
    private Integer age;
    private String gender;
    private Integer score;
    
    

    
    
    public int getStudentId() {
        return studentId;
    }



    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }



    public String getName() {
        return name;
    }



    public void setName(String name) {
        this.name = name;
    }



    public Integer getAge() {
        return age;
    }



    public void setAge(Integer age) {
        this.age = age;
    }

    

    public String getGender() {
        return gender;
    }



    public void setGender(String gender) {
        this.gender = gender;
    }



    public Integer getScore() {
        return score;
    }



    public void setScore(Integer score) {
        this.score = score;
    }






    public Students(int studentId, String name, Integer age, String gender, Integer score) {
        super();
        this.studentId = studentId;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.score = score;
    }



    public Students(){

    }
    
    @Override
    public String toString() {
        return "studentId=" + studentId + ", "
                + "name=" + name + ", age="
                + age + ", gender=" + gender + ", score=" + score + "]";
    }
    
    
}



Also the main class

Java
Course Math = new Course("Math",LocalDate.of(2021,Month.JULY,29),LocalDate.of(2023,Month.APRIL,29));
  Math.addStudent(new Students(1, "Michael Jordan", 36,"male",8));
  Math.addStudent(new Students(2, "Peppi Amiran", 26,"male",6));
  Math.addStudent(new Students(3, "Phinehas Waldo", 17,"male",10));
  Math.addStudent(new Students(4, "Catrinel Floriana", 32,"female",8));

  Course Java = new Course("Java",LocalDate.of(2021,Month.JUNE,12),LocalDate.of(2022,Month.SEPTEMBER,15));
  Java.addStudent(new Students(5,"Cornelia Oana",20,"female",7));
  Java.addStudent(new Students(6,"Ovidiu Laura",20,"female",10));
  Java.addStudent(new Students(7,"Rozalia Augustin",20,"Male",10));
  Java.addStudent(new Students(8,"Iulian Apostol",20,"female",10));
  Java.addStudent(new Students(9,"Timotei Emanuel",20,"Male",10));

  Course Python = new Course("Python",LocalDate.of(2021,Month.JUNE,12),LocalDate.of(2022,Month.DECEMBER,29));
  Python.addStudent(new Students(10,"Emanuela Laura",20,"female",7));
  Python.addStudent(new Students(11,"George Iacob",20,"male",7));
  Python.addStudent(new Students(12,"Filimon Nelu",20,"male",7));
  Python.addStudent(new Students(13,"Ioan Ilie",20,"male",7));


  List <Course> courseList = new ArrayList<Course>();
  courseList.add(Math);
  courseList.add(Java);
  courseList.add(Python);


A student should have their basic details along with course details: course name, start and end dates, which it does.

Now, I need to print the average score of all students for a given course name, and I don't know how can I do it. (Bonus for using streams)

What I have tried:

I've tried everything that crossed my mind.
Posted
Updated 10-Jan-22 5:47am
Comments
Richard MacCutchan 10-Jan-22 11:13am    
"I've tried everything that crossed my mind."
Given that we have no idea what those things might be, or what results they produced, it is difficult to offer suggestions for improvement.

Just loop through all the students on that course, count them as you go, and add up their scores.
Afterwards, drive the total by the count, and you have the average.
 
Share this answer
 
Comments
adrian adi 2022 10-Jan-22 11:20am    
I need to use streams to get it. And I don't know how.
OriginalGriff 10-Jan-22 11:24am    
Then start by reading the notes you made on the last couple of lectures. They should have covered the material, or the recommended reading you were given to accompany the course will have.

Since your current code doesn't show any use of files or streams, it might also help if you carefully re-read your homework assignment too.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900