Download Histogram Demo and Design Example using Java.io and Generic Collection Libraries and more Lecture notes Programming Languages in PDF only on Docsity!
Programming Languages
and Techniques
(CIS1200)
Lecture 32
Histogram Demo
Chapter 28
Announcements (1)
• HW07: PennPals
– Due tomorrow at 11.59pm
• This Week
- No recitations this week
- TA OH on Monday and Tuesday are virtual
- OH on Wednesday are cancelled
- No lecture on Friday
• Wednesday, November 23
rd
– Bonus Lecture
- Only 10:15 AM class
- Material is not needed for HW or Exams
- Should be fun!
HW9: Game project
• Game Design Proposal Milestone Due: (8 points)
Tuesday, November 29
th
at 11:59PM
- (Should take about 1 hour)
- Submit on GRADESCOPE
- TAs will give you feedback soon
• Final Program Due: (92 points)
Monday, December 12
th
at 11:59pm
- Submit zipfile online, submission only checks if your code compiles
- IntelliJ is STRONGLY recommended for this project
- May distribute your game (after the deadline) if you do not use any of our code
• Grade based on demo with your TA during/after reading days
- Grading rubric on the assignment website
- Recommendation: don’t be too ambitious.
• NO LATE SUBMISSIONS PERMITTED
HW9: Game Project
Problem Statement
Write a program that, given a filename for a text file as input, calculates the frequencies ( i.e. , number of occurrences) of each distinct word of the file. The program should then print the frequency distribution to the console as a sequence of “word: freq” pairs (one per line).
- The : Histogram result:
- Write :
- a :
- as :
- calculates :
- command :
- console :
- distinct :
- distribution :
- e :
- each :
- file :
- filename :
- for :
- freq :
- frequencies :
- frequency :
- given :
- i :
- input :
- line :
- number :
- occurrences :
- of :
- one :
- pairs :
- per :
- print :
- program :
- sequence :
- should :
- text :
- that :
- the :
- then :
- to :
- word :
How to produce a stream of words?
1. How do we iterate through the text file, identifying all of the
words?
• Key idea: Define a class (WordScanner) that implements this
interface by reading words from a text file.
public interface Iterator { // returns true if the iteration has more elements public boolean hasNext(); // returns the next element in the iteration public T next(); // Optional: removes last element returned public void remove(); }
Coding: Histogram.java
WordScanner.java
Histogram.java
public class WordScanner implements Iterator { private Reader r; private int c = - 1; // ... } Which combination of the following properties form a useful invariant for the WordScanner fields?
- r is not null
- r is null if and only if there is no next word A. c is 0 if there is no next word and nonzero otherwise B. c is - 1 if there is no next word and contains the first character of the next word otherwise ANSWER: 1 & B