CS99

Fundamental Programming Concepts
Summer 2001

 

Lab 10: Solutions


Histogram.java
 
import java.io.*;

/*---------------------------------------------------------------------------------------------    

Program reads in an arbitrary range of integers from the range 1 to 100 inclusive.  
Entering '0' signals that the user is finished inputting data.  A chart is then produced
that indicates how many input values fell in the ranges 1-10, 11-20, etc.
------------------------------------------------------------------------------------------------*/
class Histogram {
   
   static TokenReader in = new TokenReader(System.in);
   
   public static void main(String[] args)
   {            
       intro();
       double val = getInput(">");
       if (val !=0)
       {
           int[] freq = enterVals(val);
           printOutput(freq);
       }
       else
           bye();
   }
   
   /*---------------------------------------------------------------------------------------------    
   getInput
   
   input    :       message string
   return    :    value input by user
   
   Returns value of user's input if the value lies in the interval [0-100].
   ------------------------------------------------------------------------------------------------*/    
   private static double getInput(String message)
   {
       System.out.print(message);
       System.out.flush();
       double val = in.readDouble();
       while ((val < 0)||(val > 100))
       {
           System.out.print("Number must be [1-100] : ");
           System.out.flush();
           val = in.readDouble();
       }
       return val;
   }


   /*---------------------------------------------------------------------------------------------    
   enterVals
   
   input    :     double value from [1-100]
   return    :    int [10]
   
   Prompts user for values.  For each value from [1-100], adds 1 to the appropriate
   bin in the array.  Ends when user types 0.  Returns the array.
   ------------------------------------------------------------------------------------------------*/    
   private static int[] enterVals(double val)
   {
       int[] f = new int[10];
       while (val > 0)
       {
           f[(int) ((val - 0.5)/10)]++;
           val = getInput(">");
       }
       return f;
   }
   
   /*---------------------------------------------------------------------------------------------    
   printOutput
   
   input    :       int [10]
   return    :    none
   
   Prints a histogram of the values typed in by user.
   ------------------------------------------------------------------------------------------------*/    
   private static void printOutput(int[] f)
   {
       for (int i=1; i<=f.length; i++)
       {
           System.out.print("\n" + ((i==1) ? " " : ""+ (i-1)) + "1-" + i + "0| ");
           for (int j=1; j<=f[i-1]; j++)
               System.out.print("*");
           System.out.flush();
       }
       System.out.println();
       in.waitUntilEnter();
   }
   
   
   /*---------------------------------------------------------------------------------------------    
   bye
   
   input    :     none
   return    :    none
   
   Prints goodbye message when user elects not to play.
   ------------------------------------------------------------------------------------------------*/        
   private static void bye()
   {
       System.out.println("\nYou didn't enter any values!\nCan't make a histogram out of nothing now, can we?");
   }
   
   
   /*---------------------------------------------------------------------------------------------    
   intro
   
   input    :       none
   return    :    none
   
   Message to introduce the game
   ------------------------------------------------------------------------------------------------*/    
   private static void intro()
   {
       System.out.println("Welcome to the histogram program.\nPlease enter in a series of numbers from 1-100.");
       System.out.println("When finished, enter 0 to see your results.\n");
   }
   
}

/*
SAMPLE OUTPUT:

Welcome to the histogram program.
Please enter in a series of numbers from 1-100.
When finished, enter 0 to see your results.

>40
>42
>43
>44
>45
>10
>30
>89
>81
>83
>99
>0

1-10| *
11-20|
21-30| *
31-40| *
41-50| ****
51-60|
61-70|
71-80|
81-90| ***
91-100| *

Press Enter to continue.

*/

Arrays.java
 
class Arrays {

   public static void main( String[] args ) {
  
      /* Your testing code goes here*/

  
   }

 
   /*-------------------------------------------------------------------------------------
   rightShift()
     
   Returns a new array the same size as the input array, but whose elements 
   are the elements of the array shifted 1 position to the right. Elements falling off the right end
   are added at the left

   Example: 1 2 3 4 5 --> 5 1 2 3 4
   -------------------------------------------------------------------------------------- */
   public static int[] rightShift( int[] a ) {
      int[] b = new int[ a.length ];                       
      for( int i = 0; i < n; i++ )
            b[ i ] = a[ ( n - 1 + i )%n ];
      return b;
   }

 
   /*-------------------------------------------------------------------------------------
   Delete( int[], int )

   D
elete (array, element) takes an integer array 'array', and integer 'element', and returns
   new array consisting of all elements of 'array' except for occurrences of 'element'. The
   new array's  size should be the size of 'array' minus the number of elements removed.
  --------------------------------------------------------------------------------------- */
   public static int[] delete( int[] a, int k ) {
      /* Determine the number of times element k appears in the array */
      int numOfKs = 0;
      for( int i = 0; i < a.length; i++)
         if ( a[ i ] == k )
            numOfKs++;
   
      /* Create a new blank array shorter than the previous by the number of k's */
      int[] b = new int[ a.length - numOfKs ];
     
      /* Fill the new array with all the non-k elements from the first */
      for( int i = 0, j = 0; i < a.length; i++ )
            if ( a[ i ] != k )
                b[ j++ ] = a[ i ];
       

      return b;
   }  


   /*------------------------------------------------------------------------------------
   isPalindrome( char[] )

   isPalindrome(char[] array) takes an array of characters and returns true if the characters
   in  the array constitute a palindrome, false otherwise. A palindrome is the same
   forwards as backwards, e.g. 'a' 'b' 'c' 'b' 'a'.

   -------------------------------------------------------------------------------------*/
   public static boolean isPalindrome( char[] c ) {
      int i, n = array.length;
      /* compare left and right elements counting from both ends */
      for(  i = 0; i < n/2 && ( array[ i ] == array[ n - 1 - i ] ); i++ );
      /* if i == n/2 it means all elements in the array matched with each other */
      return i == n/2;
   }

 

}