A counting problem Problem: Write a method mostFrequentDigit that returns the digit value that occurs most frequently in a number. Example: The number.

Slides:



Advertisements
Similar presentations
Week 8 Arrays Part 2 String & Pointer
Advertisements

1 Various Methods of Populating Arrays Randomly generated integers.
BUILDING JAVA PROGRAMS CHAPTER 6.4 FILE OUTPUT. 22 PrintStream : An object in the java.io package that lets you print output to a destination such as.
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.
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.
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
Building java programs chapter 6
1 BUILDING JAVA PROGRAMS CHAPTER 7 LECTURE 7-3: ARRAYS AS PARAMETERS; FILE OUTPUT.
Topic 19 file input, line based Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Advanced File Processing NOVEMBER 21 ST, Objectives Write text output to a file. Ensure that a file can be read before reading it in your program.
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.
Building Java Programs Chapter 7
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.
Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
File Processing Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from "We can only.
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:
Building Java Programs
CSCI 162 – Introduction to Programming II William Killian
Building Java Programs
CSc 110, Autumn 2017 Lecture 24: Lists for Tallying; Text Processing
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
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
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
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
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Building Java Programs
Topics discussed in this section:
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
CSc 110, Spring 2017 Lecture 20: Lists for Tallying; Text Processing
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Topic 19 file input, line based
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
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
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Building Java Programs
Building Java Programs
Presentation transcript:

A counting 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 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

Section attendance question Read a file of section attendance (see next slide): yynyyynnynyynyyynynnyyynynnyynyynnnyyynnynynn nyynnyyyynynnnyyynnnyyynnnnnynynynynynyynynyn yynynynyynyynyynnyyynyynnnynynnynnyynyynynyny And produce the following output: Section 1 Student points: [30, 25, 25, 20, 15] Student grades: [100.0, 83.3, 83.3, 66.67, 50.0] Section 2 Student points: [25, 30, 20, 20, 15] Student grades: [83.3, 100.0, 66.67, 66.67, 50.0] Section 3 Student points: [25, 25, 25, 30, 20] Student grades: [83.3, 83.3, 83.3, 100.0, 66.67] Students earn 5 points for each section attended up to 30.

Section input file student 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. n means the student was absent (+0 points) y means they attended and did the problems (+5 points) student 123451234512345123451234512345123451234512345 week 1 2 3 4 5 6 7 8 9 section 1 section 2 section 3 yynyyynnynyynyyynynnyyynynnyynyynnnyyynnynynn nyynnyyyynynnnyyynnnyyynnnnnynynynynynyynynyn yynynynyynyynyynnyyynyynnnynynnynnyynyynynyny

PrintStream question Modify our previous Sections program to use a PrintStream to output to the file sections_out.txt. Section 1 Student points: [30, 25, 25, 20, 15] Student grades: [100.0, 83.3, 83.3, 66.67, 50.0] Section 2 Student points: [25, 30, 20, 20, 15] Student grades: [83.3, 100.0, 66.67, 66.67, 50.0] Section 3 Student points: [25, 25, 25, 30, 20] Student grades: [83.3, 83.3, 83.3, 100.0, 66.67]

System.out and PrintStream The console output object, System.out, is a PrintStream. PrintStream out1 = System.out; PrintStream out2 = new PrintStream(new File("data.txt")); out1.println("Hello, console!"); // goes to console out2.println("Hello, file!"); // goes to file A reference to it can be stored in a PrintStream variable. Printing to that variable causes console output to appear. You can pass System.out as a parameter to a method expecting a PrintStream. Allows methods that can send output to the console or a file.