Saint Louis University |
Computer Science 1300/5001
|
Computer Science Department |
Topic: Class Definitions
Related Reading: Chapter 16
Due:
10:00am, Wednesday 31 October 2018
Please make sure you adhere to the policies on academic integrity.
In a moment, we propose an implementation for a Radio class. For this problem, please do not focus on whether or not this is an accurate model for the semantics of a radio. Instead, our primary concern is that there are five critical mistakes with the implementation that lead to explicit errors.
You are to identify these five errors and for each:
Our implementation can be download electronically (code here), if you wish to experiment, or is displayed as follows (line numbers only for reference).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class Radio: def init(self): self._powerOn = False self._volume = 5 self._station = 90.7 self._presets = [ 90.7, 92.3, 94.7, 98.1, 105.7, 107.7 ] def togglePower(self): self._powerOn = not self._powerOn def setPreset(self, ind): self._presets[ind] = _station def gotoPreset(self, ind): self._station = self._presets[self._ind] def increaseVolume(self): self._volume = self._volume + 1 def decreaseVolume(self): self._volume = self._volume - 1 def increaseStation(self) self._station = self._station + .2 def decreaseStation(self): self._station = self._station - .2 |
For this problem, we revisit the robust verison of a Point
class that is used to represent
A better approach is to design a single Point class that can be used to model a point with arbitrary dimension. We've started such a class (code here). Internally, we keep a list of coordinates. For uniformity, rather than identifying coordinates with letters like x and y, we use indexing so that the coordinates of a point p can be referenced using a syntax such as p[k] for the kth coordinate (when zero-indexed). We do that by implementing special methods __getitem__ and __setitem__ in lieu of the methods getX, setX getY, and setY from our original Point class definition.
In fact, we can generalize all of the behaviors originally shown in the two-dimensional case. For our new multi-dimensional Point class, we have intentionally omitted implementations for two methods: scale and __add__. Your goal is to implement those.
While we suggest that you download our code and develop your implementations electronically, we will still collect your solutions on paper, and you need only provide the code for those two methods (not the whole class). Specifically