1

I am trying to make a constructor that creates an array of empty homework scores (6 per student) and an array of empty exam scores (3 per student).

The getHomeworkAverage() method returns the average score on the student’s homework. The getFinalScore() method returns the final grade for each student using the grading as follows: (15% for exam1, 25% for exam2, 30% for exam3, and 30% for the homework average).

UML Diagram Here

public class Student {
    private String name;
    private int[] homeworks;
    private int[] exams;
    
    private int[] homeworkScores;
    private int[] examScores;
    
    private double homeworkAvergae;
    private int finalScore;

    public Student(String name, int[] homeworks, int[] exams, int[] homeworkScores, int[] examScores, double homeworkAvergae, int finalScore) {
        this.name = name;
        this.homeworks = homeworks;
        this.exams = exams;
        this.homeworkScores = homeworkScores;
        this.examScores = examScores;
        this.homeworkAvergae = homeworkAvergae;
        this.finalScore = finalScore;
    }

    public int[] getHomeworkScores() {
        return homeworkScores;
    }

    public void setHomeworkScores(int[] homeworkScores) {
        this.homeworkScores = homeworkScores;
    }
    
    public double getHomeworkAvergae() {
        return homeworkAvergae;
    }
    
    public int[] getExamScores() {
        return examScores;
    }

    public void setExamScores(int[] examScores) {
        this.examScores = examScores;
    }
    
    public int getFinalScore() {
        return finalScore;
    }
    
    public String getName() {
        return name;
    }

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

    public int[] getHomeworks() {
        return homeworks;
    }

    public void setHomeworks(int[] homeworks) {
        this.homeworks = homeworks;
    }

    public int[] getExams() {
        return exams;
    }

    public void setExams(int[] exams) {
        this.exams = exams;
    }
}
Busayo
  • 21
  • 2
  • 1
    and what is your actual question? How is any of this uml related? – Stultuske Nov 16 '22 at 14:39
  • Welcome to SO - Currently, your question looks like you want the community to do homework. Please show us what you have tried, and give us an example of how it did not work out. Hope this helps you to get an answer asap :) – Reg Nov 16 '22 at 14:49
  • Hi, there! Yes, my question is what am I missing and am I doing the right thing? I've attached the UML diagram to aid in understanding what I'm trying to achieve. – Busayo Nov 16 '22 at 14:51

2 Answers2

1

Is there a constructor in the diagram?

If taken by the book, your UML class does not contain any constructor. A constructor in UML would be an operation with the stereotype «Create» and returning a Student:

+ «Create» Student(name:String):Student

Of course, several mainstream OOP languages use the convention of a constructor named like the class and with no return type specified. This is why many people would understand Student(name:String) as being the constructor, despite the UML inconsistency.

What is the constructor signature in the diagram?

Assuming that Student(name:String), you would need to implement this class as foreseen in the design:

public class Student {
    ...
    public Student(String name) {
    ...
    }
}

Other remarks

If you have difficulties with initialising arrays, you may find plenty of answers on SO. My first guess would be something like:

homeworks = new int[6];
...

I will not review the code nor complete missing parts. The only thing that I'd like to highlight is that the getters for array fields return the reference to the object's array, which allow to update the content of the array without control. Similarly, setting the array, reuses another array and does not overwrite the current array content. Look here on SO how to clone arrays for avoiding these risks

Christophe
  • 68,716
  • 7
  • 72
  • 138
-1

Your question is how to structure the constructor ?

    int[] homeworks = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
    int[] exams = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
    int[] homeworkScores = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
    int[] examScores = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
    Student student = new Student(
            "Name",
            homeworks,
            exams,
            homeworkScores,
            examScores,
            6.0,
            6
    );
  • Out of curiosity: have you tried to construct two students like that, and then change one of the score in one of the student and look at the same score in the other student? – Christophe Nov 16 '22 at 23:07