Download presentation
Presentation is loading. Please wait.
Published byElwin Richards Modified over 9 years ago
1
CS 121 – Intro to Programming:Java - Lecture 9 Announcements Two new Owl assignments up - they’re a bit more challenging. Programming assignment 5 is due this week. Visit office hours this week to strut your stuff (Friday, 10-12, or 230ish-5, in LGRT 220 -- or come see me Friday afternoon). Late rules in effect - late programs reduced in value by half. Preregistration coming - what should you take? (191B? 187? Math 513?)
2
We’ve seen Java at two levels: the statement level - mechanisms for getting specific, often low-level jobs, done, e.g. assignment stmts, println, etc. the (class and) object level - mechanisms for modeling things (objects) according to an” Objects served by methods ” scheme: (objects: repositories of state) (methods: machinery for realizing behaviors) Now we’re back to a new and very important idea in statement-level thinking: arrays. Basically, arrays give us a new way to think about variables.
3
Think about: students in a classroom; seats on an airplane, rooms in a motel, positions in the deli line at a super-market. In all cases: Many variables required for representation There’s an indexing scheme for locating / identifying the variables in question Student 7 Seat 23B Room 201 Deli-line position 77 some indexing schemes are more natural than others some are two-dimensional
4
public class ArrayTest1{ public static void main(String[] args) { int[] firstArray = new int[10]; for(int j = 0; j < firstArray.length; j++) firstArray[j] = j*j; System.out.println("here they come"); for(int j = 0; j < firstArray.length; j++) System.out.println(firstArray[j]); } }
5
We’re going to write an application that rolls a pair of dice 10000 times and reports the results profile of the rolls (e.g. how many 2,3, etc came up. public class DiceTester{ public static void main(String[] args){ Dice d = new Dice(); d.multiToss(10000); d.showScoreboard(); } }
6
import java.util.Random; public class Dice{ Random r; int[] scoreboard = new int[13]; public Dice(){ r = new Random(); initializeScoreboard(); } public void initializeScoreboard(){ for(int j = 0; j < 13; j++) scoreboard[j] = 0;}
7
public int tossDie(){ return (1+r.nextInt(6)); } public int throwDice(){ return(tossDie() + tossDie()); } public void multiToss(int tossCount){ int score; for (int j = 0; j < tossCount; j++){ score = throwDice(); scoreboard[score]++; } }
8
public void showScoreboard(){ for(int j = 2; j < 13; j++) System.out.println("toss of " + j + " " + scoreboard[j]); }
9
Now suppose that we would like to see a bar graph of the contents of scoreboard. How should we proceed? This is big Tossing dice is a totally different activity from drawing a bar graph -- so we should capture this separation of functionality by creating a new class (a BarGraph class) that makes bar graphs when passed an integer array, Then we “marry” the two activites in a “master” class - here, a driver class.
10
public class DiceTester{ public static void main(String[] args){ Dice d = new Dice(); d.multiToss(10000); d.showScoreboard(); BarGraph b = new BarGraph(d.getScoreboard(),150,500,200); b.drawData(); } }
11
Problems how many bars? How wide? How far apart? how tall? -- That pesky scale factor -If the bars are to appear on a baseline at y = 150 in the drawing window, and the largest data item in the data is 1500, then the scaling factor is.1 = 1/10: -Every unit in the data array accounts for 1/10 of a pixel. -Yikes -Not as bad as it sounds: we’ll multiply every data element in the array by this scale factor, and then cast as an integer. -Example: if some other data item in the array is 955, then it will produce a rectangle of height (int) 95.5 = 95.
12
public class BarGraph{ int[] data; DrawingWindow d; float scaleFactor; // how tall should bars be? int base; // the floor of the bars int barWidth = 4; int spacer = 10; int windowWidth; Rect bar; public BarGraph(int[] data, int base, int width, int height){ d = new DrawingWindow(width,height,"graphing"); this.data = data; this.base = base; windowWidth = width; scaleFactor = (float)base / arrayMax(); }
13
public void drawData(){ int nwX,nwY,ht; for(int j =0; (j < data.length); j++){ nwX = (1 + j)*(barWidth + spacer); nwY = (int)(base - data[j]*scaleFactor); ht = (int)(data[j]*scaleFactor); bar = new Rect(nwX,nwY,barWidth,ht); bar.drawOn(d); } }
14
Palindromes are words, phrases, sentences that are the same forward and backward: Madam I’m Adam A man a plan a canal, Panama Live dirt up a side track carted is a putrid evil Able was I ere I saw Elba Remarkable was I ere I saw Elba, Kramer Doc note I dissent: a fast never prevents a fatness. I diet on cod. … And so forth [notice: we’re ignoring capitals, blanks, punctuation]
15
import element.*; public class PalTester{ public static void main(String[] args){ ConsoleWindow c = new ConsoleWindow(); c.out.println("enter a word or phrase"); String s = c.input.readLine(); Palindrome p = new Palindrome(s); System.out.println(" s a palindrome? " + p.palCheck()); } }
16
public class Palindrome{ String s; char[] letters; public Palindrome(String s){ letters = s.toCharArray(); } public boolean palCheck(){ int left, right; boolean ok = true; // innocent until proven guilty for (left = 0, right = letters.length-1; ok && (left < right); left++, right--) if (letters[left] != letters[right])ok = false; return ok; } }
17
Palindrome2 - handles upper/lower case, blanks.. public class Palindrome2{ String s; char[] letters; final char BLANK = ' '; public Palindrome2(String s){ letters = s.toLowerCase().toCharArray(); } Last statement 1)makes all letters lower case; then 2) turns the string into an array of chars.
18
public boolean palCheck(){ int left = 0; int right = letters.length-1;; boolean ok = true; // innocent until proven guilty while(ok && left < right){ if ( letters[left] == BLANK) left++; else if ( letters[right] == BLANK) right--; else if ( letters[left] == letters[right]){ left++; right--;} else ok = false; } return ok; }
19
Sorting Perhaps the single most important operation in computer science - includes alphabetizing, arranging by height, salary, years of service, and so forth. There’s a vast collection of sorting algorithms. We’ll talk about a particularly simple one. An upcoming Owl assignment discusses a fancy sorting technique embryonically.
20
static void selectionSort(int[] nums){ int min, temp; for(int index = 0; index < nums.length-1; index++){ min = index; for(int scan = index+1; scan < nums.length; scan++) if (nums[scan] < nums[min]) min = scan; temp = nums[min]; // swap smallest to index position nums[min] = nums[index]; nums[index] = temp; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.