Download presentation
Presentation is loading. Please wait.
Published byBrook Richards Modified over 9 years ago
1
CS 121 – Intro to Programming:Java - Lecture 8 Announcements Current Owl assignment is due Wednesday. Next Owl assignment - arrays I - will be up by tomorrow morning Programming assignment 5 is up, due next week. Hand in protocol will be announced on-line tomorrow afternoon. Late rules in effect - late programs are reduced in value by half. Preregistration coming - what should you take?
2
Get going on program five! Here’s one thing you can do…the bouncing ball example.. Form your groups.. Email me your ideas, etc..
3
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” (repositories of state) served by methods (machinery for realizing behaviors) scheme 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.
4
Think about: students in a class; 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
5
public class ArrayTest1{ public static void main(String[] args) { int[] firstArray = new int[10]; for(int j = 0; j < 10; j++) firstArray[j] = j*j; System.out.println("here they come"); for(int j = 0; j < 10; j++) System.out.println(firstArray[j]); } }
6
public class BasicArray { final static int LIMIT = 15; final static int MULTIPLE = 10; public static void main (String[] args) { int[] list = new int[LIMIT]; for (int index = 0; index < LIMIT; index++) list[index] = index * MULTIPLE; list[5] = 999; // change one array value for (int index = 0; index < LIMIT; index++) System.out.print (list[index] + " "); }
7
public class Test{ // all characters have a position in Unicode // Ascii comes first : 0 - 127 // when an integer op is applied to a character, it // is automatically converted into an integer public static void main(String[] args){ int i,j; i = (int) 'a'; j = (int) 'A'; System.out.println(i + " " + j); System.out.println('a' - 'A'); System.out.println('e' - 'a');} } ´œ ----jGRASP exec: java Test œœßœ97 65 œœßœ32 œœßœ4
8
public class LetterCount{ //----------------------------------------------------------------- // Reads a sentence from the user and counts the number of // uppercase and lowercase letters contained in it. //----------------------------------------------------------------- public static void main (String[] args) { ConsoleWindow c = new ConsoleWindow(50,40, "my window"); final int NUMCHARS = 26; int[] upper = new int[NUMCHARS]; int[] lower = new int[NUMCHARS]; char current; // the current character being processed int other = 0; // counter for non-alphabetics c.out.println ("Enter a sentence:"); String line = c.input.readLine(); // reads line c.out.println("line length " + line.length());
9
// Count the number of each letter occurence for (int ch = 0; ch = 'A' && current = 'a' && current <= 'z') lower[current-'a']++; else other++; }
10
// Print the results c.out.println (); for (int letter=0; letter < upper.length; letter++) { c.out.print ( (char) (letter + 'A') ); c.out.print (": " + upper[letter]); c.out.print ("\t\t" + (char) (letter + 'a') ); c.out.println (": " + lower[letter]); } c.out.println (); c.out.println ("Non-alphabetic characters: " + other); } }
12
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(); } }
13
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;}
14
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]++; } }
15
public void showScoreboard(){ for(int j = 2; j < 13; j++) System.out.println("toss of " + j + " " + scoreboard[j]); }
16
Results: œœœ œœ´œ ----jGRASP exec: java DiceTester œœßœtoss of 2 303 œœßœtoss of 3543 œœßœtoss of 4 807 œœßœtoss of 5 1123 œœßœtoss of 6 1432 œœßœtoss of 7 1630 œœßœtoss of 8 1389 œœßœtoss of 9 1129 œœßœtoss of 10 808 œœßœtoss of 11 557 œœßœtoss of 12 279 œœßœ œœ©œ ----jGRASP
17
import element.*; public class PolyTester{ public static void main(String[] args){ ConsoleWindow c = new ConsoleWindow(); DrawingWindow d = new DrawingWindow(500,200); Pt[] poly = new Pt[5]; int x, y; c.out.println("enter 5 pts - 10 ints - separated by CRs"); for(int j = 0; j < 5; j++) { x = c.input.readInt(); y = c. input.readInt(); poly[j] = new Pt(x,y); } for(int j = 1; j < 5; j++) { L = new Line(poly[j-1], poly[j]); L.drawOn(d); } L = new Line(poly[4],poly[0]); // final connector L.drawOn(d); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.