Write a class (Solitaire), in Java, given the following: You are given a Solitai
ID: 643941 • Letter: W
Question
Write a class (Solitaire), in Java, given the following:
You are given a SolitaireTester class that uses Solitaire.
public class SolitaireTester { public static void main(String[] args) { Solitaire s = new Solitaire(); System.out.println("Start: " + s.toString()); int rounds = 0; while (!s.over()) { s.round(); ++rounds; System.out.println(rounds + ": " + s.toString()); } System.out.println("Rounds: " + rounds); } }
SolitaireTester produces a random starting configuration and prints it. Then keeps applying the solitaire step and prints the result. Stops when the solitaire final configuration is reached and prints the number of rounds.
The Solitaire class:
Constants as required
Exactly and only the following instance variable is required: private ArrayList piles;
constructor() Initialize piles to a random number of piles of random size, but exactly 45 cards total. Use Math.random() as you do this. random() returns a double value greater than or equal to 0.0 and less than 1.0. As an example, to set num to a random integer value between 1 and 10 inclusive is: num = (int) (Math.random() * 10) + 1;
toString() Return a string representation of a Solitaire object. To do this, simply: return piles.toString();
over() Return true if the solitaire is over, false otherwise. The solitaire is over when the piles have size 1, 2, 3, 4, 5, 6, 7, 8, and 9, in some order. (It can be shown that you always end up with such a configuration.)
round() In each round, you take one card from each pile, forming a new pile with these cards.