OOP [Python] - Test #1 - Python test #1 PDF

Title OOP [Python] - Test #1 - Python test #1
Course Object Oriented Programming
Institution University of Ontario Institute of Technology
Pages 5
File Size 363.9 KB
File Type PDF
Total Downloads 94
Total Views 160

Summary

Python test #1...


Description

Faculty of Business and Information Technology Course: Instructor: Semester: Course component: Duration:

INFR 2141: Object Oriented Programming (Python) – Networking and IT Samaneh Mazaheri Winter 2021 Test #1 – 20% 2 hours and 50 minutes

Instructions: ▪

There are 4 (four) coding questions. Only answer 2 (two) questions using your laptop. Note that you need to answer one question with 35 marks, and one with 20 marks.



You are allowed to use any resource you might find useful.



All your files must have your name and student number commented at the top.



Once you have completed your solution, submit your answers in Canvas to the designated drop box. You can submit multiple files. No need to zip your files.



You are allowed to upload multiple files. (The latest version for each question will be graded.) Naming for your file should be the question number; for example: Q1, Q2, etc.



If during the midterm, you have any question/issue, ask your question in “Piazza” in Canvas. Canvas → Piazza → Test #1 channel



Make sure you submit your files before 4:59pm, otherwise your submissions will not be accepted. (for those of you with accommodation, your extra time will be visible in Canvas).



If you have difficulty submitting your files in Canvas, email your TA all your files immediately and copy me. Failing to do so will result in not accepting your submission. In this case, make sure you ZIPPED them all, as Outlook will block any file with .py extension. Failing to do so will result in not accepting your submission. Your TA email: [email protected] My email: [email protected]

Page 1 of 5

1. Triangle Cl Class: ass: (35 marks) Design a class named Triangle that extends the given GeometricObject class. The Triangle class contains: • • • • • •

Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. The accessor methods for all three data fields. A method named getArea() that returns the area of this triangle. A method named getPerimeter() that returns the perimeter of this triangle. A method named __str__() that returns a string description for the triangle.

The formula for computing the area of a triangle is: 𝑠=

(𝑠𝑖𝑑𝑒1 + 𝑠𝑖𝑑𝑒2 + 𝑠𝑖𝑑𝑒3) 2

𝑎𝑟𝑒𝑎 = √𝑠(𝑠 − 𝑠𝑖𝑑𝑒1)(𝑠 − 𝑠𝑖𝑑𝑒2)(𝑠 − 𝑠𝑖𝑑𝑒3) Implement the Triangle class. Write a test program that prompts the user to enter the three sides of the triangle, a color, and 1 or 0 to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the triangle’s area, perimeter, color, and True or False to indicate whether the triangle is filled or not. class GeometricObject: def __init__(self, color = "green", filled = True): self.__color = color self.__filled = filled def getColor(self): return self.__color def setColor(self, color): self.__color = color def isFilled(self): return self.__filled def setFilled(self, filled): self.__filled = filled def __str__(self): return "color: " + self.__color + \ " and filled: " + str(self.__filled)

Page 2 of 5

2. (Algebra: Quadrati Quadraticc equations) (35 marks) The two roots of a quadratic equation example, 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0, can be obtained using the following formula: 𝑟1 =

−𝑏+ √𝑏2 −4𝑎𝑐 2𝑎

and

𝑟2 =

−𝑏− √𝑏2 −4𝑎𝑐 2𝑎

𝑏 2 − 4𝑎𝑐 is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative the equation has no real roots. Design a class named QuadraticEquation for a quadratic equation 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0. The class contains: • • • • •

The private data fields a, b, and c that represent three coefficients. A constructor for the arguments for a, b, and c. Three get methods for a, b, and c. A method named getDiscriminant() that returns the discriminant, which is 𝑏 2 − 4𝑎𝑐. The methods named getRoot1() and getRoot2() for returning the two roots of the equation using the two given formulas.

These methods are useful only if the discriminant is nonnegative. Let these methods return 0 if the discriminant is negative. Write a test program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display the two roots. If the discriminant is 0, display the one root. Otherwise, display "The equation has no roots." Sample runs:

Page 3 of 5

3. Unique Characters (20 marks) Create a program that determines and displays the number of unique characters in a string entered by the user. For example, Hello NIT NITSS Stude Studennts! has 15 unique characters, while zzzz has only one unique character.

4. Count the elements (20 marks) Python's standard library includes a method named count that determines how many times a specific value occurs in a list. In this question, you will create a new function named countRange which determines and returns the number of elements within a list that are greater than or equal to some minimum value and less than some maximum value. Your function will take three parameters: the list, the minimum value and the maximum value. It will return an integer result greater than or equal to 0. Include a main program that demonstrates your function for several different lists, minimum values and maximum values. Ensure that your program works correctly both lists of integers and lists of floating-point numbers. Submit your main program as a part of your solution too.

Page 4 of 5

From 15.1 of your academic calendar: Academic misconduct includes, but is not limited to: 1. Unreasonable infringement on the freedom of other members of the academic community (e.g., disrupting classes or examinations, harassing, intimidating, or threatening others). 2. Violation of safety regulations in a laboratory or other setting. 3. Cheating on examinations, assignments, reports, or other work used to evaluate student performance. Cheating includes, among other things, copying from another student’s work or allowing one’s own work to be copied, submitting another person’s work as one’s own, fabrication of data, consultation with an unauthorized person during an examination, use of unauthorized aids, or submitting work prepared in collaboration with other member(s) of a class, when collaborative work on a project has not been authorized by the instructor. 4. Impersonating another student or allowing oneself to be impersonated for purposes of taking examinations, or carrying out laboratory or other assignments. 5. Plagiarism, which is the act of presenting the ideas, words, or other intellectual property of another as one’s own, including images, designs, processes, computer software, digital, audio and video files, Internet resources and other works without appropriate attribution or credit. The use of other people’s work must be properly acknowledged and referenced in all written material. 6. Obtaining by improper means examination papers, tests, or similar materials; use or distribution of such materials to others. 7. Falsifying academic records, including tests and examinations, or submitting false credentials for purpose of gaining admission to a program or course, or for any other purpose. 8. Misrepresentation of facts, whether written or oral, which may have an effect on academic evaluation. This includes making fraudulent health claims, obtaining medical or other certificates under false pretences, or altering certificates for the purpose of misrepresentation. 9. Submission of work when a major portion has been previously submitted or is being submitted for another course, without the expressed permission of all instructors involved.

Page 5 of 5...


Similar Free PDFs