Copy the program from the IceCreamScoop example into your IDE, using the general developer tips that follow to organize the source code. You should have a folder that contains
IceCreamScoop.py
main.py
Confirm that you can run the program and that you see the following print out:
This vanilla ice cream scoop, topped with ['sprinkles'] has not melted!
This strawberry ice cream scoop, topped with ['whipped cream'] has melted :(
Upload the two files to Gradescope Lab 1 Part 1: Ice Cream assignment and confirm that you pass the tests!
Think about how you will organize your code and work in your IDE. I recommend making a cs161 directory, then using it as the main “working directory” for VS Code. The easiest way to do this is to use File -> Open Folder and then choose this directory.
When you are working on a project, create a new directory to organize the files associated with it. Particularly since we will organize the code so that each class definition is in a different file, put all files for a project in the same directory.
To use a class that is defined in another file, you’ll need to import it, much the way you’ve used modules like random and json. Since you want to include a class defined in another file, the syntax is
from <script name without the extension> import <class name>
So, for example, since I'll use the convention of naming the file with a class definition the same as the class name (put the IceCreamScoop class definition in a file named IceCreamScoop.py, we need to have the following in your main.py file which construct IceCreamScoop instances.
from IceCreamScoop import IceCreamScoop
This is not a typical Python naming conventions, but will help us transition to Java!
As a general rule of thumb, use conventional identifier rules when you’re naming directories and files.
In particular, do not put spaces in directory or file names!
Marker.py
Define a simple class that represents a marker. Write your class definition in a file named Marker.py, adhering to the following specification:
The class should be named Marker.
There should be a two instance properties of type str that maintain:
the color of the marker
the color of the marker's cap
The constructor should take in a single argument for the color of the marker. The cap should be initialized to the same color.
There should be getters for each instance property called getColor and getCapColor.
There should be a setter to change the marker cap color (because this happens in real life!) called setCapColor.
Since markers can’t change color, there will be no setter for the marker's color.
There should be an instance method called __str__ that takes in no parameters and returns a string representation of the marker. If the marker is red with a blue cap, for example, it should return
"A red marker with a blue cap."
main.py
In a file main.py, define a main function that:
Instantiate a new Marker instance that represents a red marker and assign it to a variable called redMarker.
Instantiate a new Marker instance that represents a green marker and assign it to a variable called greenMarker.
Print information about the greenMarker.
Print information about the redMarker.
Set the color of the cap of the red marker to be blue.
Print information about the redMarker.
Submit the following files to the Gradescope assignment Lab 1 Part 2: Markers.
Marker.py
main.py
You’ve been asked to help the Fimbel Maker & Innovation Lab support an independent project to create a slot machine. The slot machine will have a varying number of reels (e.g., 3 or 4) that can take on 10 different values: the numbers 1-9 and the $ symbol. When a slot machine is played, each reel spins to a random value; if the values match, the “payout” is equal to the number shown OR $1000 for all $ symbols.
You’ll be programming this lab in the IDE of your choice and submitting the python files via Gradescope. I will not be providing you with all instance properties and methods, so you will need to think a bit about the design. You may work in pairs or groups of 3 if you would like.
Your design should have:
A Reel class (defined in a file called Reel.py) to represent a single reel
There should be a spin method to randomly determine the reel's value (a number from 1 to 9 or the $ symbol) and return the result as a str. For example, if the value is 8, you should return "8"; if the value is $, you should "$".
A SlotMachine class (defined in a file called SlotMachine.py) to represent a single slot machine with n reels
There should be a play method to compute and return the payout as an int (0 if the reels do not all match, 1000 if the reels all show $ and the number shown otherwise). The method should also print the values of each reel, using the following formatting. Be sure to pay attention to capitalization, spaces and punctuation as the autograder is very unforgiving :(
You got 333 with a payout of $3!
You got $$$ with a payout of $1000!
You got 434 with a payout of $0!
A main.py file that tests your SlotMachine class.
To align with my testing code, you’ll need to use random.randint(0,9) when spinning a Reel; if 0 is generated, you should display the $ symbol.
Probably the SlotMachine constructor should take in a parameter specifying the number of reels...
Submit the following files to the Gradescope assignment Lab 1 Part 2 Bonus: Slot Machine.
Reel.py
SlotMachine.py
main.py