Reference no: EM132357206
Question 1: Analysis and design
Comment on the type of question
This type of question provides a scenario (a problem to be solved or simulated and asks you to write the pseudocode. Structure and logic of your answer is important. Clearly show the beginning and the end of the program as well as conditional statements (if..elif..else). Use indentation to clearly identify blocks within the program.
Example question
A sports club is interested in classifying its members based on age and gender. Female members are put in group 'F' and male members are put in group 'M' unless the member is an adult - in which case they are put in group 'A' regardless of their gender. A member is considered to be an adult if he or she is 18 years of age or older.
Write a pseudocode to describe an algorithm for a program that allows the club to enter a member's name, gender and their age, and then prints out a message containing their name and the group into which they belong.
A typical example interaction with the program might look like the following:
Enter member name? Julie
Enter gender (F/M)? F
Enter age? 10
Julie has been put in group F
Get member name
Get member gender
Get age
If age>= 18
Group equals adult
Elif gender = ‘f'
Group = ‘f'
Else group = ‘m'
Print
Question 2: Selection
Comment on the type of question
Implement the algorithm in the previous question in Python programming language. The program should be syntactically complete and correct and should only use a main() function and no other.
Example question
Write a complete Python program that implements your algorithm from Question 1. The program will prompt the user to enter input data, process data and then print an appropriate message to display the member's group.
Note that you must NOT use any function other than the main() function to answer this question.
Name = input("enter name")
Gender = input("enter gender ‘F\M' ")
age = int(input("enter age"))
If age >= 18:
Group = "A"
Else:
Group = gender
Print ("name:-", name, "gander:-", gender, "age:-", age , " group:-" , group)
Question 3: Functions
Comment on the type of question
Implement the algorithm in the previous question this time utilising a function, which determines the group of the member. The interaction with the user is done from the main function.
The main function displays the necessary prompts to get member's name, gender and age; determines whether the person is an adult or not and then calls the new function to determine the group; finally, it displays the name and the group of the member. The new function takes gender and a boolean argument indicating whether the member is an adult or not (True or False) as input and returns the group (as ‘A', ‘F', or ‘M') to the calling function (ie. the main()).
Example question
Write the Python code to define a function called determineGroup() that takes two inputs (a character string gender and a boolean to indicate adult or not) and returns the group of the member (a character string). Rewrite the program from Question 2 to utilise this function. The 'input' and 'output' operations (interaction with the user) remain in the 'main' function.
Def determineGroup(gen,adult):
If adult == "true":
Group = ‘a'
Else :
Group = gen
Return group
Def main
Inputs
If age >= 18
True
Else
False
Group = determineGroup(gen, adult)
Question 4: Loops
Comment on the type of question
Modifying/extending the previous program to allow for the processing of multiple members using a while loop.
Example question
Modify your program from Question 3 to handle the processing of a number of members in the same run. The program should provide necessary prompts for entering data, the call to the function to determine the group, and displaying the group within a loop.
The loop should continue to execute until the user enters a blank name (ie the user enters nothing by pressing the Enter key when asked to supply a name).
It is assumed that function determineGroup() has already been defined and as such there is no need to show the function definition in your new program. Only the function calls need to be shown.
Def main()
Name = Input("name")
While name ! = ‘' :
Question 5: Programming Principles
Comment on the type of question
This question includes several parts of descriptive questions that relate to basic programming principles.
Example questions
(a) Why are comments used in a program? Briefly describe two types of comments.
(b) What are the advantages of modularising programs using functions
(c) Briefly describe two tools that can be used in presenting the design of a program
(d) What are the five naming rules for functions and variables in Python programming language?
(e) Describe the main stages in program development cycle
Question 6: Files and Exceptions
Comment on the type of question
Writing Python code to open a text file for reading and then processing the contents and displaying the output on the screen as well as sending the output to another text file which is opened for reading. Using exception handling to cover for situations where an error occurs during the opening, closing or processing text files. Close the file(s) after finishing with them.
Example question
(a) Write the Python code to open a text file for reading and display the output line by line on the screen.
(b) Modify the program in previous part to use exception handling to deal with the case where an error occurs during the opening of the file.
(c) Extend the program further to open another file for writing so that the output is sent to another text file as well as the screen.
Question 7: Sequences and Strings
Comment on the type of question
Given a list and/or a string object, perform a number of associated list and/or string operations and functions with and on the objects.
Example question
Consider the list below:
myList1 = [‘N', 'X', 'A', 'C', 'W', 'E']
For the following questions, assume the list starts from this state in each question. Moreover, you only need to write the line/s in Python code to do the following tasks and do not need to worry about other parts.
Write Python code to:
a) Print the 2nd and 5th elements of the list
b) Delete the 3rd element from the list
c) Find the length of the list
d) Add an element with value ‘U' at the end of the list
e) Add an element with the value ‘G' between the elements ‘X' and ‘A'
f) What is the output of the statement print(myList1.pop()) ?
g) Find the minimum and maximum elements in the list
h) Iterate through the list concatenating all elements into a single string variable s1
i) Sort the list in ascending order
j) Reverse the list
k) Concatenate a second list ‘myList2' to the list
Now consider the string given below:
myString1 = "My cat was sleeping in the room"
Write Python code to:
l) Create a list of strings consisting of words making up the string ‘myString1' and assign the result to a list variable called ‘stringList'
m) Replace the substring ‘cat' with ‘dog'
n) Specify a string slice that contains substring ‘ping'
Question 8: Classes and Objects
Comment on the type of question
Defining classes in Python, instantiating classes (creating objects) and invoking functions on objects.
Example question
(i) Define a class called ‘Person' with the following characteristics: a constructor (initialiser function); two data members ‘name' and ‘age'; one getter (accessor member function) and one setter (mutator member function) for each data member and a member function called ‘display' which prints the data members of Person objects.
(ii) Assuming that the class definition above is saved in file ‘person.py' in the same folder with the file "myFile.py' which has the ‘main' function in it, write the code to create two Person object one with name ‘Richard' and age ‘23' and the other with name ‘Janet' and age ‘19'.
(iii) Add code to the main, which will display the details of the person objects p1 and p2.
(iv) Add code to the main, which will change the name of the first person object (p1) to ‘George' and the age of the second person object (p2) to 30.
(v) Add code to the main to check the new name of the first Person (p1) and the new age of the second Person object (p2).
(vi) Add code to the class definition of Person which will allow one to set the address of the person as well as getting it and displaying it with the other details using the display member function.