Data Input and Output Using the Library Software CSC 1401: Introduction to Programming with Java Lecture 4 – Part 1 Wanda M. Kunkle.

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Introduction to Computers and Programming Lecture 7:
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
CS150 Introduction to Computer Science 1
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
File Input and Output CSC 1401: Introduction to Programming with Java Week 4 – Lecture 2 Wanda M. Kunkle.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
The Java String Type CSC 1401: Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Input, Output, and Processing
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
Program Input and the Software Design Process ROBERT REAVES.
Introduction to Programming
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Constants, Data Types and Variables
Topics Designing a Program Input, Processing, and Output
BASIC ELEMENTS OF A COMPUTER PROGRAM
INC 161 , CPE 100 Computer Programming
INPUT STATEMENTS GC 201.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
IDENTIFIERS CSC 111.
CS150 Introduction to Computer Science 1
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Presentation transcript:

Data Input and Output Using the Library Software CSC 1401: Introduction to Programming with Java Lecture 4 – Part 1 Wanda M. Kunkle

Data Input and Output Inputting data from the keyboard and outputting it to the screen using the software that comes with our book is similar to doing so using the features added to the Java programming language in Java 5. We’ve already seen some examples of inputting and outputting different kinds of data in the lab 1 and 2 programs.

Inputting Different Kinds of Numbers Integers Use the readint() method to input integers. Use the readint() method to input integers. Example from Operators.java : input in = new input(); output out = new output(); out.writeln("Enter first number " + "(an integer, please)"); int first = in.readint(); Example from Operators.java : input in = new input(); output out = new output(); out.writeln("Enter first number " + "(an integer, please)"); int first = in.readint(); Important Note: The type of the variable must match the type of the data the method is expecting.

Inputting Different Kinds of Numbers Floats Use the readfloat() method to input floats (floating- point numbers ranging from ±3.4 × to ±1.4 × ). Use the readfloat() method to input floats (floating- point numbers ranging from ±3.4 × to ±1.4 × ). Example from Time.java : input in = new input(); output out = new output(); out.writeln("Please enter distance (in miles)"); float distance = in.readfloat(); Example from Time.java : input in = new input(); output out = new output(); out.writeln("Please enter distance (in miles)"); float distance = in.readfloat(); Note: Note: Floating-point numbers got their name because they are stored internally in scientific notation. This requires “floating” the decimal point to a new location and adjusting the exponent (e.g., rewriting.0625 as 6.25 × ).

Inputting Different Kinds of Numbers Doubles Use the readdouble() method to input doubles (floating-point numbers ranging from ±1.8 × to ±4.9 × ). Use the readdouble() method to input doubles (floating-point numbers ranging from ±1.8 × to ±4.9 × ). Example from Tax.java : input in = new input(); output out = new output(); out.writeln("Program to compute tax"); out.writeln("Enter price of item:"); double price = in.readdouble(); Example from Tax.java : input in = new input(); output out = new output(); out.writeln("Program to compute tax"); out.writeln("Enter price of item:"); double price = in.readdouble();

Inputting Characters Characters Use the read() method to input a single character. Use the read() method to input a single character. For example, given the input ‘Y’ the statement: char choice = in.read(); would store ‘Y’ in choice. Use the readnext() method to input a single character and skip it. Use the readnext() method to input a single character and skip it.

Inputting Strings Strings Use the readname() method to input a string consisting of letters, digits, and underscores. Use the readname() method to input a string consisting of letters, digits, and underscores. The method reads until it encounters a character that is not a letter, digit, or underscore. For example, given the input “Ah! I see.” the statement: String name = in.readname(); would store “Ah” in name. Use the readword() method to input a string consisting of nonblank characters. Use the readword() method to input a string consisting of nonblank characters. The method reads until it encounters a blank character (Blank characters include the space and newline characters.). For example, given the input “Ah! I see.” the statement: String word = in.readword(); would store “Ah!” in word.

Inputting Strings Strings Use the readline() method to input a string consisting of nonblank as well as blank characters. Use the readline() method to input a string consisting of nonblank as well as blank characters. The method reads until it encounters the newline character (which it reads but does not store). For example, given the input “Ah! I see.” the statement: String line = in.readline(); would store “Ah! I see.” in line. Use the readln() method to input a string and skip it. Use the readln() method to input a string and skip it. The method reads until it encounters the newline character; it skips all characters, including the newline. For example, the statement: in.readln(); would read and skip any input string ending in newline.

Inputting Strings Strings Use the readblanks() method to input and skip consecutive blanks. Use the readblanks() method to input and skip consecutive blanks. For example, the statement: in.readblanks(); would read and skip consecutive blanks in an input string.

Outputting Different Kinds of Data Numbers, characters, strings Use the write() or writeln() methods to output numbers, characters, or strings. Use the write() or writeln() methods to output numbers, characters, or strings. write() outputs the expression specified in the parentheses. write() outputs the expression specified in the parentheses. writeln() outputs the expression specified in the parentheses followed by a newline character. writeln() outputs the expression specified in the parentheses followed by a newline character. Example 1: Example 1: input in = new input(); output out = new output(); out.write("Please enter an integer: "); int intNum = in.readint(); out.writeln("The integer you entered is " + intNum);

Outputting Different Kinds of Data Numbers, characters, strings Example 2: Example 2: input in = new input(); output out = new output(); char space = ‘ ’; out.writeln("Please enter your first name: "); String firstName = in.readname(); out.writeln("Please enter your last name: "); String lastName = in.readname(); out.writeln("You entered your name as: " + firstName + space + lastName); Concatenation: Glueing the same or different types (String, char) together using the ‘+’ operator to form a single output string