简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM