29-Arrays-And-ArrayLists

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

Info icon This 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(); } }
Image of page 1

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

View Full Document Right 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
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 ); point.setFillColor(color); return point; } private static final int NUM_DAYS = 75; private static final int PHYSICAL_PERIOD = 23; private static final int EMOTIONAL_PERIOD = 28; private static final int
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern