29-Arrays-And-ArrayLists

29-Arrays-And-ArrayLists - CS106A Handout 29 May 4th, 2010...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106A Handout 29 Spring 2011 May 4 th , 2010 Arrays and ArrayLists Here’s a short program that prompts the user to enter a sentence and then uses an array (of length 26) to compile and publish a case-insensitive ASCII histogram of all that sentence’s letters. public class Histogram extends ConsoleProgram { public void run() { setFont("Courier-BOLD-18"); println("Enter lines of text and we'll present their character histograms."); while ( true ) { String line = readLine("Enter a line of text: "); if (line.length() == 0) break ; int [] counts = buildHistogram(line); presentHistogram(counts); } } private int [] buildHistogram(String line) { int [] counts = new int [26]; for ( int i = 0; i < line.length(); i++) { char ch = line.charAt(i); if (Character. isLetter (ch)) { int offset = Character. toUpperCase (ch) - 'A'; counts[offset]++; } } return counts; } private void presentHistogram( int [] counts) { println("Here's a list its letters, and how often they appear:"); println(); for ( int i = 0; i < counts.length; i++) { if (counts[i] > 0) { char ch = ( char ) ('A' + i); println(" " + ch + ": " + counts[i]); } } println(); } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Biorhythms The following program draws the picture to the right, emulating the highs and lows of our physical, emotional, and intellectual biorhythms from the day we’re born. We have our good days and our bad one, but biorhythm theory states that we oscillate between physically strong and weak and then strong again with a period of 23 days, that we undulate between emotionally stable and emotionally fragile with a period of 28 days, and that we swing back and forth between smart and brilliant with a period of 33 days. The drawing above benefits from about ability to take discrete sine wave samples and store them in arrays—arrays that model the biowaves. public class Biorhythms extends GraphicsProgram { public void run() { double [] physicalWave = createWave( PHYSICAL_PERIOD , NUM_DAYS ); double [] emotionalWave = createWave( EMOTIONAL_PERIOD , NUM_DAYS ); double [] intellectualWave = createWave( INTELLECTUAL_PERIOD , NUM_DAYS ); waitForClick(); drawWave(physicalWave, Color. BLUE ); drawWave(emotionalWave, Color. RED ); drawWave(intellectualWave, Color. GREEN ); } private double [] createWave( int period, int samples) { double [] wave = new double [samples]; for ( int day = 0; day < wave.length; day++) { double angle = 2 * Math. PI * day / period; wave[day] = Math. sin (angle); } return wave; } 2
Background image of page 2
private void drawWave( double [] wave, Color color) { double stepWidth = ( double ) getWidth() / (wave.length - 1); int windowCenterY = getHeight()/2; for ( int day = 0; day < wave.length; day++) { int cx = ( int ) (day * stepWidth); int cy = ( int ) ((1 - 0.9 * wave[day]) * windowCenterY); add(createSamplePoint(cx, cy, SAMPLE_POINT_RADIUS , color)); } } private GObject createSamplePoint( int cx, int cy, int radius, Color color) { GOval point = new GOval(cx - radius, cy - radius, 2 * radius, 2 * radius); point.setFilled( true );
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

29-Arrays-And-ArrayLists - CS106A Handout 29 May 4th, 2010...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online