Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit 


COMP1040: Problem Solving and Programming

Final Examination – Trimester 1, 2020

 

QUESTION 1                                                                                                                                                                         [15 marks]

a)   Write a while loop that iterates through the sequence 1 to 10 stepping by 2.

[3 marks]

b)   Generate a random number and use it to create a boolean variable where if the random number was 1 the boolean would be True; and if the random number was anything else, the boolean would be False.

[3 marks]

c)    Define a function called getFamily that takes a number as a parameter. It should loop number times, taking input for different names. Each name should be added to a list and that list should be returned.

[5 marks]

d)   Write code to convert a random number between 1 and 1000 to a string and output its length.

[2 marks]

e)   Write code to calculate how many lots of 5 are in 23 and how many are left over.

[2 marks]

 

QUESTION 2                                                                                                                                                               [15 marks]

a)   Take input for three angles of a triangle. The sum of the three angles must add up to 180 degrees. If they do not, take input again, and continue to take input until 3 valid angles have been entered.

[8 marks]

b)   Given three angles, calculate and output what type of triangle is represented by those angles. It is "acute" if all angles are less than 90 degrees; a "right angle" triangle if one of the angles is 90; and an "obtuse triangle if one of the angles is greater than 90.

[7 marks]

 


QUESTION 3                                                                                                                                                               [15 marks]

a)   Write a function called splitNames that takes a name as a parameter and returns a list containing all the words in that person's name. You may not use the str.split() function.    You must use a loop in your         solution.

[8 marks]

b)   Given two people's names, use your splitNames function, written in part a), to obtain two lists containing     the words in those names. Swap the first person's first name for the second person's last name. Swap the     second person's last name for the first person's first name. Then output the resulting name as a string (with no square brackets []).

If you did not complete part a), you may assume that you did in order to answer this question.

For example, if person1 was "Michael Edward Karl Lever Ulpen" and person2 was "Nittin" the output should be:

person1 = "Nittin Edward Karl Lever Ulpen"

person2 = "Michael"

For example, if person1 was "Vanetta Lever" and person2 was "Peter Arvid" the output should be: person1 = "Arvid Lever"

person2 = "Peter Vanetta"

[7 marks]


QUESTION 4                                                                                                                                                               [15 marks]

a)   Write code to open up a file called "people.txt" for writing. Write code to take input for people's names until the name is an empty string. For each name also take input for that person's age. If the age is negative,           output an appropriate error message and take input for that age again, until valid input is entered. Each          name should be written to the file followed by a space and the associated age, with one name age pair on      each line.

For example, after inputting the names and ages, the file might contain:

"Bob 51

Mary 17

Sue 29

John 18"

[8 marks]

b)   Write code to open up the file called "people.txt" for reading. Write code to iterate over all lines in the file and for each name and age pair, output the values in the format:

"name is age years old" where name is the person's name and age is the person's age. If you did not complete part a), you may assume that you did in order to answer this question.

If we use the file from the previous example the output would be:

"Bob is 51 years old"

"Mary is 17 years old"

"Sue is 29 years old"

"John is 18 years old"

[7 marks]


QUESTION 5                                                                                                                                                               [10 marks]

a)   Write the get_ink and set_ink methods in the following Pen class. Get_ink should return the __ink variable. Set_ink should set the __ink attribute to an argument if the argument is greater than or equal to zero.          Otherwise, the method should output an error message.

[5 marks]

class Pen:

def __init__(self, colour):

self.__colour = colour

self.__ink = 100

def write(self, text):

i=0

while i < len(text) and ink > 0:

print(text[i], end="")

self.__ink -= 1

def get_colour(self):

return self.__colour

def set_colour(self, newcolour):

if newcolour != "":

self.__colour = newcolour

else:

print("Error")

def __str__(self):

return self.__colour + " Pen"

def get_ink(          ):

def set_ink(           ):

b)   Use the Pen class, written in part a), to write the letter 'A' until the __ink is zero. Use the set_ink method to set the ink back to 5. Then write your name using the Pen.write method. Finally, print the pen.

[5 marks]