CS 30 Homework 6

Due by class time Wednesday, March 29

If you wish, you may work with a partner on this assignment.

Reading

Problems to Turn In

  1. Finish implementing the class definitions for Car, Student, BeerCan, and Neuron from Lab 9.

  2. Add a method to your Neuron class called adjustWeights, which takes two inputs: a pattern such as [1, 1], and a value called correctResponse, which is either 0 or 1. This method should do the following:

  3. We can use this simple technique to train a neuron to recognize a particular pattern by repeatedly calling adjustWeights on the pattern and the desired output. To try this out, first randomize the weights of your neuron by calling n.randomize(). Then call n.test() to verify that the neuron produces 0 on all four test patterns. Now call n.adjustWeights([1, 1], 1) a few times, observing how the weight values and the neuron's behavior change after each weight adjustment. Eventually, the neuron should learn to produce 1 in response to the pattern.

    >>> n.randomize()
    >>> n.weights
    >>> n.test()
    >>> n.adjustWeights([1, 1], 1)
    >>> n.weights
    >>> n.test()
    ...
    
  4. Consider the BankAccount example. Add a too-good-to-be-true method called resetBalance, which takes no input parameters and resets the account's balance to the original amount that it was created with. For example:

    >>> a = BankAccount("777-101", 500)
    >>> a.withdraw(350)
    Withdrew $350.00
    >>> a.withdraw(100)
    Withdrew $100.00
    >>> a.inquiry()
    Balance is currently $50.00
    >>> a.resetBalance()
    Balance is currently $500.00
    >>> 
    
  5. Now let's make BankAccount objects be password-protected. Modify the constructor so that it takes an extra password string as input, as in

    a = BankAccount("777-101", 500, "open sesame")
    

    The account should process a withdraw or inquiry request only if it is accompanied by the password with which the account was created, and should otherwise complain:

    >>> a.withdraw(100, "open sesame")
    Withdrew $100.00
    >>> a.withdraw(100, "abracadabra")
    Sorry, that password is incorrect
    >>> a.inquiry("stick em up")
    Sorry, that password is incorrect
    >>>
    
  6. Implement a Product class. A product has a name and a price, for example: Product("Toaster", 29.95). Include a method __str__ that returns the name and price of the product as a formatted string, a method priceWithTax(percent), which returns the price with sales tax of the given percentage added on, and a method setNewPrice(amount), which changes the product's price to the new amount. Your objects should behave as follows:

    >>> gizmo = Product("Ronco Tomato Scrambler", 39.95)
    >>> print gizmo
    Ronco Tomato Scrambler, price: $39.95 plus tax
    >>> gizmo.priceWithTax(10)
    43.945
    >>> gizmo.setNewPrice(49.95)
    Ronco Tomato Scrambler now costs $49.95
    >>> gizmo.priceWithTax(10)
    54.945
    >>>
    
  7. Implement an Auditorium class. An auditorium has a seating capacity that is specified when the auditorium is created. Once this capacity is reached, no more seats can be filled. An Auditorium object should keep track of its seating capacity, and the number of seats that are currently open. Write and test the following methods:

    >>> carnegieHall = Auditorium(1500)
    >>> carnegieHall.lookInside()
    No one is inside
    >>> carnegieHall.fillSeats(1000)
    Filled 1000 seats
    >>> carnegieHall.lookInside()
    1000 people inside with 500 seats left
    >>> carnegieHall.fillSeats(700)
    Sorry, sold out after filling 500 seats
    >>> carnegieHall.lookInside()
    1500 people inside with 0 seats left
    >>> carnegieHall.fillSeats(100)
    Sorry, all sold out
    >>>
    
  8. Implement a Vehicle class that, like the Auditorium class above, keeps track of the number of people inside and the total number of seats available. However, a Vehicle should also remember the names of each person inside. Write and test the following methods:

    >>> chevy = Vehicle(4)
    >>> chevy.addPerson("Thelma")
    >>> chevy.addPerson("Louise")
    >>> chevy.lookInside()
    Thelma
    Louise
    2 empty seats left
    >>> 
    

Turning in Your Homework

Put all of your code into a single file called assign6.py. Include your name at the top of the file as a comment, and submit it using /common/cs/submit/cs30-submit. Be sure to run this command from the directory containing your file.

If you have questions about anything, don't hesitate to ask!