Reference no: EM133087586
Lab - Objects, Class Diagrams and UML
Introduction
We had a brief look at Objects in week 6 - this week we revisit objects together with a look at a Computer-aided software engineering (CASE) tool - Enterprise Architect (EA).
Consider the following Python code that we looked at in week 6:
class Student():
def init (self, first_name, last_name, student_id): self.first_name = first_name
self.last_name = last_name self.student_id = student_id
def display(self):
print("{0}, {1} ({2})").format(last_name, first_name,
student_id))
We could draw a class diagram by hand - which is fine - if it is only a small class or two; if the code involves many more classes some help is greatly appreciated. Let us take a quick look at EA; on Startup, The screen should look something like this:
Cancel the Startup Dialog and close the Start Page. From the Menu choose File -> New Project and create a new project called Week10.
Use the code in week 6 for the Car and ElectricCar (the first version is fine). It should look something like this. (In this case a main class has also been and imported together with Car.py and ElectricCar.py)
Note that ElectricCar has inherited from Car. You can click on the instantiation of an electric car and view its attributes.
Enterprise Architect is an industrial strength CASE tool which lets us do many things.
Just one more thing. Consider the following code:
"""
File: Student.py
Resources to manage a student's name and test scores. """
class Student(object): """Represents a student."""
def init (self, name, number):
"""Constructor creates a Student with the given name and number of scores and sets all scores to 0."""
self.name = name self.scores = []
for count in range(number): self.scores.append(0)
def getName(self):
"""Returns the student's name.""" return self.name
def setScore(self, i, score):
"""Resets the ith score, counting from 1.""" self.scores[i - 1] = score
def getScore(self, i):
"""Returns the ith score, counting from 1.""" return self.scores[i - 1]
def getAverage(self):
"""Returns the average score."""
return sum(self.scores) / len(self.scores) def getHighScore(self):
"""Returns the highest score.""" return max(self.scores)
def str (self):
"""Returns the string representation of the
student."""
return "Name: " + self.name + "\nScores: " + \ " ".join(map(str, self.scores))
Notice the triple docs (""" """) at 3 different levels. The first level is that of the module.
The second level is just after the class header. There might be more than one class defined in a module, so each class can have a docstring to describe its purpose.
The third level is located after each method header.
Attachment:- Class Diagrams and UML.rar