GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — ThinkJavaCode/ch08/Histogram.java at master · dwynewilly/ThinkJavaCode Expand file tree/ Copy pathHistogram.javaMore file actions 59 lines (51 loc) · 1.45 KB / Copy pathHistogram.javaFile metadata and controls59 lines (51 loc) · 1.45 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 import java.util.Random; /** * Example code related to histograms. */ public class Histogram { /** * Returns an array of random integers. */ public static int[] randomArray(int size) { Random random = new Random(); int[] a = new int[size]; for (int i = 0; i < a.length; i++) { a[i] = random.nextInt(100); } return a; } /** * Computes the number of array elements in [low, high). */ public static int inRange(int[] a, int low, int high) { int count = 0; for (int i = 0; i < a.length; i++) { if (a[i] >= low && a[i] < high) { count++; } } return count; } public static void main(String[] args) { int numValues = 8; int[] array = randomArray(numValues); ArrayExamples.printArray(array); int[] scores = randomArray(30); int a = inRange(scores, 90, 100); int b = inRange(scores, 80, 90); int c = inRange(scores, 70, 80); int d = inRange(scores, 60, 70); int f = inRange(scores, 0, 60); // making a histogram int[] counts = new int[100]; for (int i = 0; i < scores.length; i++) { int index = scores[i]; counts[index]++; } // histogram with enhanced for loop counts = new int[100]; for (int score : scores) { counts[score]++; } } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |