Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. /**
  2.  * <p>Title: Histogram</p>
  3.  *
  4.  * <p>Description: Histogram Drawing Class</p>
  5.  *
  6.  * <p>Copyright: Copyright (c) 2009</p>
  7.  *
  8.  * <p>Company: UMB</p>
  9.  *
  10.  * @author Bob Wilson
  11.  * @version 1.0
  12.  */
  13.  
  14. public class Histogram
  15. {
  16.   private int [] values;
  17.   private int minIndex;
  18.   private int maxIndex;
  19.   private int maxLength;
  20.  
  21.   /** constructor for histogram drawing class
  22.     * @param values: the array of integer values to draw
  23.     * @param minIndex: the lowest index in the array for the range to draw
  24.     * @param maxIndex: the highest index in the array for the range to draw
  25.     * @param maxLength: the length of line to draw for the largest value
  26.     */
  27.  
  28.   public Histogram(int [] values, int minIndex, int maxIndex, int maxLength)
  29.   {
  30.     // initialize the values of the instance variables from the constructor parameters
  31.     this.values = new int [maxIndex + 1];   // allocate memory for a copy of the values array
  32.     this.minIndex = minIndex;
  33.     this.maxIndex = maxIndex;
  34.     this.maxLength = maxLength;
  35.  
  36.     // step 6:
  37.     // find largest number in values array for scaling length of bars
  38.     int maxValue=0;
  39.     for (int i=0; i <= values.length-1; i++){
  40.       if (values[i]>maxValue)
  41.         maxValue=values[i];
  42.     }
  43.  
  44.     // step 7:
  45.     // copy data from values array to this.values array while scaling to maximum length of bars
  46.  
  47.     for(int i=0; i<=values.length-1; i++){
  48.       this.values[i] = (int)((double)(values[i]*maxLength)/maxValue);
  49.     }
  50.   }
  51.  
  52.   /** draw a horizontal bar graph
  53.     */
  54.  
  55.   public void drawHor()
  56.   {
  57.     // step 8:
  58.     // draw horizontal bar graph (one line per roll value)
  59.     for (int i=2; i <= maxIndex; i++){
  60.       System.out.print ("Value " + i + ": ");
  61.       for (int z=0; z <= values.length-1; z++){
  62.         if (values[i] == z)
  63.           System.out.println(" " + values[i]);
  64.         else
  65.           System.out.print("*");
  66.       }
  67.     }
  68.   }
  69.  
  70.   /** draw a vertical bar graph
  71.     */
  72.  
  73.   public void drawVer()
  74.   {
  75.  
  76.     // step 9:
  77.     // draw vertical bar graph (one column per roll value)
  78.  
  79.    
  80.    
  81.    
  82.    
  83.    
  84.    
  85.    
  86.    
  87.    
  88.    
  89.    
  90.    
  91.  
  92.    
  93.    
  94.    
  95.    
  96.    
  97.   }
  98. }/*201040*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement