Guest User

Untitled

a guest
Oct 19th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. package numbers;
  2.  
  3. import java.util.*;
  4.  
  5. public class histogram {
  6.  
  7.     public static void main(String[] args)
  8.     {
  9.        
  10.         Random rand = new Random();
  11.         int[] myArray;
  12.         myArray = new int[50];
  13.        
  14.         for(int i = 0; i < myArray.length; i++)
  15.         {
  16.             myArray[i] = rand.nextInt(50) +1;
  17.         }
  18.         printArray(myArray);
  19.         checkMatches(myArray);
  20.         printHistogram(myArray);
  21.        
  22.     }
  23.  
  24.    
  25.     public static void printArray(int[] array)
  26.     {
  27.        
  28.         for (int i = 0; i < array.length; i++)
  29.         {
  30.             if(i%5==0)
  31.             {
  32.                 System.out.println();
  33.             }
  34.             System.out.printf("%5d", array[i]);
  35.         }
  36.         System.out.println();
  37.    
  38.     }
  39.    
  40.  
  41.    
  42.     public static void checkMatches(int[] array)
  43.     {
  44.         for (int x = 1; x < array.length+1; x++)
  45.         {
  46.             int matchCount = 0;
  47.             for(int i = 0; i < array.length; i++)
  48.             {
  49.                 if(array[i] == x)
  50.                 {
  51.                     matchCount++;
  52.                 }
  53.             }
  54.             System.out.println("There is " + matchCount + " instances of the number " + x);
  55.         }
  56.     }
  57.    
  58.     private static void printHistogram(int[] array) {
  59.          for (int i = 1; i < array.length; i++) {
  60.             String label = i + " : ";
  61.             System.out.println(label + convertToStars(array[i]));
  62.         }
  63.     }
  64.  
  65.     private static String convertToStars(int num) {
  66.         StringBuilder builder = new StringBuilder();
  67.         for (int j = 1; j < num; j++) {
  68.             builder.append('*');
  69.         }
  70.         return builder.toString();
  71.     }
  72.    
  73.    
  74.    
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment