// Question #1 on the 2011 AP Exam // Arrays // ====== // No Methods // Use [index] // new? // String [] cats = new String[20]; // cats[0] = "Tom"; // cats.length // int [] numbers = new int[10]; // numbers[0] = 98; // numbers.length // Horse [] horses = new Horse[10]; // Access // horses[0] = new Horse("Silver", 15); // Things you should do: // READ the question 2 or 3 times // Make notes, circle and/or box things // Circle your instance variables // Box off the method to write and note the pre and post conditions // Draw pictures or draw on their pictures // Think first and understand, then write your code. // MAKE SURE THAT YOU WRITE YOUR CODE IN THE CORRECT PLACE import java.util.*; public class Sound { private int [] samples; public Sound(int [] samples) { this.samples = samples; } // ****************************** // ****************************** // ****************************** // part A on AP exam // ****************************** // ****************************** // ****************************** public int limitAmplitude(int limit) { // change sound value > limit to limit // change sound value < -limit to -limit // return the number of elements changed // DON'T USE A FOR EACH LOOP - YOU CANNOT REMOVE OR ADD elements // You also don't have an index, so what's the point int count = ?; // number of elements changed for (int sample : samples) { // variable sample receives a int each time // through the loop // you cannot access samples[someIndex] // samples[?] = limit; } return ?????; // number of elements changed } public String toString() { return Arrays.toString(samples); } public static final void main(String [] args) { System.out.println("Sound"); System.out.println(); System.out.println(); int [] originalSamples1 = {40,2532,17,-2300,-17, -4000,2000,1048,-420,33,15,-32,2030,3223}; Sound mySound = new Sound(originalSamples1); System.out.println("Original"+mySound); System.out.println(); System.out.println(); int x = mySound.limitAmplitude(2000); System.out.println("Sound elements changed = "+x); System.out.println(); System.out.println(); System.out.println("Limit "+mySound); System.out.println(); System.out.println(); int [] originalSamples2 = {0,0,0,40,2532,17,-2300,-17, -4000,2000,1048,-420,33,15,-32,2030,3223}; mySound = new Sound(originalSamples2); System.out.println("Original"+mySound); System.out.println(); System.out.println(); mySound.trimSilenceFromBeginning(); System.out.println("Silence "+mySound); System.out.println(); System.out.println(); System.out.println(); System.out.println(); } }