Building Java Programs

Slides:



Advertisements
Similar presentations
1 Various Methods of Populating Arrays Randomly generated integers.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: File Output; Reference Semantics reading: , 7.1, 4.3, 3.3 self-checks:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
Building Java Programs Chapter 7 Arrays. 2 Can we solve this problem? Consider the following program (input underlined): How many days' temperatures?
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:
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.
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.
1 Reference semantics. 2 A swap method? Does the following swap method work? Why or why not? public static void main(String[] args) { int a = 7; int b.
CS 112 Introduction to Programming Reference Semantics; 2D Arrays; Array as State Yang (Richard) Yang Computer Science Department Yale University 308A.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
1 BUILDING JAVA PROGRAMS CHAPTER 7 LECTURE 7-3: ARRAYS AS PARAMETERS; FILE OUTPUT.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
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:
Building Java Programs Chapter 7
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.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
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
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
1 Building Java Programs Chapter 7: Arrays. 2 Chapter outline array basics declaring and initializing an array getting and setting values of elements.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
CSCI 162 – Introduction to Programming II William Killian
Building Java Programs
Building Java Programs
CSc 110, Autumn 2017 Lecture 24: Lists for Tallying; Text Processing
Building Java Programs
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Array traversals, text processing
CSC 142 Computer Science II
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
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
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs Chapter 7
Adapted from slides by Marty Stepp and Stuart Reges
slides created by Marty Stepp and Alyssa Harding
CSc 110, Spring 2017 Lecture 20: Lists for Tallying; Text Processing
slides created by Ethan Apter
Building Java Programs
Building Java Programs
Array basics Readings: 7.1.
Building Java Programs
Building Java Programs
Building Java Programs
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
Presentation transcript:

Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 7.6, 4.3

A multi-counter problem Problem: Write a method mostFrequentDigit that returns the digit value that occurs most frequently in a number. Example: The number 669260267 contains: one 0, two 2s, four 6es, one 7, and one 9. mostFrequentDigit(669260267) returns 6. If there is a tie, return the digit with the lower value. mostFrequentDigit(57135203) returns 3.

A multi-counter problem We could declare 10 counter variables ... int counter0, counter1, counter2, counter3, counter4, counter5, counter6, counter7, counter8, counter9; But that makes our code really long (and kind of redundant): int digit = n % 10; if (digit == 0) { counter0++; } else if (digit == 1) { counter1++; } else if (digit == 2) { counter2++; } else if (digit == 3) { ... } What we really want is something like counter<digit>++. How do we build such an array? And how does it help?

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. Example for 669260267: How do we build such an array? And how does it help? index 1 2 3 4 5 6 7 8 9 value

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

Tally solution // Returns the digit value that occurs most frequently in n. // Breaks ties by choosing the smaller value. public static int mostFrequentDigit(int n) { int[] counts = new int[10]; while (n > 0) { int digit = n % 10; // pluck off a digit and tally it counts[digit]++; n = n / 10; } // find the most frequently occurring digit int bestIndex = 0; for (int i = 1; i < counts.length; i++) { if (counts[i] > counts[bestIndex]) { bestIndex = i; return bestIndex;

Array histogram question Given a file of integer exam scores, such as: 82 66 79 63 83 Write a program that will print a histogram of stars indicating the number of students who earned each unique exam score. 85: ***** 86: ************ 87: *** 88: * 91: **** If you have time: Curve the scores; add a fixed number to each score. (But don't allow a curved score to exceed the max of 100.)

Array histogram answer // Reads a file of test scores and shows a histogram of the score distribution. import java.io.*; import java.util.*; public class Histogram { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("midterm.txt")); int[] counts = new int[101]; // counters of test scores 0 - 100 while (input.hasNextInt()) { // read file into counts array int score = input.nextInt(); counts[score]++; // if score is 87, then counts[87]++ } for (int i = 0; i < counts.length; i++) { // print star histogram if (counts[i] > 0) { System.out.print(i + ": "); for (int j = 0; j < counts[i]; j++) { System.out.print("*"); System.out.println(); For time purposes, I will begin with the code already written to open the file and read each score.

Text processing reading: 4.3

String traversals index 1 2 3 4 5 6 value 'l' 'e' 't' 'r' 's' Strings are represented internally as arrays of chars. We can write algorithms to traverse strings to compute information. What useful information might the following string have? "BAABAABCAABCDABAAABABABBCBCAACCAACBABBB" index 1 2 3 4 5 6 value 'l' 'e' 't' 'r' 's'

Grade inflation? Output: // string stores students’ grades String votes = "BAABAABCAABCDABAAABABABBCBCAACCAACBABBB"; int[] counts = new int[4]; // A -> 0, B -> 1, C -> 2, D -> 3 for (int i = 0; i < votes.length(); i++) { char c = votes.charAt(i); if (c == 'A') { counts[0]++; } else if (c == 'B') { counts[1]++; } else if (c == 'C') { counts[2]++; } else { // c == 'D' counts[3]++; } System.out.println(Arrays.toString(counts)); Output: [17, 14, 7, 1]

Section attendance question Read a file of section attendance (see next slide): yynyyynayayynyyyayanyyyaynayyayyanayyyanyayna ayyanyyyyayanaayyanayyyananayayaynyayayynynya yyayaynyyayyanynnyyyayyanayaynannnyyayyayayny And produce the following output: Section 1 Student points: [20, 16, 17, 14, 11] Student grades: [100.0, 80.0, 85.0, 70.0, 55.0] Section 2 Student points: [16, 19, 14, 14, 8] Student grades: [80.0, 95.0, 70.0, 70.0, 40.0] Section 3 Student points: [16, 15, 16, 18, 14] Student grades: [80.0, 75.0, 80.0, 90.0, 70.0] Students earn 3 points for each section attended up to 20.

Section input file Each line represents a section. A line consists of 9 weeks' worth of data. Each week has 5 characters because there are 5 students. Within each week, each character represents one student. a means the student was absent (+0 points) n means they attended but didn't do the problems (+1 points) y means they attended and did the problems (+3 points) student 123451234512345123451234512345123451234512345 week 1 2 3 4 5 6 7 8 9 section 1 section 2 section 3 yynyyynayayynyyyayanyyyaynayyayyanayyyanyayna ayyanyyyyayanaayyanayyyananayayaynyayayynynya yyayaynyyayyanynnyyyayyanayaynannnyyayyayayny

Section attendance answer import java.io.*; import java.util.*; public class Sections { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("sections.txt")); int section = 1; while (input.hasNextLine()) { String line = input.nextLine(); // process one section int[] points = new int[5]; for (int i = 0; i < line.length(); i++) { int student = i % 5; int earned = 0; if (line.charAt(i) == 'y') { // c == 'y' or 'n' earned = 3; } else if (line.charAt(i) == 'n') { earned = 1; } points[student] = Math.min(20, points[student] + earned); double[] grades = new double[5]; for (int i = 0; i < points.length; i++) { grades[i] = 100.0 * points[i] / 20.0; System.out.println("Section " + section); System.out.println("Student points: " + Arrays.toString(points)); System.out.println("Student grades: " + Arrays.toString(grades)); System.out.println(); section++;

Data transformations In many problems we transform data between forms. Example: digits  count of each digit  most frequent digit Often each transformation is computed/stored as an array. For structure, a transformation is often put in its own method. Sometimes we map between data and array indexes. by position (store the i th value we read at index i ) tally (if input value is i, store it at array index i ) explicit mapping (count 'J' at index 0, count 'X' at index 1) Exercise: Modify our Sections program to use static methods that use arrays as parameters and returns.

Array param/return answer // This program reads a file representing which students attended // which discussion sections and produces output of the students' // section attendance and scores. import java.io.*; import java.util.*; public class Sections2 { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("sections.txt")); int section = 1; while (input.hasNextLine()) { // process one section String line = input.nextLine(); int[] points = countPoints(line); double[] grades = computeGrades(points); results(section, points, grades); section++; } // Produces all output about a particular section. public static void results(int section, int[] points, double[] grades) { System.out.println("Section " + section); System.out.println("Student scores: " + Arrays.toString(points)); System.out.println("Student grades: " + Arrays.toString(grades)); System.out.println(); ...

Array param/return answer ... // Computes the points earned for each student for a particular section. public static int[] countPoints(String line) { int[] points = new int[5]; for (int i = 0; i < line.length(); i++) { int student = i % 5; int earned = 0; if (line.charAt(i) == 'y') { // c == 'y' or c == 'n' earned = 3; } else if (line.charAt(i) == 'n') { earned = 1; } points[student] = Math.min(20, points[student] + earned); return points; // Computes the percentage for each student for a particular section. public static double[] computeGrades(int[] points) { double[] grades = new double[5]; for (int i = 0; i < points.length; i++) { grades[i] = 100.0 * points[i] / 20.0; return grades;