Sei sulla pagina 1di 22

Cover Page 

Saturday, February 29, 2020 9:11 PM

Mapúa University 
School of Electrical, Electronics and Computer  
Engineering  

Experiment 2: Programming Paradigms/ Object Oriented Design  


CPE106L (Software Design Laboratory) 

Alcaraz, Flynn Matthew  


Garcia, Loven Palencia 
Velasco, Erikka Louisse 
Pre-Laboratory 
Saturday, February 29, 2020 9:11 PM

Pre-Laboratory 
Readings, Insights, and Reflection  Python: 
Fundamentals of Python Programming by Kenneth Lambert
(Chapter 9) 
Core Python Programming by Negeswara Rao (Chapter 5) 
 
Git: 
Professional Git by Brent Laster Connected Lab 2 
 
UML: 
 
UML 2 Toolkit by Hans-Erik Eriksson (Chapter 5) 
Dennis (9781119030263): Chap 5 

Answers to questions  Lambert Exercises 1 to 3, page 346 

 
Fundamentals of Python Programming by Kenneth Lambert Chapter 9 Insights
and Reflection 
 
 
Insights 
In the book Fundamentals of Python First Programs in chapter 9. We see how programmers use
objects and classes and how they are initiated. We will also use OOP or object oriented programming
as we will use pre written classes and at the same time we will make classes.  
 
It is also discuss in the book the use of the two dimensional grid which includes basic types of data
structures from pythons like strings, lists, and tuples.  
 
Also, it is shown how to implement and design your own abstraction mechanism, we will be using a
style of programming called “Object Oriented Programming” since we will be the one to create new
classes of objects, unlike in Object-Based Programming, we are only using codes that are ready
made.  
 
Next, docstrings can occur at three different levels. The first level is located at the module, Second
level is located after the class header and lastly, the third level is located after each method header.  
  
Lastly, the book discusses structuring classes with Inheritance and Polymorphism. With the use of
data encapsulation which manipulates the object's state to a set of method calls,  inheritance which
allows class to be recuse to extend the code of the class, and lastly polymorphism which allows
multiple classes to use with the regard of using the same general method name.  
 
 
Reflections 
 
In this lesson I have learned that you can use simple class definitions to define a certain element.
Which can be separated and defined from each other. An object or class may have its own memory
of data space. It also allows the user of the class to provide initial values for the variables. The
method discussed that it contains a header and a body that makes a parameter or boundary on a
particular object. Some standard can be overloaded and use with the new classes in the program.
Once the program no longer references an object the program is already dead or unfunctional. 
 
Moreover, the parameter self is always included in each method definition. This is used to refer to
the objects by name. You can directly call the specific element from the list by including the
parameter self. 
 
 
 
Kenneth Lambert's Exercises 
 
Exercise 1 Page 346 
 
What are the benefits of having class B extend or inherit from class A? 
To minimize the duplicated or multiple identical common code in several classes. It can count for not
rewriting codes as it can be reusable. Inheritance can also make application code more flexible to
change because classes that inherit from a common class can be used interchangeably. If the return
type of a method is class. 
 
 
Exercise 2 Page 346 
 
Describe what the __init__ method should do in a class that extends another class. 
The method used for this is called Inheritance hierarchies. It provides an abstraction mechanism that
reduces the programmer to avoid writing redundant codes by simply reusing it. Moreover, the
subclass extends from its parents class by modifying the same methods.  
 
Exercise 3 Page 346 
 
Class B extends class A. Class B defines an __str__ method that returns the string
representation of its instance variables. Class B defines a single instance variable named age,
which is an integer, Write the code to define the __str__ method for class B. This method
should return the combines string information from both classes. Label the data for age with
the string "Age: " 
 
 

 
 
 
 
 
Core Python Programming by Negeswara Rao Chapter 5 Insights and Reflection 
 
Insights 
 
Discussed on the first part of chapter 5 are the output statements. This part heavily focuses on the use
of print statements. Print statements is simply outputs any value that a user chooses. It is the
counterpart of cout() in C++ and console.log() in Javascript. Print statements does not only output
simple strings but also all kinds of data types like integer and booleans. Furthermore, print
statements can also print objects like lists, dictionaries and tuples.  

Next are input statements, from what I already know, input functions are used to get values from a
user and then store it to a certain variable. As default, inputs from users are in type string unless
specified by the user. For example, my input is 89, by default, 89 will be stored as a string('89')
unless I put an int function to cover the input statement. Aside from integers and string, input
function also accepts list from the user.  
 
From the readings, an eval() function is introduced. An eval() function takes a string and evaluates
the result of the string by taking it as a python expression. Eval function can be very helpful since it
allows user to input a whole expression. 

Lastly are command line arguments. Command line arguments are the values passed to the program
from outside the program. These are values that user enters or inputs. For example, my program
requires the user to input a certain number in order for that number to be processed to whatever I
want to do. The command line arguments in the example are the numbers the I want the user to
input. These arguments are stored in in the form of strings in a list with the name 'argv'. In order to
know how many arguments there are, len(argv) function is used, len simply stands for length.  
 
 
Reflections 
 
Based from the readings in chapter 5, I can say that it is good to refresh the mind on the simple
topics like the print statement and input statement. It is vital for a programmer to always re-learn and
refresh the basics of programming.  From there, I want to point out 2 things that I've learned outside
from what I already know. First is the eval() function. Eval function allows the user to input an
expression and solve it as a regular 1.  Second is the command line arguments. The command line
argument simply tells us that the input form the users are in default stored in sys module as a string
waiting to be processed depending on what other functions are given.  It is good to know that we
know how the language work from the inside, it is vital since we want to be able to utilize its full
capacity in order to make better programs.  
 
 
 
 
Professional Git by Brent Laster Connected Lab 2 Insights and Reflections 
 
Insights 
 
For objective of Connected Lab 2 is for us to be able to create our own Git repository on our own
local disk. The first step is to create a directory on which we will put all the files the is going to be
uploaded in GitHub. In my case, I want to put my repository in git_workplace directory.  
Now in we need to configure the settings and tell git our name and email address. Since I already
have one, I'll just show my config file. 
 

 
If we want to see what files in our local drive are not yet uploaded in our GitHub account, the
command is git status. 
 
From there we can see that file.txt is untracked meaning that it is not yet in my GitHub account. 
 
 
Now if I want to add file.txt to my repository in github,  the command is git add <file>. 
 

 
 
From there we can see that file.txt is now ready to be committed. To commit in git, the syntax is git
commit -m "comment string".  
 

 
 
Now if we want to edit a file to our current file the syntax is echo more >> <filename>. From the
example below, we can see that I added  more text in file.txt using the syntax given above. 
 

 
From there I just used the commit command to update what I edited in file.text. To skip the add
command we can just add a to the commit command.  
 

 
 
 
Reflections 
 
Comparing the things that I've learned and the things that I already knew, I can say that this book
really helped me to understand and be familiarized by the basic command in making my own local
repository in git. The readings also helped on understanding how I can add and commit files from
my  local repository directly to my GitHub account. Furthermore, I've also learned how I can easily
modify or change the file that is in my repository using echo more. Overall, I found the book to be
very substantial to any beginner. 
 
 
 
 
UML 2 Toolkit by Hans-Erik Eriksson Chapter 4 Insights and Reflections 
 
Insights 
 
UML is flexible to use since it supports static, dynamic and functional modeling. With this, you can
also create Class Diagrams which consists of classes and relationships. The following relationships
are namely: Association, Generalization, Dependencies and Abstractions.  
The first part of the chapter showed us the difference between Classes and Objects and what is their
relationship to each other. Object was described as the representation of something that exists in the
real world, while Classes is used to classify the objects, this is the representation of elements with
common characteristics. Both of these are used to describe the elements in the system. With the use
of an UML (Unified Modeling Language), creating a system that can be more efficient since it gives
a course of action for the designer to show how to build and refine systems. Moreover, whether it
may be a business or a machine type of system, as long as it is an object-oriented model, using UML
can still be reliable and will still be easy for the models to understand the system.  
 
 
Reflections 
 
Based from the readings, I have learned the different types of relationships that can be used in class
diagrams. Class Diagram is used for modeling the system of application and for translating the
model into a programming code. Furthermore, Class is often divided into three compartments
namely: The Name Compartment – which contains the name of the class, and should be capitalized
and centered in Bold face. Next, The Attributes Compartment – which defines the features of an
object, an example of this is weight, color and length. Moreover, attributes can have different
visibility, which can be chosen between private and public. Lastly, The Operations Component, this
manipulates the attributes and operations.  
 
 
 
Sytems Analysis and Design: An Object-Oriented Approach with UML by Allan
Dennis Chapter 5:  Structural Modeling 
 
Insights 
 
In Chapter 5, it elaborates the use of CRC or class-responsibility-collaboration) which are presented
as class diagrams and objects diagrams that are used for making structural models. Structural models
who's the possible outcomes of the proposed projects. 
 
 
Reflections 
 
 
Structural modelling is very useful for making and visualizing how you want your project's outcome
to be finished. It shows your step by step process in creating your product from the start till the end.
CRC Cards also help as your documentation to keep track of your progress. 

We learn also that there is 3  basic categories of data abstraction first generalization relationships, 
second aggregation relationships, and third association relationships.  

First is generalization relationship this data abstraction helps to create classes that inherits attributes
and operations. 

Second is aggregation relationship this data abstraction helps in to relate parts or to relate different
classes and there relate their relationship. 

Lastly is association relationship which helps to see  classes that don’t fit neatly or a weak form of
aggregation. 
 
 
 
 
 
 
 
 
 
 

 
 
 
 
In-Laboratory 
Saturday, February 29, 2020 9:21 PM

In-Laboratory 
Note: Leaders should assign the problems to members 
• Objectives (you can have your own objectives) 
• Steps Performed with screenshots of tools used (UMLet and VSCode), debugging, sample run
with DISCUSSION (Don’t copy and paste from ebook). Use the source of Lambert. Given the
source code (with classes), create the corresponding class diagram using UMLET  
• IMPORTANT: Figure numbers and labels.  

Objectives 

The objectives of the In-Laboratory are as follows: 


1. To show the basic structure of classes including it’s the instance variable and methods. 
i. The __str__() method vs __init__() method 
ii. Discuss accessors and mutators 
 
2. To discuss Unified Modeling Language (UML) and the different relationships among
classes 
i. Inheritance/Generalization 
ii. Association 
iii. Aggregation 
iv. Composition 
 
 
First Objective: The basic structure of classes 
 
 
For the first objective, the goal is to show the basic structure of classes including its methods
and constructors.  
 
To understand classes, we need to first know that each object at data is an instance of a class.
For example, if we print the type of integer x, the output is  <class int>. This mean that every
object has a certain class. 

Figure 1.1: The class type of integer x 


 
 
Basically, class is just a blueprint on how we will make an object. For example, if we want to
make a student class, we must consider attributes that every students have like name, age, and
grade. 
 
In order to define a class, the syntax is class <ClassName>: 
 

Figure 1.2: Syntax for creating a class 

 
 
The next thing that we need to learn are the instance attributes. Since we are delaing with
objects, we must understand that objects has different characteristics just like name, age, and
student numberes of students.  
 
By using an __init__ () method, we can initialize these attributes or characteristics. The init
method is also called a constructor which gets invoked when an object is instantiated. The
example below shows the initialization of the name, number, and score of class Student: 
 

Figure 1.3: The init method 

 
Next are the methods. Methods are just similar to functions except they are inside a class and
are used to perform operations using the attributes of the object. Below are some of the
methods used in the student.py in Lambert's source codes. 
 
 

Figure 1.4: Instance methods of Student class 


 
 
 
__str__() method vs __init__() method: 
 
Both the init and  the str method are considered, While the __init__ method initializes the
attributes of a certain object, the __str__ method returns a string representation of an object's
state. For example, in the student class, a __str__ method is called in order to show the name
and the score of the object. 
 

Figure 1.5: The str method 


 
 
Mutators and accessors 
 
A mutator is a method that allows modification of values inside the class. It is also called an
update method since the user can update the state of a certain object. On the other hand,
accessors are those methods which does not affect or change the state  or value of an object but
just simply returns it. In the student class, only the  setScore() method is considered as a
mutator since it allows user to reset the test score of the student.  
 

Figure 1.6: Mutators and Accessors 


 
 
 
 
Second Objective:  The Unified Modeling Language (UML) and the different
relationships among classes 
 
UML (Unified Modeling Language) 
This consists of a set of diagrams that describes the structure of a system by showing its classes,
attributes, methods and the relationship between the objects.  
 

Figure 2.1: Basic format for a UML diagram 


 
The diagram is usually divided into three compartments. The top part is called the Name
Compartment which contains the name of the class. Next, the second compartment is called
the Attribute Compartment which describes and makes an identity for each object. Lastly,
The Operations Compartment which contains the functions or methods used in a class.  
UML is used in order to convert the given system into a programming code, using this can be
efficient and time saving since you already have an idea what functions and methods you will
be using on each class.  
 
At the end of this objective, we will be able to identify on how to differentiate the different
types of relationships among classes.  
 
Inheritance refers to the creation of a new class without any or little modification from the
existing class. This new class is called the child class, while the existing class is called the
parent class. This method is usually used in order to avoid writing redundant codes.   
 Basically, the syntax of the Python Inheritance, the child class inherits features from the parent
class and adding new features to it. Doing this results into re-usability of codes.    
 
Figure 2.1: Inheritance 
 
Association is basically the relationship between two classes which is composition and
aggregation. Association is very similar to Inheritance and both allows the programmers to
reuse the code.   The only difference between the Association and Inheritance is that the
Inheritance extends a class and we cannot custom attributes and behavior to the inherited class
while Association uses an instance of a class.   
  

Figure 2.1: Association 


 
When we talk about Inheritance, we have so called easier relationship. For example, you can
think of an employee, which is a human or you can think of a car which is a vehicle.   
Whereas, when we talk about Association we have composition and aggregation classes.   
   
Aggregation is a weaker form of composition, if a container object is deleted then all of its
content object can still exist without container object. For example, you can think of a team
which is a collection of players, if you dissolve the team the players can still exist.  
Aggregation implies that the relationship between two subjects can independently exist and
even if you delete one subject the other subject can still stand alone.  
  

 
Figure 2.1: Aggregation 
 
 
Composition relationship is created when one of the classes is composed of one or more
instances of other classes. In other words, one of the classes plays a role of a container and
other place the role of a content and if the other container object is deleted then all of its content
objects are deleted as well. For example, you can think of human which has different body
parts, a human can live without body parts, whereas body parts cannot exist alone. So when a
human dies, all body parts die as well.  Composition means that a relationship cannot stand
alone. Once you delete a subject your other subject will not exist. 
 

Figure 2.1: Composition 


 
 
 
 
 
 
Post-Laboratory 
Saturday, February 29, 2020 9:21 PM

Post-Laboratory 
Note: Leaders should assign the problems to members 
• Machine Problems  Lambert: Chapter 9 projects page 349  
• Debugging and Sample Run (with screengrabs and Projects 4, 5 and 6 
discussion 
• UMLET class diagram 
 
Note:  
1. Save all files (.py and .uxf) in one folder, upload the
folder to your OneDrive and put a tiny/bitly url in your
OneNote 
Name the Folder: <Course and
Section>_Group#_Exp#. 
Example: CPE106L-B1_Group01_Expt2 
2. Commit all python source codes to Github (individual
Github account) Github Repository Name: Software
Design Lab Exercises 
Put a tiny/bitly url of your Github repository 

Project # 4 - Fundamentals of Python Programming by Kenneth Lambert  

The ATM program allows a user an indefinite number of attempts to log in. Fix the program so
that it displays a popup message that the police will be called after a user had these successive
failures. The program should double the login button when this happens.  
 
Figure 1.1. Sample output successful login  
 
Based from Figure 1.1, it can be seen that the user has entered the correct credentials, thus the
user was able to access the savings account.  
 

Figure 1.2. Sample output with unsuccessful login  

As shown from Figure 1.2, the user has entered the wrong credentials. Therefore, the status
shows that the name and pin  is not found and the login button was also disabled. 

Figure 1.3. Sample output with three consecutive unsuccessful tries  


 
After trying several attempts in Figure 1.3, the user has entered the wrong credentials three
consecutive times, this denies access from the user and a pop-up message will be shown
indicating that 3 attempts has been reached. The login button has also been disabled.  

Figure 1.4: UML diagram for project 4 


 
 
From Figure 1.4, we can see the UML Class Diagram of the program, we used a closed diamond
in classes savingsAccount, restrictedSavingsAccount, ATM which is connected to the class
Bank since the relationship between them is called composition, which means in order for the
mentioned classes to exist, the class Bank must also exist and if the class Bank is deleted,
therefore the rest of the class will also be deleted. Moreover, we used an arrow for the class
ATM and restrictedSavingsAccount which is connected to the class savingsAccount, since the
relationship used between them is called Inheritance, which means the functions that are used in
the class savingsAccount was reused in the class ATM and restrictedSavingsAccount. Few
functions were also added along the way.   
 
 
 
 
 
Project # 5 - Fundamentals of Python Programming by Kenneth Lambert  
 
 
The Doctor program described in Chapter 5 combines the data model of a doc- tor and the
operations for handling user interaction. Restructure this program according to the model/view
pattern so that these areas of responsibility are assigned to separate sets of classes. The program
should include a Doctor class with an interface that allows one to obtain a greeting, a signoff
message, and a reply to a patient’s string. The rest of the program, in a separate main program
module, handles the user’s interactions with the Doctor object. You may develop either a
terminal-based user interface or a GUI. 

Figure 2.1. Sample output of  program 

As noticed from Figure 2.1, the program requires the user to respond to the question, as the user
answered the following question, the program will repeat the response of the user with a
different pronoun. This was created with the use of replacements, in which certain pronounced
are automatically changed when the user responds.   

Figure 2.2. Terminating the Program 


Based from Figure 2.2,  the program will continuously ask the user different questions, the
program will only be terminated if the user entered the word QUIT. 

Figure 2.3: UML diagram for project 4 


 
 
We state that the different set of sentence from the program namely: the greeting and the
farewell for the start and the end of program. Also In def reply, the probable replies of the
program on user's input. Moreover, the def changePerson is the command to put pronounce that
will make more the sentence off the programs outputs more correct in terms of grammar. Lastly,
The def main is the command that executes the whole program and calls the different definition
in the class. 
 
 
 
Project # 6 - Fundamentals of Python Programming by Kenneth Lambert  
 
 
The play method in the Player class of the craps game plays an entire game without interaction
with the user. Revise the Player class so that its user can make individual rolls of the dice and
view the results after each roll. The Player class no longer accumulates a list of rolls, but saves
the string representation of each roll after it is made. Add new methods rollDice, getRollsCount,
isWinner, and isLoser to the Player class. The last three methods allow the user to obtain last
two methods are associated with new Boolean instance variables. Two other instance variables
track the number of rolls and the string representation of the most recent roll. Another instance
variable tracks whether or not the first roll has occurred. At instantiation, the roll, rollsCount,
atStartup, winner, and loser variables are set to their appropriate initial values. All game logic is
now in the rollDice method. This method rolls the dice once, updates the state of the Player
object, and returns a tuple of the values of the dice for that roll. Include in the module the
playOneGame and playManyGames functions, suitably updated for the new interface to the
Player class.  
 
 
 
On making Project 6, the most important thing is to check die.py since craps.py uses the Die
class from it. Below is the python file of class Die.  

Since we need to randomly generate a value for the die, the random library was used. The Die
class simply represents a six sided die. 

Figure 3.1: class Die 

For the craps.py, the first thing we did was to debug the RollDice method in order for the count
of dice rolls to be initiated and for it to add 1 per round.  The rollDice function also determines
the winner of the round by comparing the sum of 2 dices rolled for each player. On the other
hand, for the getRollsCount method, we simply made it return the number of dice that was
rolled.  

Figure 3.2: rollDice and getRollsCount methods 


Meanwhile, for the isWinner and isLoser methods, it simply returns a boolean value which is
determined by the rollDice function above 

      Figure 3.3: isWinner and isLoser methods 

For the playOneGame method, it simply allows user to play a single game and returns the
winner of that game. The playManyGames method on the other hand allows user to play many
games and return a statistics like the total number of games played, total number of wins, total
number of losses, average number of rolls per win and the average number of rolls per loss.  

Figure 3.4: playOneGame and playManyGames methods 


 
Sample output: 

Figure 3.5: sample outputs 


From the sample output, we can see that for that the program will initially print out a number of
roll and returns if you win or lose. Then it prompts us a number of games that we want to play (I
input 3) and then return the corresponding statistics. 

 
Link for OneDrive files 

Loven Garcia's OneDrive link - http://bit.ly/38wRGiy 


Louisse Velasco's OneDrive link - https://bit.ly/2TxCSMt 
Flynn Alcaraz's OneDrive link - https://bit.ly/2wCmJMy 

Link for Github Repositories 


Loven Garcia's GitHub repository  - http://bit.ly/2IsHDjY 
Louisse Velasco's GitHub repository - https://bit.ly/32ZdqSR 
Flynn Alcaraz's GitHub repository - https://bit.ly/2Tx4TDV 

 
 
 
 
 
 
 
 
 

Potrebbero piacerti anche