Course Home | Class Schedule | Assignments | Git Submission | Perusall | Python | Tutoring

Saint Louis University

Computer Science 1300/5001
Introduction to Object-Oriented Programming

Michael Goldwasser

Fall 2018

Computer Science Department

Homework Assignment 14

Defining a Class

Overview

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.


Problems to be Submitted (20 points)

  1. (10 points)

    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:

    1. Identify the line number of the error
    2. Explain in your own words why the existing code leads to an error
    3. Explain how the code should be modified to avoid that error

    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
    

  2. (10 points)

    For this problem, we revisit the robust verison of a Point class that is used to represent two-dimensional points. If we wanted to represent three-dimensional, we could rewrite that code presumably to include a z-coordinate. But that seems like a poor design to have to duplicate so much code for a closely related concept. Worse yet, what if we wanted a four-dimensional point? or a five-dimensional point?

    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


Michael Goldwasser
CSCI 1300/5001, Fall 2018
Last modified: Tuesday, 30 October 2018
Course Home | Class Schedule | Assignments | Git Submission | Perusall | Python | Tutoring