SWE 321 Lab 1 - Student Manual
SWE 321 Lab 1 - Student Manual
SWE 321 Lab 1 - Student Manual
SWE 321
OBJECT ORIENTED PROGRAMMING – LAB
Grading Scheme
• The completion and submission of all assignments in this lab manual contributes to 70% of the total
course grade.
• The remaining 30% is for the project component.
• The rubric to grade each assignment and the Grading Key are provided in the Appendix section of
this document. The rubric for the project will be presented in the project document later.
The lab assignments are graded by the instructor and the feedback is provided to the student via Blackboard.
It is the student responsibility to ensure that their individual/group work is updated and submitted on a
timely basis with the working solutions of the assignments and project.
Submission Guidelines
The instructor will follow one/all of the following two modes of the submission for the whole section:
2. Submission in a group:
The instructor will organize the students in groups to experience others’ ideas for problem-solving
and combine groups knowledge and skills. That doesn’t mean you will be splitting the work and not
work together on all questions. To ensure individual practice and learning you must work all
questions together.
When submitting your work, ensure to provide UML diagrams, code in Python, sufficient documentation,
screenshots of output, and other content to help in the assessment of the submitted work.
Labs has some questions for students to practice in class and some questions to work at home then submit
on the Blackboard. So mind the deadlines in the lab syllabus.
Submissions Deadlines
• Please follow the deadlines according to the lab syllabus. Any change to the deadline will be
announced to you by your instructor.
• Evaluation is completed by instructor at the end of each submission deadline.
• For all assignments (Homework in lab manual) the final submission is going to be graded. That
submission is final. If you submitted after the deadline, you will be graded with penalty. Sometimes
it is 100% of the grade if (means zero in the lab) if it is too late.
Code Documentation
All labs and the project have to be solved by Python. Python code that is submitted as solutions for the
assignments must contain proper documentation. Suggested header documentations are provided for
programs and functions. All programs and all functions must contain sufficient documentation to explain
their intent and use.
# Function Description:
# Desc:
# Parameters:
# Return Type:
As a student of the University that carries the name of the beloved and revered father of the nation, the late
Sheikh Zayed Bin Sultan Al Nahayan (may his soul rest in eternal peace),
I pledge to:
Plagiarism Policy
Plagiarism is presenting someone else’s work or ideas as if it were one’s own. Examples of plagiarism include
the following:
• Copying another person’s or group’s work either word for word or making some changes but keeping the
structure, much of the language, and main ideas the same. Even if the work is not published, it should
be treated as someone else’s work and not one’s own work.
• Buying, borrowing, or otherwise obtaining and handing in a paper, project or course assignment as if it
were one’s own.
• Turning in someone else’s paper as if it were one’s own is strictly prohibited, even if the paper is enclosed
in quotation marks. A large part of a paper cannot simply be quotations.
• Allowing someone else to edit, rewrite or make substantial changes in one’s work and turning it in as if
one had done it all, without acknowledging the other person’s contribution and without prior permission
of the instructor.
• Using someone else’s words or ideas without crediting that person.
o If a student uses someone else’s words, he must identify them by putting quotation marks
around them and citing the source.
o If a student downloads a picture from the Internet, he must cite the source of the picture.
o If a student paraphrase someone’s work, he must specify the source of the statement.
o Every book, magazine, or internet site used in a paper must be identified in the bibliography.
• At any time, if a student thinks he may have unknowingly plagiarized someone’s work, he should discuss
it with his instructor before turning in the assignment.
LAB 1 – REVIEW OF BASIC PROGRAMMING STRUCTURES
The assignments for this week provide a review of the topics in SWE-225 Introduction to Programming and
Problem Solving (pre-requisite course). The objective here, is to review basic programming constructs, like
the use of primitive data types, operators, sequential logic, branching with selection, repetitions, the use of
functions, and creating collection of data using lists.
IN CLASS EXERCISES
Part1: Writing python program to code functions (Sample Solution to the 1st question is given):
o All input values are to be taken from the user for part 1 and printed in the main code.
o Ensure to check for possible error cases like mismatch of data type and input out of range, for
every function.
o Include test cases for the functions with different parameters to ensure all working conditions.
1. greatestOfFour (w, x, y, z) - Function returns the greatest of four numbers w, x, y, and z, and return it.
Sample code:
# Function Name: greatestOfFour
# Desc: Finds greatest of 4 numbers
# Parameters: num1 - int, num2 - int, num3 - int, num4 - int
# Return: int
Output:
2. reverseSentence(sentence) - Function that returns the reverse of a given sentence (words) entered by
user.
3. listOdd(n1, n2) – Function to return list of all the odd numbers between two numbers.
4. calculateTotalSalary(month_sal, no_of_years) - Function returns the computed total salary based on
monthly salary and number of years. If the number of years is greater than 4 years, an increment of 3%
is added to the total salary from 5th year.
5. listMax(numList) – Function returns the maximum from a list of numbers.
6. profit_or_loss(cost_price,selling_price) - Function returns the calculated actual profit or loss based on
cost price and selling price using the formula, Profit = (Selling Price - Cost Price) and Loss = (Cost Price -
Selling Price).
7. printEveIndexChar(str) - Function that returns only those characters which are present at an even index
number in a given String.
8. rental_car_cost(rate_per_day, no_of_days) - Function that returns the calculated cost of renting the car
based on the rental rate per day and number of days the car is rented. If you rent the car for 7 or more
days, you get $40 off your total cost. Alternatively, if you rent the car for 3 or more days, you get $20 off
your total. You cannot get both of the above discounts at the same time.
9. average(list) - Function to find the average of given list of numbers.
10. square_of_numbers(num1, num2) -function to create and returns a list where the values are square of
numbers between 1 and 30 (both included).
Part2: Writing python program to read/write from/to files (some sample text files are given):
11. Write a Python program to read and print an entire text file (create your own text file).
12. Write a Python program to read a file line by line and store it into a list (create your own text file).
13. Write a Python program to find the longest word in a text file (create your own text file).
14. Write a Python program that reads a text file called “sample.txt”.
• A sample of the file is provided below, you may add more details to the file.
• Your program must read the data from the file and count the number of words in the file
sample.txt:
15. Write a Python program that reads a text file called “employee.txt”.
• A sample of the file is provided below, you may add more details to the file.
• Each row has an employee name and respective salary. Your program must read the data from
the file and identify the person with the highest and lowest salaries in the list.
salary.txt:
John 41500
Mary 43500
Ahmed 31000
Tom 55300
Peter 38600
Samantha 36900
16. Write a Python program asking the user for a username and password as shown below:
Login:
Username: John
Password: abc@123
• The Python program should check if the user exists in a text file named user.txt.
• A sample of the file is provided below, you may add more details to the file.
• If the user exists, it should welcome the user by printing “Welcome <username>”, if the user does
not exist, it should print “Invalid User”.
user.txt:
John abc@123
Tom pqr@789
Mary xyz@456
HOMEWORK:
The submission for all functions and text files have to match the requirements:
1. cent_to_inches_&meters(centimeter) - Function that asks the user to input data in centimeter (Cent),
convert it to inches (Inches) - using the formula: Inches= Cent/2.54) and convert it to meters (Meters) -
using the formula: Meters= Cent/100). Both values to be returned to the main to print.
2. countVowels(sentence) – Function that returns the count of vowels in a sentence. Check for both upper-
case and lower-case alphabets and convert them all into lower case.
3. square_of_factorial(num) - Function that computes the factorial of a number entered then square the
result. Example of factorial of 5 is F= 5*4*3*2*1. Square of factorial S=(120)^2
4. commonData(list1, list2) - Function that takes two lists (of numbers or characters) and returns true if
they have at least one common element.
5. sortList(nList, order) – Function that sorts a list nList, either in descending or ascending order.
6. Write a Python program that reads a text file called student.txt.
• The input text file contains student_ID, student_name, Quiz_grade as well as Test_grade for
each student.
• Your program should read each student details from the file
• Then calculate the average of Quizzes and Tests marks
• Display the students’ details: Total number of students, total average, the total highest and
lowest marks.
• The program should output the report into a file called output.txt.
• A sample of output text file is given below.
student.txt:
output.txt:
Class Report
There are total of 6 students
The average of total marks is 71.267
Shamma (20197541) scored the highest marks as 45
Hamda (20185432) scored the lowest marks as 92.8
TEXT FILE READING AND WRITING
In Python, text files are a sequence of lines. Each line ends with a special character, called the EOL or End of
Line character (this represents the enter key ↵). It ends the current line and tells the interpreter a new line
has begun.
To open a file for writing or reading you must use the Python built-in function: open().
The open() function, returns a file object which is a handler for the file. The file object or handler has a
number of functions associated with it, to help handle the file i.e. to read or write to the file.
So, for example, to open a file called “height.txt”, the Python syntax would look like this:
foHeights = open("height.txt", "r")
Here, “r” stands for “read”, which is the mode to open the file with.
So, the code above is asking Python to open the file named “height.txt” in the read mode i.e. to read from
the file and not to write to the file. The variable foHeights is the file object which handles the file
henceforth in the program. Don’t forget to create the file “height.txt” before running the above line of
code!
• ‘r’ – Read mode which is used when the file is only being read
• ‘w’ – Write mode which is used to edit and write new information to the file (any existing files with
the same name will be erased when this mode is activated)
• ‘a’ – Appending mode, which is used to add new data to the end of the file; that is new information
is automatically amended to the end
• ‘r+’ – Special read and write mode, which is used to handle both actions when working with a file
The above code uses the for loop to loop through each line of the file and prints it out.
Now, let’s look at how to make a simple copy of the file into another.
We would need the first file object for the file we read from and the second file object for the file we have
to write to. Each needs to be opened in the appropriate mode. Then, we read through every line in the first
file and write it to the second file. Here’s the code.
The for loop reads every line from foHeights and the write() function of fwHeights in the loop writes every
line to the new file. Try it out!
While reading the lines from a file in the loop, the program can make various decisions and process the
content of the file using the usual programming techniques.
UML TOOL
Download the UMLET tool to draw UML diagrams. Any tool that helps to draw UML diagrams can be used,
this is only suggestion.
1. Navigate to http://www.umlet.com/changes.htm
The reader is advised to go through the following content to understand assessment criteria for the lab
assignments. The assignment grading rubrics are provided here and is used to assess the submitted
assignments.