Collections, Prośba o poradę.

0

Witam,

Mam problem, otóż mam do klasę ''School". Istnieją także inne klasy: "Course", "Prof", "Student" "Exam". Jako, że na kursie jest więcej studentów utworzyłam Set<Student> a także List<Exam> i <Course>. Celem zadania jest nauczenie się operowania na listach, zbiorach. Moim zadaniem jest stworzenie metody:

  • x got more than 50% average on all exams *
    public boolean getMoreThan50(Student x)

oraz

public Student getBestStudent()

Klasa Exam składa się z studenta oraz rezultatu w procentach.
Dopiero uczę się posługiwania kolekcjami i szczerze powiedziawszy nie wiem jak się za to zabrać. Może jakieś rady? Pozdrawiam

3

Student jest przypisany do kilku egzaminów (może nie bezpośrednio tylko przez kurs? w sensie student - kurs - egzamin). Niemniej metoda

public boolean getMoreThan50(Student student)

powinna zawierać pętlę - lista egzaminów danego studenta, w tej pętli dodajesz wyniki procentowe a następnie dzielisz przez liczbę egzaminów tego studenta.

Z kolei najlepszy student, to ten, który ma najwyższą średnią ze wszystkich, czyli liczysz średnią dla wszystkich studentów.

P.S.: Jak chcesz konkrety, to wstaw kod.

0

wstaw kod :)

0

Oto pierwsza metoda:

	// x got more than 50% average on all exams
	public boolean getMoreThan50(Student x) {
		double result = 0;
		double n = 0;
		for (Course a : courses) { 
			for (Exam e : a.getExams()) {
				if (e.getStudent().equals(x)) {
					result += e.getResult();
					n++;
				}
			}
			if (result / n > 50) {
				return true;

			} else {
				return false;
			}

		}

		return true;
	}
1

Dlaczego coś sumujesz?

    public boolean getMoreThan50(Student x) {
        for (Course a : courses) { 
            for (Exam e : a.getExams()) {
                if (e.getStudent().equals(x) && e.getResult() <= 50) {
                    return false;
                }
            }
        } 
        return true;
    }
0

Oto drugi kod:

public Student getBestStudent() {
		Student theBest = null;
		for (Course a : courses) {
			for (Exam e : a.getExams()) {
				if (theBest == null) {
					theBest = e.getStudent();
					
				}
				if ((a.calculateAvarage(e.getStudent()) > a.calculateAvarage(theBest))) {
					theBest = e.getStudent();

				}

			}
		}

		return theBest;
	}

calculateAvarage, to metoda którą umieściłam w klasie Course, jej celem jest policzenie średniej ucznia

public double calculateAvarage(Student x){
		double result = 0;
		double n = 0;
			for(Exam e : exams){
					if (e.getStudent().equals(x)&& e.getResult() <= 50) {
						result += e.getResult();
						n++;
						result/=n;
					}
		
			}
				return result;
}
	

Wiem jednak, ze nie jest najlepsze rozwiązanie.

1
Student theBest = null;
double maxAvg = 0.0;
for(Student student: students)
{
    double avg = calculateAverage(student);
    if(theBest == null || avg > maxAvg)
    {
         theBest = student;
         maxAvg = avg;
    }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1