Facebook
From Adjowa Asamoah, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 153
  1. ArrayList<Integer> temperatures = new ArrayList<Integer>(); // Dynamic array to store temperatures
  2. ArrayList<Integer> occurrences = new ArrayList<Integer>(); // Dynamic array to store occurrences of temperatures (4 to 20)
  3. int sum = 0; // Variable to store the sum of temperatures
  4. int cellWidth = 100; // Width of each cell in the table
  5. int cellHeight = 40; // Height of each cell in the table
  6.  
  7. void setup() {
  8.   size(800, 600);
  9.   int sizeOfArray = 10; // Define the size of the array (change as needed)
  10.  
  11.   // Initialize occurrences ArrayList with zeros
  12.   for (int i = 0; i <= 17; i++) {
  13.     occurrences.add(0);
  14.   }
  15.  
  16.   // Populate the array with random integers between 4 and 20
  17.   for (int i = 0; i < sizeOfArray; i++) {
  18.     int temp = (int) random(4, 21); // Generate random integer between 4 and 20
  19.     temperatures.add(temp); // Add temperature to the ArrayList
  20.     sum += temp; // Calculate sum
  21.     occurrences.set(temp - 4, occurrences.get(temp - 4) + 1); // Update occurrences array
  22.   }
  23.  
  24.   // Display the table on the canvas
  25.   background(255);
  26.   fill(0);
  27.   textAlign(CENTER, CENTER);
  28.  
  29.   // Draw table headers
  30.   text("Temperature", width/4 - cellWidth/2, 50);
  31.   text("Occurrences", 3*width/4 - cellWidth/2, 50);
  32.  
  33.   // Draw the temperatures and occurrences in the table
  34.   for (int temp = 4; temp <= 20; temp++) {
  35.     int rowY = 100 + (temp - 4) * cellHeight;
  36.     text(temp, width/4 - cellWidth/4, rowY);
  37.     text(occurrences.get(temp - 4), 3*width/4 - cellWidth/4, rowY);
  38.   }
  39.  
  40.   // Draw horizontal lines
  41.   for (int temp = 4; temp <= 20; temp++) {
  42.     int rowY = 100 + (temp - 4) * cellHeight;
  43.     line(width/4 - cellWidth/2, rowY + cellHeight/2, width/4 + cellWidth/2, rowY + cellHeight/2);
  44.     line(3*width/4 - cellWidth/2, rowY + cellHeight/2, 3*width/4 + cellWidth/2, rowY + cellHeight/2);
  45.   }
  46.  
  47.   // Draw vertical lines
  48.   line(width/2, 50, width/2, height);
  49.   line(width/4 - cellWidth/2, 50, width/4 - cellWidth/2, height);
  50.   line(3*width/4 - cellWidth/2, 50, 3*width/4 - cellWidth/2, height);
  51.  
  52.   // Display the sum of temperatures
  53.   text("Sum of temperatures: " + sum, width/2, height - 50);
  54.  
  55.   save("Activity19.png");
  56. }
  57.  
  58.