Objective: Create a class in Java, make instances of that class. Call methods th
ID: 3762337 • Letter: O
Question
Objective: Create a class in Java, make instances of that class. Call methods that solve a particular problem Consider the difference between procedural and object-oriented programming for the same problem.
Program Purpose:
Find out how long it will take Ashleyto lose all of her money playing 3 instances of the SlotMachine class.
Part 1. Specification: Ashley in Vegas
Ashley takes a jar of quarters to the casino with the intention of becoming rich. She plays three machines in turn. Unknown to her, the machines are entirely predictable. Each play costs one quarter.
(This is a new spec – not the same numbers as the last time)
the first machine pays 30 quarters every 40th time it is played;
the second machine pays 60 quarters every 85th time it is played;
the third pays 11 quarters every 10th time it is played.
(If she played the 3rd machine only she would be a winner.)
Your program should take as input the number of quarters in Ashley's jar, and the number of times each machine has been played since it last paid.
Your program should output the number of times Ashley plays until she goes broke.
Sample Session. User input is in italics
How many quarters does Ashley have in the jar? 5000
How many times has the first machine been played since playing out? 30
How many times has the second machine been played since paying out? 10
How many times has the third machine been played since paying out? 9
Ashley plays XXX times
Specific requirements: You must solve this using an object-oriented approach. Create a class called SlotMachine.
Think about: What does a SlotMachine Have? Instance variables.
What does it do? Instance methods.
This is the “state” and “behavior” of the object. Each instance of this class will represent a single slot machine.
In the main method, create 3 instances of the slot machineclass to solve the problem in the spec.
play() the machines in a loop that ends when she runs out of money.
This exercise is to reinforce the difference between procedural programming and OO programming. When you are working with objects, think of calling their methods as asking the objects to do things with their own data. For example
car.accelerate(3); // hey car, go increase your speed by 3
bucket += slotMachine1.play(); // if you win something put it in the bucket
The details of how the objects do things are buried in the code that is inside the class.
Encapsulation (the data in the object is manipulated inside the object) and,
Abstraction ( the user of the object does not see the details of what it is doing) are the OO principles here.
When you are writing code that takes data out of objects does something with it and then puts the data back inside the object, you are doing procedural programming with objects, as opposed to OO programming. Consider the following:
From the previous assignment, this is an example of what we wouldn’t do in OO thinking
car.setSpeed( car.getSpeed() + 3);
Instead we would ‘abstract’ that functionality by putting the detail inside the class by making a method that can be called like this
car.accelerate(3);
Think about this when you are designing your class. What does the slot machine know about? It must only be properties of the machine, not properties of the player, or the casino.
The logic for determining if the player has won must be encapsulated into the class SlotMachine. From the perspective of the user of the SlotMachine class, think of it like the player of the physical slot machine. What can the player see? The code that uses a Slotmachine object should ‘see’ what the player of the physical machine would see.
Show this test case:
Ashley starts out with 5000 quarters, and the machines have been played 30,10, and 9 times respectively. How many plays does she have before running out of money? Post your answer on the course homepage discussion board.
Also show the test case that appears in the sample output. Post your answer – the number of times she plays - on the discussion board to make sure everyone gets the same thing.
Part II An Extension of Part 1.
Use the same SlotMachine class from the previous part. Create 50 SlotMachine objects, storing them in an array. SlotMachine[] slots = new SlotMachine[50];
You will create these slot machines using random numbers to generate the payout amount and the interval to payout.
Generate a random number for the payout amount between 10 and 100.
The interval will be the payout amount + a random number not bigger than ½ of the payout amount.
So, if for the first machine, you generate a payout of 75, the payout interval will be
75 + random.nextInt( 75 / 2 )
Generate another random number for how many times the machine has been played already. That will be up to the payout amount – 1.
To test your array of slot machines. Suppose a wealthy gambler comes to the casino to play each of the 50 slot machines until he loses in sequence. She starts with one million quarters. How many times does she play until he runs out of money.
Explanation / Answer
http://www.javacodegeeks.com/2014/08/programming-a-simple-slot-machine-game-using-java.html
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.