132k views
0 votes
Need a UML diagram

Design a class named Student that has the following members:

- Constructor: should accept parameters and initialize the object’s firstName and lastName fields. The value of gpa should be set to 0.
- Fields: firstName, lastName, gpa
- Accessors: include getters for all the fields
- calculateGpa: this method should accept 3 grades as parameters, calculate the average and update gpa value.
The main method prompts the user to enter the student’s first and last name. Create an object of class Student and initialize its fields with the values accepted from the user. Next, prompt the user to enter 3 grades for the student. Once all the grades are entered and validated, call calculateGpa method of Student object and display its gpa. Present clear formatted code with comments

User Joanmacat
by
7.7k points

2 Answers

1 vote

Sure, here is the UML diagram for the Student class:

```

Class Diagram

Class: Student

Attributes:

firstName: String

lastName: String

gpa: double

Methods:

constructor(firstName: String, lastName: String)

getfirstName(): String

getlastName(): String

getgpa(): double

calculateGpa(grade1: double, grade2: double, grade3: double): void

```

Here is the code for the Student class:

```python

class Student:

def __init__(self, firstName, lastName):

self.firstName = firstName

self.lastName = lastName

self.gpa = 0.0

def getfirstName(self):

return self.firstName

def getlastName(self):

return self.lastName

def getgpa(self):

return self.gpa

def calculateGpa(self, grade1, grade2, grade3):

self.gpa = (grade1 + grade2 + grade3) / 3

def main():

firstName = input("Enter student's first name: ")

lastName = input("Enter student's last name: ")

student = Student(firstName, lastName)

grade1 = float(input("Enter student's first grade: "))

grade2 = float(input("Enter student's second grade: "))

grade3 = float(input("Enter student's third grade: "))

student.calculateGpa(grade1, grade2, grade3)

print("Student's GPA is: ", student.gpa)

if __name__ == "__main__":

main()

```

This code prompts the user to enter the student's first and last name, then creates an object of the Student class and initializes its fields with the values accepted from the user. Next, the user is prompted to enter 3 grades for the student. Once all the grades are entered and validated, the calculateGpa method of the Student object is called and its gpa is displayed.

I hope this helps! Let me know if you have any other questions.

User Ill
by
7.7k points
5 votes

Here's the UML class diagram for the `Student` class you described:

```

------------------------------------------

| Student |

------------------------------------------

| - firstName: String |

| - lastName: String |

| - gpa: double |

------------------------------------------

| + Student(firstName: String, |

| lastName: String) |

| + getFirstName(): String |

| + getLastName(): String |

| + getGpa(): double |

| + calculateGpa(grade1: double, |

| grade2: double, |

| grade3: double): void |

------------------------------------------

```

Explanation of UML class diagram:

- The `Student` class has three private fields: `firstName` of type `String`, `lastName` of type `String`, and `gpa` of type `double`.

- The `Student` class has a public constructor that takes `firstName` and `lastName` as parameters and initializes the `firstName` and `lastName` fields. The `gpa` field is set to 0.

- The `Student` class has public getter methods (`getFirstName()`, `getLastName()`, and `getGpa()`) to access the values of the fields.

- The `Student` class has a public method `calculateGpa()` that accepts three grade parameters (`grade1`, `grade2`, `grade3`). This method calculates the average of the grades and updates the `gpa` field with the calculated value. The method does not return any value (void).

User Nyte
by
8.2k points