Homework Six. A201/A597/I210 Spring Semester 2005

Similar documents
Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

16-Dec-10. Consider the following method:

Lecture 4: Exceptions. I/O

Project #1 Computer Science 2334 Fall 2008

Remedial Java - Excep0ons 3/09/17. (remedial) Java. Jars. Anastasia Bezerianos 1

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

CS 116. Lab Assignment # 1 1

Tips from the experts: How to waste a lot of time on this assignment

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

Announcements. 1. Forms to return today after class:

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ

IT101. File Input and Output

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Input from Files. Buffered Reader

Getting Started in Java. Bill Pugh Dept. of Computer Science Univ. of Maryland, College Park

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Java Basics Lecture: January 26, 2012 (On-line Lecture 1)

C++ Support Classes (Data and Variables)

Introduction to Java Unit 1. Using BlueJ to Write Programs

CS 231 Data Structures and Algorithms, Fall 2016

Lesson 3: Accepting User Input and Using Different Methods for Output

Lab Assignment Three

Exceptions and Working with Files

Lab 2: File Input and Output

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

CHAPTER 7 OBJECTS AND CLASSES

When we reach the line "z = x / y" the program crashes with the message:

Tips from the experts: How to waste a lot of time on this assignment

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

Inheritance E, xc Ex eptions ceptions I/O

Course Content. Objectives of Lecture 22 File Input/Output. Outline of Lecture 22. CMPUT 102: File Input/Output Dr. Osmar R.

Computer Science 62 Lab 8

//Initializes the variables that will hold the eventual final new versions of the string String pluralextension; String secondwordextension;

CmpSci 187: Programming with Data Structures Spring 2015

1. Download the JDK 6, from

Java Input/Output. 11 April 2013 OSU CSE 1

Documenting, Using, and Testing Utility Classes

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Options for User Input

Welcome (back) to CS1007!

Chapter 1 Introduction to Java

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

York University Department of Electrical Engineering and Computer Science. Regular Expressions

Exercise 4: Loops, Arrays and Files

Introduction IS

Soda Machine Laboratory

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

Expanded Guidelines on Programming Style and Documentation

Lab 10: Sockets 12:00 PM, Apr 4, 2018

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Computer Science 1 CSCI-1100 Spring Semester 2015 Exam 1 Overview and Practice Questions

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

CSPP : Introduction to Object-Oriented Programming

LECTURE 2 (Gaya College of Engineering)

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Active Learning: Streams

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Many strategies have been developed for this, but they often produce surprising results.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Web Server Project. Tom Kelliher, CS points, due May 4, 2011

COMP-202: Foundations of Programming. Lecture 4: Methods Jackie Cheung, Winter 2016

CS664 Compiler Theory and Design LIU 1 of 16 ANTLR. Christopher League* 17 February Figure 1: ANTLR plugin installer

Each command-line argument is placed in the args array that is passed to the static main method as below :

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

AP Computer Science A Summer Assignment 2017

PROGRAMMING STYLE. Fundamentals of Computer Science I

Computer Hardware. Java Software Solutions Lewis & Loftus. Key Hardware Components 12/17/2013

Class 1: Homework. Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

last time in cs recitations. computer commands. today s topics.

Introduction to Java Applications

My First Command-Line Program

Java Coding style guide 1. Java. Coding Style Guide. (July 2015)

Values and Variables 1 / 30

Principles of Computer Science I

CHAPTER 7 OBJECTS AND CLASSES

CS 106 Introduction to Computer Science I

Comp Assignment 2: Object-Oriented Scanning for Numbers, Words, and Quoted Strings

Programming Languages and Techniques (CIS120)

Internet Technology 2/7/2013

Lecture 5: Implementing Lists, Version 1

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

Announcements for the Class

ASSIGNMENT 5 Objects, Files, and a Music Player

Exceptions. What exceptional things might our programs run in to?

ASSIGNMENT 5 Objects, Files, and More Garage Management

1 If you want to store a letter grade (like a course grade) which variable type would you use? a. int b. String c. char d. boolean

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Objec&ves. Review. Standard Error Streams

Supplement D: Expanded Guidelines on Programming Style and Documentation

CMSC 201 Spring 2017 Lab 01 Hello World

Transcription:

Homework Six A201/A597/I210 Spring Semester 2005 Due in OnCourse by Friday, March 11, 11:59pm (No wrten report is due in class, read below details!) Abstract Wre programs that solve 5 of the problems below as described. It s up to you what 5 problems you choose one of them must be Problem 3.9 (that is, the Copy problem). One of the problems (Problem 3.1) is solved already, so you can t choose that one. Also included in the set are 4 (four) bonus problems. Thus the total presented below is of 10 problems. Programs should be submted to OnCourse in the dropbox Homework Six. One or two paragraphs must summarize your program at the top of your source code file (see example posted, reproduced below). You re encouraged to work in groups and discuss the problems you need to wre the programs and prepare the wre-up all by yourself. The Computer Science Department 1 and the School of Inmatics 2 clearly specify the rules of academic honesty and academic integry, so please read the documents and make sure you understand them and comply wh them. Posting solutions or major hints on the bulletin board is not allowed. 1 The Summary This homework is about using everything that you learned so far (loops, tokenizers etc.) to read data from a file and process. In the case of Problem 3.9 you also need to open a file and wre data into. All files we deal wh are text files. In Java we could read and wre many types of files (serialized data, XML, etc) we only focus on text files in A201. Problems listed below are at the level of difficulty of Homework Five and they would look very similar if instead of a file they would be reading from the console, a sequence of lines terminated wh done, as we have done in the past. In that context we would simulate the end of file (EOF) by typing s value: Control-D (press the Ctrl key and while you keep pressed also press the key the letter D). To test end of file we would be asking in our program if the line that was read was empty (null). 1 http://www.cs.indiana.edu/academics/integry.html 2 http://www.inmatics.indiana.edu/courses/honesty.asp 1

2 What, No Wrten Report?! Starting wh this assignment you will not have to turn in a wrten report in lecture any longer 3. Instead you will have to describe the program(s) by wring Javadoc comments in the program, the way I did below. Please follow the template of Solved Problem 3.1 when turning in the programs, wh Javadoc comments about your program design, as appropriately. 3 The Problems As indicated earlier, the first problem is shown wh a solution. We hope you find this helpful. Please note the large Javadoc comments 4 from now on will replace the wrten reports we used to turn in lecture. 3.1 Count the Number of Lines in a File (Solved) Wre a program that reads a file and reports the number of lines in the file. The program receives the name of the input file on the command line and prints the report back on the screen. Here s how works: cat one.txt a quote by soren kirkegaard wards. java One Exception in thread "main" java.lang.arrayindexoutofboundsexception: 0 at One.main(One.java:23) java One one Something went wrong: java.io.filenotfoundexception: one (No such file or directory) There are 0 lines in file one java One one.txt There are 16 lines in file one.txt 3 This, of course, comes as a great relief to many of us 4 This code is also posted on-line (in color) 2

Here s the code of the program, wh comments as a model, you. import java.io.*; /** * In this program I solve the problem of counting the number of lines * in a file. This is Problem One in Homework Six (due Fri 3/11 @11:59pm) * * @author Adrian German * */ public class One { /** The main method is only two lines long because a procedure (method) * is used to count the lines in a file. The name of the file is passed * on the command line and is available to the main method as the first * of the command line arguments, args[0]. args[0] is passed to the * One.countLines(String) method and the int returned by the method is * placed in the count variable ( is local to main). All of this is * described on one line. The next line prints count and the program ends. */ public static void main(string[] args) { int count = One.countLines(args[0]); System.out.println("There are " + count + " lines in file " + args[0]); /** The countlines method receives one argument, called name, of type String. * It sets a local variable, count, to zero, It then tries to create a new * FileInputStream, called cable, using the name of the file on the disk. On * top of the cable we put receiver, is an InputStreamReader, and on top * of that one we create the handset (a BufferedReader) will allow us to * read lines from the file wh the name "name". So we start by reading the * first line from the handset and then we enter a loop: while the line is not * the end of file, increment the count by one, and read a new line, repeat. * If the try fails an error message is printed. Otherwise the file input stream * is closed. The program returns count is the number of lines read from * the file. A return value of zero doesn t necessarily mean there was an error * since the file could have been empty. */ public static int countlines(string name) { int count = 0; try { FileInputStream cable = new FileInputStream(name); InputStreamReader receiver = new InputStreamReader(cable); BufferedReader handset = new BufferedReader(receiver); String line = handset.readline(); while (line!= null) { 3

count = count + 1; // System.out.println("(" + line + ")"); line = handset.readline(); cable.close(); catch (Exception e) { System.out.println("Something went wrong: " + e.tostring()); return count; 3.2 Number of Words in a File Wre a program that reads a file and reports the number of words 5 in the file. The program receives the name of the input file on the command line and prints the report back to the screen. java Two one.txt The total number of tokens in the file is: 31 3.3 Average Number of Words per Line words 6 on a line, in the file. The program receives the name of the input file on the command line and prints the report back to the screen. java Three one.txt This file has 31/16 = 1.9375 words per line. 3.4 Average Line Length (in Characters) characters 7 on a line, in the file. The program receives the name of the input file on the command line and prints the report back to the screen. java Four one.txt This file has 136/16 = 8.5 characters per line. 5 We re interested in tokens, actually 6 We re actually interested in tokens, again 7 For the purpose of this exercise blank characters do not count. 4

3.5 Average Word Length (per File) characters per word (that is, token) in the file. The program receives the name of the input file on the command line and prints the report back to the screen. The result is one number. java Five one.txt This file has 136/31 = 4.387096774193548 characters per word. 3.6 Average Word Length (per Line) characters per token per line. The program gets the name of the input file on the command line and prints the report back to the screen. The result is a sequence of average word lengths, one each line in the file. java Six one.txt Line 1 has 8/2 = 4.0 characters per token, on average. Line 2 has 7/3 = 2.3333333333333335 characters per token, on average. Line 3 has 3/1 = 3.0 characters per token, on average. Line 4 has 9/2 = 4.5 characters per token, on average. Line 5 has 10/2 = 5.0 characters per token, on average. Line 6 has 6/2 = 3.0 characters per token, on average. Line 7 has 17/3 = 5.666666666666667 characters per token, on average. Line 8 has 5/1 = 5.0 characters per token, on average. Line 9 has 13/3 = 4.333333333333333 characters per token, on average. Line 10 has 13/4 = 3.25 characters per token, on average. Line 11 has 10/1 = 10.0 characters per token, on average. Line 12 has 10/1 = 10.0 characters per token, on average. Line 13 has 3/1 = 3.0 characters per token, on average. Line 14 has 2/1 = 2.0 characters per token, on average. Line 15 has 11/3 = 3.6666666666666665 characters per token, on average. Line 16 has 9/1 = 9.0 characters per token, on average. 3.7 Print the Longest Line in File Wre a program that reads a file and reports the longest line (the line wh the most characters 8 in ). The program gets the name of the input file on the command line and prints the report back to the screen. The result is a line of text, surrounded by parentheses. java Seven one.txt The longest line in file one.txt is: ( ) As you can realize, there were spaces in my file. 8 Spaces are counted, this time. 5

3.8 Print the Line wh Most Words Wre a program that reads a file and reports the line wh the most tokens in. Obviously, the blank spaces can t have an influence here (unlike in the previous problem). The program gets the name of the input file on the command line and prints the report back to the screen. The result is a line of text, surrounded by parentheses. java Eight one.txt The line wh most words in file one.txt is: ( ) 3.9 Make a Copy of a File Wre a program that makes a copy of the file. The program receives two arguments on the command line: the first one is the source file (to be copied) and the second one is the target file (the newly created copy). ls -ld one.txt two.txt Nine.java ls: two.txt: No such file or directory -rw------- 1 dgerman faculty 744 Mar 5 23:46 Nine.java -rw------- 1 dgerman faculty 319 Mar 5 17:41 one.txt javac Nine.java java Nine one.txt two.txt ls -ld one.txt two.txt Nine.java -rw------- 1 dgerman faculty 744 Mar 5 23:46 Nine.java -rw------- 1 dgerman faculty 319 Mar 5 17:41 one.txt -rw------- 1 dgerman faculty 319 Mar 5 23:47 two.txt cat one.txt two.txt a quote by soren kirkegaard wards. 6

a by soren kirkegaard wards. quote 3.10 Search and Replace Wre a program that reads a file and two words and wres back the contents of the while wh all the occurrences of the first word replaced by the second. The file name and the two words are to be specified on the command line, as args[0], args[1] and args[2] respectively. java Ten one.txt kirkegaard kierkegaard a quote by soren kierkegaard wards. 4 The Datafile Used I used one and the same datafile (one.txt) throughout 9. 9 Perhaps the single most memorable feature of s contents is that I misspelled Kierkegaard. 7