


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Should an employee's address be represented as a set of instance variables or as an Address object?
It may be unnecessary to create separate classes for each type of appliance in a house It may be sufficient to define a more general Appliance class with appropriate instance data It all depends on the details of the problem being solved
// SnakeEyes.java Demonstrates the use of a programmer-defined class. //******************************************************************** public class SnakeEyes { //----------------------------------------------------------------- // Creates two Die objects and rolls them several times, counting // the number of snake eyes that occur. //----------------------------------------------------------------- public static void main (String[] args) { final int ROLLS = 500; int num1, num2, count = 0; Die die1 = new Die(); Die die2 = new Die(); for (int roll=1; roll <= ROLLS; roll++) { num1 = die1.roll(); num2 = die2.roll(); if (num1 == 1 && num2 == 1) // check for snake eyes count++; } System.out.println ("Number of rolls: " + ROLLS); System.out.println ("Number of snake eyes: " + count); System.out.println ("Ratio: " + (float)count / ROLLS); } }
// Die.java Java Foundations // Represents one die (singular of dice) with faces showing values // between 1 and 6. //******************************************************************** public class Die { private final int MAX = 6; // maximum face value private int faceValue; // current value showing on the die // Constructor: Sets the initial face value of this die. //----------------------------------------------------------------- public Die() { faceValue = 1; } // Computes a new face value for this die and returns the result. //----------------------------------------------------------------- public int roll() { faceValue = (int)(Math.random() * MAX) + 1; return faceValue; } (more…)
// Face value mutator. The face value is not modified if the // specified value is not valid. //----------------------------------------------------------------- public void setFaceValue (int value) { if (value > 0 && value <= MAX) faceValue = value; } //----------------------------------------------------------------- // Face value accessor. //----------------------------------------------------------------- public int getFaceValue() { return faceValue; } //----------------------------------------------------------------- // Returns a string representation of this die. //----------------------------------------------------------------- public String toString() { String result = Integer.toString(faceValue); return result; } }
// FlipRace.java Java Foundations public class FlipRace { // Flips two coins until one of them comes up heads three times in a row. public static void main (String[] args) { final int GOAL = 3; int count1 = 0, count2 = 0; Coin coin1 = new Coin(), coin2 = new Coin(); while (count1 < GOAL && count2 < GOAL) { coin1.flip(); coin2.flip(); System.out.println ("Coin 1: " + coin1 + "\tCoin 2: " + coin2); // Increment or reset the counters count1 = (coin1.isHeads())? count1+1 : 0; count2 = (coin2.isHeads())? count2+1 : 0; } if (count1 < GOAL) System.out.println ("Coin 2 Wins!"); else if (count2 < GOAL) System.out.println ("Coin 1 Wins!"); else System.out.println ("It's a TIE!"); } }