Building Java Programs

Slides:



Advertisements
Similar presentations
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Advertisements

1 Various Methods of Populating Arrays Randomly generated integers.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-2: Strings reading: 3.3, self-check: Ch. 4 #12, 15 exercises:
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Designing Algorithms Csci 107 Lecture 4.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Tallying and Traversing Arrays reading: 7.1 self-checks: #1-9 videos:
Multi-Dimensional Arrays Rectangular & Jagged Plus: More 1D traversal.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Tallying and Traversing Arrays reading: 7.1 self-checks: #1-9 videos:
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 4.3, 7.6.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
1 CSC 222: Object-Oriented Programming Spring 2012 Object-oriented design  example: word frequencies w/ parallel lists  exception handling  System.out.format.
1 Text Processing. 2 Type char char : A primitive type representing single characters. –A String is stored internally as an array of char String s = "Ali.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 5/27/20161.
Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 7.6, 4.3.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
1 Traversal algorithms. 2 Array traversal traversal: An examination of each element of an array. Traversal algorithms often takes the following form:
1 Fencepost loops suggested reading: The fencepost problem Problem: Write a static method named printNumbers that prints each number from 1 to a.
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 7.1, 4.4 self-checks: #1-9.
The median again The steps of our algorithm: Read the size of the list, N. Declare and instantiate an array of integers, "list". Read the elements of list.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
BUILDING JAVA PROGRAMS CHAPTER 7: ARRAYS 1. 2 OUT-OF-BOUNDS INDEXES The indexes that are legal to access in an array are those in the range of 0 to the.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 17: Arrays for Tallying; Text Processing reading: 4.3, 7.6 (Slides adapted.
Topic 23 arrays - part 3 (tallying, text processing) Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
CSE 143 Lecture 11: Sets and Maps reading:
Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-3: Strings, char reading: 3.3, 4.3.
7 - Programming 7J, K, L, M, N, O – Handling Data.
CS 160 – Summer 16 Exam 1 Prep.
Building Java Programs Chapter 4
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Building Java Programs
Conditional Execution
Building Java Programs Chapter 4.3
CSc 110, Autumn 2017 Lecture 24: Lists for Tallying; Text Processing
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Array traversals, text processing
Building Java Programs
Building Java Programs
CS 177 Week 15 Recitation Slides
Java for Teachers Intermediate
Building Java Programs
Building Java Programs
Lecture 7-3: Arrays for Tallying; Text Processing
Topic 23 arrays - part 3 (tallying, text processing)
Building Java Programs
Why did the programmer quit his job?
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
Building Java Programs
Arrays.
Building Java Programs
Building Java Programs
Array basics Readings: 7.1.
Building Java Programs
Suggested self-checks: Section 7.11 #1-11
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Why are arrays useful? We can use arrays to store a large amount of data without declaring many variables. Example: Read in a file of 1000 numbers, then.
Building Java Programs
Building Java Programs
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
A counting problem Problem: Write a method mostFrequentDigit that returns the digit value that occurs most frequently in a number. Example: The number.
Building Java Programs
More on iterations using
Presentation transcript:

Building Java Programs Chapter 7 Lecture 7-3: Tallying and Traversing Arrays, Strings reading: 7.1, 4.4 self-checks: #1-9 videos: Ch. 7 #4

Exercise: Triangle Math Write a program that lets the user play one game of Triangle Math Player adds adjacent numbers until one number reached E.g. 5 + 4 = 9, 4 + 1 = 5; 9 + 5 = 14 Prompt the user for the number of tiers to use before playing # of tiers? 3 5 4 1 + + + 14

Arrays for counting and tallying reading: 7.1 self-checks: #8

A multi-counter problem Problem: Examine a large integer and count the number of occurrences of every digit from 0 through 9. Example: The number 229231007 contains: two 0s, one 1, three 2s, one 7, and one 9. We could declare 10 counter variables for this... int counter0, counter1, counter2, counter3, counter4, counter5, counter6, counter7, counter8, counter9; Yuck!

A multi-counter problem A better solution is to use an array of size 10. The element at index i will store the counter for digit value i. for integer value 229231007, our array should store: The index at which a value is stored has meaning. Sometimes it doesn't matter. What about the weather case? index 1 2 3 4 5 6 7 8 9 value

Creating an array of tallies int num = 229231007; int[] counts = new int[10]; while (num > 0) { // pluck off a digit and add to proper counter int digit = num % 10; counts[digit]++; num = num / 10; } index 1 2 3 4 5 6 7 8 9 value

Array traversals, text processing reading: 7.1, 4.4 self-check: Ch. 7 #8, Ch. 4 #19-23

Text processing text processing: Examining, editing, formatting text. Often involves for loops to break up and examine a String Examples: Count the number of times 's' occurs in a file Find which letter is most common in a file Count A, C, T and Gs in Strings representing DNA strands

Strings as arrays Strings are represented internally as arrays. Each character is stored as a value of primitive type char. Strings use 0-based indexes, like arrays. We can write algorithms to traverse Strings. Example: String str = "Ali G."; index 1 2 3 4 5 value 'A' 'l' 'i' ' ' 'G' '.'

Recall: type char char: A primitive type representing a single character. Values are surrounded with apostrophes: 'a' or '4' or '\n' Access a string's characters with its charAt method. String word = console.next(); char firstLetter = word.charAt(0); if (firstLetter == 'c') { System.out.println("That's good enough for me!"); }

char vs. String 'h' and "h" have different types char values are primitive; you can't call methods on them char c = 'h'; c.toUpperCase(); // ERROR: "char cannot be dereferenced" Strings are objects; they contain methods String s = "h"; int len = s.length(); // 1 char first = s.charAt(0); // 'h'

String traversals traversal: An examination of each element of an array. for (int i = 0; i < array.length; i++) { do something with array[i]; } Use for loops to examine each character. for (int i = 0; i < string.length(); i++) { do something with string.charAt(i);

Text processing question Write a method tallyVotes that accepts a String parameter and prints the number of McCain, Obama and independent voters. // (M)cCain, (O)bama, (I)ndependent String voteText = "MOOOOOOMMMMMOOOOOOMOMMIMOMMIMOMMIO"; tallyVotes(voteText); Output: Votes: [16, 14, 3]

Text processing answer public static int[] tallyVotes(String votes) { int[] tallies = new int[3]; // M -> 0, O -> 1, I -> 2 for(int i = 0; i < votes.length(); i++) { if(votes.charAt(i) == 'M') { tallies[0]++; } else if(votes.charAt(i) == 'O') { tallies[1]++; } else { // votes.charAt(i) == 'I' tallies[2]++; } System.out.println("Votes: " + Arrays.toString(tally));;