// Car.java -- the object of the game is to win the car by guessing // its price. This class will define a car of a particular price. import java.util.*; public class Car { private int [] digit; private BagArrayList bag; private String showString; private String correct; // The price of the car should be 10,000-40,000 and with distinct digits. public Car() { showString = "_____"; correct = ""; bag = new BagArrayList(); Random gen = new Random(); digit = new int[5]; digit[0] = gen.nextInt(3) + 1; for (int i = 1; i < 5; ++i) { boolean found = false; do { digit[i] = gen.nextInt(10); found = false; for (int j = 0; j < i; ++j) if (digit[j] == digit[i]) found = true; } while(found); } // put the digits and the 3 strikes in the bag for (int i = 0; i < 5; ++i) { bag.add(new String("" + digit[i])); correct += digit[i]; } bag.add(new String("X")); bag.add(new String("X")); bag.add(new String("X")); } public BagArrayList getBag() { return bag; } public String toString() { return showString; } public String getCorrect() { return correct; } // No one outside this file should know the price, but we should // provide a way to verify if a particular guess of a digit is correct. // Reset the show string to reveal newest digit if correct. public boolean isCorrect(int position, int value) { boolean result = digit[position - 1] == value; if (result) showString = showString.substring(0, position - 1) + value + showString.substring(position); return result; } }