A Java Course Outline
Using the Java By Dissection book
by Ira Pohl and Charlie McDowell

 



Solution to Homework 5 - The Zodiac

Excellent program! Very simple solution, clean code, good comments, and great variable names.
/* ZodiacExample.java  Christine Dunham  
 *   
 * The purpose of this program is to display the probability of two people 
 * at a party having the same zodiac sign.  This will be done for parties of 
 * 2 to 12 people.
 */


public class ZodiacExample {   
   public static void main (String[] args) 
   {      
      int people = 12;
      float probability, totalProbability, numOfSameSigns;
      
      // do for 2 to 12 people
      for (int i = 2; i <= 12; i++) 
      {         
         numOfSameSigns = 0.0f;
         
         // do for 100 trials   
         for (int x = 1; x <= 100; x++)   
         {
            if(isSameSign(i))
               numOfSameSigns++;
         
         }//end of 100 trials loop
         
         probability = (float)(numOfSameSigns/100.0f);               

         System.out.println("For 100 trials of " + i + " people, the probability of two people");
         System.out.println("having the same zodiac sign is " + probability + ".");         
      }//end of number of people loop   
   
   }//end of main         
         
   public static boolean isSameSign(int peopleAtParty)
   {         
      int aries = 0, taurus = 0, gemini = 0, cancer = 0;
      int leo = 0, virgo = 0, libra = 0, scorpio = 0, sagittarius = 0;
      int capricorn = 0, aquarius = 0, pisces = 0, sign;      
      for(int i = 1; i <= peopleAtParty; i++)      
      {   sign = (int)(Math.random() * 12);                  
         
         switch (sign)
         {
            case 0: 
               aries++; 
               break;
            case 1: 
               taurus++; 
               break;
            case 2: 
               gemini++;
                break;
            case 3: 
               cancer++; 
               break;
            case 4: 
               leo++; 
               break;
            case 5: 
               virgo++;
               break;
            case 6: 
               libra++; 
               break;
            case 7: 
               scorpio++; 
               break;
            case 8: 
               sagittarius++;   
               break;
            case 9: 
               capricorn++; 
               break;
            case 10:
               aquarius++; 
               break;
            case 11: 
               pisces++; 
               break;
                  
         }// end of switch
            
      }//end of loop      
            
      if (aries >= 2)
         return true;
      if (taurus >= 2)
         return true;
      if (gemini >= 2)
         return true;
      if (cancer >= 2)
         return true;
      if (leo >= 2)
         return true;
      if (virgo >= 2)
         return true;
      if (libra >= 2)
         return true;
      if (scorpio >= 2)
         return true;
      if (sagittarius >= 2)
         return true;
      if (capricorn >= 2)
         return true;
      if (aquarius >= 2)
         return true;
      if (pisces >= 2)
         return true;         
      return false;
      
   }// end of isSameSign method   
   
} // end of class 

Feel free to report any site problems to the Webmaster, Debra Dolsberry.