CMU 15-112: Fundamentals of Programming and Computer Science
Extra Practice for Week 9 (Due never)

  1. Do pages 1-2 (including Student and Class) from s19 quiz8

  2. Do page 3 (including Vehicle and Car) from f18 quiz9

  3. # This is from this week's recitation. # Write the classes required to pass these tests: def testCakeClasses(): cake = Cake("Vanilla") print(str(cake)) # "Vanilla Cake" # you can add unlimited toppings to cakes for i in range(3): cake.addTopping("Almonds") print(cake.toppings) # ["Almonds", "Almonds", "Almonds"] appleFruitCake = FruitCake("Apple") print(isinstance(appleFruitCake, Cake)) # True appleFruitCake.addTopping("Peanut Butter") appleFruitCake.addTopping("Ranch") # yum print(appleFruitCake.toppings) # ["Peanut Butter", "Ranch"] # You can only add 2 toppings to a fruitcake appleFruitCake.addTopping("Tomatoes") print(appleFruitCake.toppings) # ["Peanut Butter", "Ranch"] print(str(appleFruitCake)) # "no one likes Apple fruitcakes" blackForestCake = YummyCake("Chocolate", "Vanilla") # cakes aren't yummy without frosting! print(isinstance(blackForestCake, Cake)) # True print(isinstance(blackForestCake, FruitCake)) # False print(str(blackForestCake)) # "Chocolate YummyCake with Vanilla frosting" # you can add unlimited toppings to yummy cakes for i in range(69): blackForestCake.addTopping("Cherry") print(blackForestCake.toppings) # ["Cherry"]*69 print(blackForestCake == YummyCake("Chocolate", "Vanilla")) # True print(blackForestCake == YummyCake("Vanilla", "Chocolate")) # False print(blackForestCake == appleFruitCake) # False print(blackForestCake == "sign up for hack112!!") # False # YummyCake class keeps track of what cakes combos have been made carrotCake = YummyCake("Carrot", "Cream Cheese") print(YummyCake.hasUsedFlavorCombo("Carrot", "Cream Cheese")) # True print(YummyCake.hasUsedFlavorCombo("Carrot", "Vanilla")) # False superCarrotCake = YummyCake("Carrot", "Cream Cheese") superCarrotCake.addTopping("Sprinkles") # perfection # you can put yummycakes in sets cakes = set() print(carrotCake in cakes) # False cakes.add(carrotCake) print(carrotCake in cakes) # True print(superCarrotCake in cakes) # True testCakeClasses()

  4. # This is from this week's recitation. # You've been hired by a stealth-mode gaming startup called [REDACTED] # and your first task is to copy a prototype of the 2015-era-popular # game # Thankfully, you're well versed in event-based animations and this # should be fairly straightforward for you! Your prototype must have # the following: # * 600x600 canvas # * One radius 40 yellow dot in the center of the screen with its # coordinates displayed # * It can move around, but it always appears in the center of the screen # * 50 lightgreen dots with radius 20, randomly distributed on -1200 to 1200, # and -1200 to 1200 (most will not be immediately displayed on the canvas) # * Clicking on the canvas puts a lightblue dot there # There is some starter code provided. Please run it, and make modifications # such that it fits the spec! from cmu_112_graphics import * from tkinter import * import random class Dot(object): def __init__(self, x, y, r, color): self.x = x self.y = y self.r = r self.color = color def move(self, dx, dy): self.x += dx self.y += dy def draw(self, canvas): canvas.create_oval(self.x - self.r, self.y - self.r, self.x + self.r, self.y + self.r, fill=self.color) canvas.create_text(self.x, self.y, text=f'{self.x}, {self.y}') class SideScroller(App): def appStarted(self): = Dot(self.width//2, self.height//2, 40, "yellow") def movePlayer(self, dx, dy):, dy) def keyPressed(self, event): if (event.key == "Left"): self.movePlayer(-5, 0) elif (event.key == "Right"): self.movePlayer(+5, 0) elif (event.key == "Up"): self.movePlayer(0, -5) elif (event.key == "Down"): self.movePlayer(0, +5) def mousePressed(self, event): # TODO: implement this! pass def redrawAll(self, canvas): canvas.create_text(300, 150, text='Finish this app!') SideScroller(width=600, height=600)