Facebook
From Tristan Louth-Robins, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 46
  1. // Run program
  2. void setup() {
  3. size(200, 150);
  4. background(200, 255, 220);
  5.  
  6. ArrayList<Integer> newList = RNG(40);
  7.  
  8. displayResults(sortHeights(newList), heightSum(newList), heightMean(newList));
  9.  
  10. save("activity22.png");
  11.  
  12. }
  13.  
  14. // Function 1: (Psuedo) Randomly generate a list of heights between 100 to 200
  15. ArrayList<Integer> RNG(int number){  
  16.   ArrayList<Integer> heightList = new ArrayList<Integer>();
  17.     for(int i = 0; i < number; i++){
  18.       heightList.add((int)round(random(100, 200)));
  19.     }
  20.    return heightList;
  21. }
  22.  
  23. // Function 2: Sort the list into 3 categories (static list.)
  24. int[] sortHeights(ArrayList<Integer> heightList){
  25.   int[] heightCategory = new int[3];  
  26.   for(int i = 0; i < heightList.size(); i++){
  27.     if(heightList.get(i) < 134){
  28.       heightCategory[0]++;
  29.     } else if(heightList.get(i) < 167){
  30.       heightCategory[1]++;
  31.     } else {
  32.       heightCategory[2]++;
  33.     }
  34.   }
  35.   return heightCategory;
  36. }
  37.  
  38. // Function 3: Calculate the sum of all heights
  39. int heightSum(ArrayList<Integer> newList){
  40.   int sum = 0;
  41.   for(int i = 0; i < newList.size(); i++){    
  42.     sum = sum + newList.get(i);
  43.   }
  44.   return sum;
  45. }
  46.  
  47. // Function 4: Calculate the mean of all heights
  48. float heightMean(ArrayList<Integer> newList){
  49.   float mean = 0;
  50.   for(int i = 0; i < newList.size(); i++){
  51.     mean = mean + newList.get(i) / newList.size();
  52.   }
  53.   return mean;
  54. }
  55.  
  56.  
  57. // Function 5: Display results on the canvas.
  58. void displayResults(int[] sortHeights, int sum, float mean){
  59.   fill(0);
  60.   textSize(14);
  61.   text("Height 100-133:     " + sortHeights[0], 10, 20);
  62.   text("Height 133-166:     " + sortHeights[1], 10, 40);
  63.   text("Height 166-200:     " + sortHeights[2], 10, 60);
  64.   line(10, 70, 175, 70);
  65.   text("Sum of all heights: " + sum, 10, 90);
  66.   text("Mean height:        " + mean, 10, 110);
  67. }