Home > Enterprise >  How to convert an UML class with a constructor to a java object?
How to convert an UML class with a constructor to a java object?

Time:11-17

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). Empty means all 0s in this context.

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).

Then I need to use Math.ceil() method to round up to the nearest integer. The getters/setters for the three variables should work as normal.

Here the class diagram: enter image description here

Here the code that I produced so far:

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;
    }
}

My question is what am I missing and am I doing the right thing?

CodePudding user response:

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
    );

CodePudding user response:

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

  • Related