ICS 4M. Introduction to Programming in Java

Similar documents
Event-Driven Programming

INPUT AND OUTPUT STREAMS

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

13 File Output and Input

Files and input/output streams

File I/O - Chapter 10. Many Stream Classes. Text Files vs Binary Files

Stream Classes and File I/O

Using Files as Input/Output in Java 5.0 Applications

Chapter 2: Elements of Java

Object-Oriented Programming in Java

Creating a Simple, Multithreaded Chat System with Java

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

CS506 Web Design and Development Solved Online Quiz No. 01

Division of Informatics, University of Edinburgh

JAVA - FILES AND I/O

Lesson: All About Sockets

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

J a v a Quiz (Unit 3, Test 0 Practice)

Building Java Programs

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

CS 1302 Ch 19, Binary I/O

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

public static void main(string[] args) { System.out.println("hello, world"); } }

Building Java Programs

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

AP Computer Science Static Methods, Strings, User Input

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

What is an I/O Stream?

CS 106 Introduction to Computer Science I

D06 PROGRAMMING with JAVA

Programming Languages CIS 443

Input / Output Framework java.io Framework

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

WRITING DATA TO A BINARY FILE

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Iteration CHAPTER 6. Topic Summary

Readings and References. Topic #10: Java Input / Output. "Streams" are the basic I/O objects. Input & Output. Streams. The stream model A-1.

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

AP Computer Science Java Mr. Clausen Program 9A, 9B

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Lecture 5: Java Fundamentals III

Introduction to Programming

Reading Input From A File

Moving from CS 61A Scheme to CS 61B Java

Sample CSE8A midterm Multiple Choice (circle one)

Chapter 10. A stream is an object that enables the flow of data between a program and some I/O device or file. File I/O

B.Sc (Honours) - Software Development

AP Computer Science Java Subset

Install Java Development Kit (JDK) 1.8

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Chapter 3. Input and output. 3.1 The System class

AVRO - SERIALIZATION

Java Basics: Data Types, Variables, and Loops

Introduction to Java. CS 3: Computer Programming in Java

Introduction to Java. Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

Building a Multi-Threaded Web Server

Variables, Constants, and Data Types

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Programming in Java

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Explain the relationship between a class and an object. Which is general and which is specific?

Text file I/O and simple GUI dialogues

Introduction to Java

FILE I/O IN JAVA. Prof. Chris Jermaine Prof. Scott Rixner

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Java CPD (I) Frans Coenen Department of Computer Science

Quick Introduction to Java

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

The following four software tools are needed to learn to program in Java:

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

Simple Java I/O. Streams

Basic Programming and PC Skills: Basic Programming and PC Skills:

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Chapter 1 Java Program Design and Development

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. AVRO Tutorial

LOOPS CHAPTER CHAPTER GOALS

Chapter 2 Introduction to Java programming

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Introduction to Object-Oriented Programming

Arrays in Java. Working with Arrays

objectives chapter Define classes that act like blueprints for new objects, made of variables and methods. Explain encapsulation and Java modifiers.

LAB4 Making Classes and Objects

CS170 Lab 11 Abstract Data Types & Objects

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

6.1. Example: A Tip Calculator 6-1

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Some Scanner Class Methods

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Continuous Integration Part 2

In this Chapter you ll learn:

Programming and Data Structures with Java and JUnit. Rick Mercer

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Transcription:

ICS 4M Introduction to Programming in Java Chapter 6 Notes 6.1 Entering Programs Programs are entered using the environment editor. Syntax errors are reported at compilation time and corrections can be made. When there are no errorsm the program can run. If results do not match what is expected, the program must contain semantic errors. These errors must be found and corrected. 6.2 Standard Input and Output Java provides a way of reading input from the standard input device (the keyboard) and writing to the standard output device (the screen) but its built-in facilities for this are quite primitive. Reading from standard input uses a predefined object called System.in. This is an object of class InputStream. An InputStream can only read in single characters. To be useful, System.in must be converted to a DataInputStream, from which an entire line of input can be read at a time. The statement DataInputStream stdin = new DataInputStream (System.in); performs the conversion. Once, this is done, the readline method is used to read a line of input from the standard input device. The System.out object s print and println methods send data to the standard output device. System.out.print (s)outputs a string s not followed by a Return while System.out.println (i) output an integer I followed by Return. Example: // The "EchoName" class. // Asks for your name and echoes it back.

public class EchoName static public void main (String [] args) throws IOException String name; DataInputStream stdin = new DataInputStream (System.in); System.out.println ("Please type your name: "); name = stdin.readline (); // Output the reply. System.out.println ("Your name is \"" + name + "\""); /* EchoName class */ 6.3 Input of Numerical Data // The "NextYear" class. Input of INTEGER numbers. // This tells you how old you will be next year. public class NextYear int age; c.print ("Please enter your age: "); age = c.readint (); c.print ("Your age next year will be "); c.println (age + 1); // NextYear class

// The "FloorArea" class. Input of REAL numbers. // Find the area of a floor given its length and width. public class FloorArea double len, width; c.print ("Enter length in meters "); len = c.readdouble (); c.print ("Enter width in meters "); width = c.readdouble (); c.print ("Area is "); c.print (len * width); c.println (" square meters"); // FloorArea class 6.4 Input of String Data // The "NameInOut" class. Input of STRINGS. // Read a name and output it. public class NameInOut String name; c.println ("Please enter your full name"); name = c.readline (); c.println ("Your name is \"" + name + "\""); // NameInOut class

6.5 Input of Sequences of Data Reading in a KNOWN number of data. Use a COUNTER loop. // The "TenAvg" class. // Compute the average of 10 marks. public class TenAvg int mark; int sum = 0; c.println ("Enter 10 marks one to a line"); for (int count = 1 ; count <= 10 ; count++) mark = c.readint (); sum += mark; c.println ("Average of 10 marks is " + (double) sum / 10); // TenAvg class Reading in an UNKNOW number of data. Use a CONDITIONAL loop. // The "LastLetter" class. // Read a sequence of words and output the last letter // of each word until the sentinel is read. public class LastLetter

String word; final String SENTINEL = "stop"; c.println ("Enter a sequence of words, end with " + SENTINEL); // Words must have at least one letter. word = c.readline (); while (!word.equals (SENTINEL)) c.println ("Last letter of " + word + " is " + word.charat (word.length () - 1)); word = c.readline (); c.println ("This is the end of the sequence"); // LastLetter class 6.6 Input from a File To read text data from a file, an input object of class BufferedReader is instantiated by a statement such as input = new BufferedReader(new FileReader (filename)); All output to a text file is as strings of characters. Files of strings are terminated by null. // The "FileAvg" class. // Input a sequence of integers entered originally one to a line // from a file whose name is to be read in from the keyboard // and find their average. public class FileAvg throws IOException

String filename, line; int number; int sum = 0, count = 0; c.println ("What is the name of the file of integers? "); filename = c.readline (); BufferedReader input; input = new BufferedReader (new FileReader (filename)); line = input.readline (); //Read a line of characters. while (line!= null) //File is terminated by a null. number = Integer.parseInt (line); count++; sum += number; line = input.readline (); //Change to an integer. //Read next line. c.println ("Average of " + count + " numbers is " + (double) sum / count); // FileAvg class 6.7 Output to a File To write text data to a named file, an object of type PrintWriter is declared and instantiated by chaining to a class FileWriter by statements of this type PrintWriter output; Output = new PrintWriter (new FileWriter(fileName)); // The "OddInts" class. // Produce a file of data consisting of the odd integers // from 1 to 361 inclusive. public class OddInts throws IOException

PrintWriter output; String filename; c.println ("What is the name of the file for the integers? "); filename = c.readline (); output = new PrintWriter (new FileWriter (filename)); for (int number = 1 ; number <= 361 ; number += 2) output.println (number); // Close file. output.close (); // OddInts class 6.8 Generated Data // The "DiceData" class. // Simulate the throw of two dice 300 times // and store the generated data in file "dice". public class DiceData throws IOException int die1, die2, roll; PrintWriter output; output = new PrintWriter (new FileWriter ("dice")); for (int count = 1 ; count <= 300 ; count++) die1 = (int) (Math.random () * 6) + 1; die2 = (int) (Math.random () * 6) + 1; roll = die1 + die2; output.println (roll);

output.close (); c.println ("Simulated data of 300 throws now in file 'dice' "); // DiceData class 6.9 Statistical Analysis of Data // The "DiceStats" class. // Compute the mean and variance for the simulated dice throws // in file "dice" prepared by the data generation program. public class DiceStats throws IOException int roll; int count = 0, sum = 0, sumofsquares = 0; String line; BufferedReader input; input = new BufferedReader (new FileReader ("dice")); line = input.readline (); while (line!= null) roll = Integer.parseInt (line); count++; sum += roll; sumofsquares += roll * roll; line = input.readline (); double average = (double) sum / count; double variance = (double) sumofsquares / count - average * average; c.println ("Average=" + average + " Variance=" + variance); // DiceStats class

6.10 Review Read textbook review. 6.11 Technical Terms Write out technical terms in your notes. Be sure you know them. 6.12 Exercises Pages 147-148 Do questions #1, #2, #3, #4