CSCI 1100/1202 January 23, 2002. Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods.

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
Lecture 15: I/O and Parsing
Formatting Output For a pleasant interaction with the user.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
Java review and more. Class Header  Public class Welcome  Case sensitive  Body of the class must be enclosed by braces.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
Lecture 13: Keyboard Input and Text Files Yoni Fridman 7/23/01 7/23/01.
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Class Decimal Format ► Import package java.text ► Create DecimalFormat object and initialize ► Use method format ► Example: import java.text.DecimalFormat.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
Chapter 3b Standard Input and Output Sample Development.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Java I/O Input: information brought to program from an external source
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
1 Course Lectures Available on line:
Two Ways to Store Data in a File Text format Binary format.
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
I/O in Java Dennis Burford
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
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,
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
CS 11 java track: lecture 2 This week: more on object-oriented programming (OOP) objects vs. primitive types creating new objects with new calling methods.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Programming in Java (COP 2250) Lecture 7 Chengyong Yang Fall, 2005.
Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Introduction to programming in java
Formatting Output & Enumerated Types & Wrapper Classes
Outline Creating Objects The String Class Packages Formatting Output
Objectives You should be able to describe: Interactive Keyboard Input
Introduction to Exceptions in Java
Interactive Standard Input/output
Objects, Classes, Program Constructs
Classes, Libraries & Packages
I/O Basics.
Computer Programming Methodology File Input
INPUT STATEMENTS GC 201.
Chapter 3, cont Sept 20, 2004.
Reading and Writing Text Files
L3. Necessary Java Programming Techniques
L3. Necessary Java Programming Techniques
Objects and Primitive Data
Chapter 3 Numerical Data
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Exception Handling Contents
Math class services (functions)
EEC 484/584 Computer Networks
Presentation transcript:

CSCI 1100/1202 January 23, 2002

Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods are called class methods or static methods The Math class (defined in java.lang) contains many static methods that provide mathematical functions temp = Math.cos(90) + Math.sqrt(delta); See pages of the text for a complete listing of available methods

Formatting Output The NumberFormat class has static methods that return a formatter object getCurrencyInstance() getPercentInstance() Each formatter object has a method called format that returns a string with the specified information in the appropriate format See Price.javaPrice.java

Formatting Output The DecimalFormat class can be used to format a floating point value in generic ways For example, you can specify that the number be printed to three decimal places The constructor of the DecimalFormat class takes a string that represents a pattern for the formatted number Instantiated in traditional way with new operator See CircleStats.javaCircleStats.java

Input We will not be using the Keyboard class mentioned in the text. Refer to: –Notes on input handed out on January 18 th –Back of program template sheet for sample code.

Changes to enable input Need to import the classes that provide us with services for input. Need to deal with the errors that may occur during user input. Need to instantiate the objects necessary to handle the flow of data from the keyboard to the program.

Input Template public class MyProgram {}{} public static void main (String[] args) {}{} import java.io.*; InputStreamReader reader = new InputStreamReader(System.in); bufferedReader input = new BufferedReader(reader); System.out.print(“Prompt for input:”); String inputString = input.readline(); throws IOException

How input works InputStreamReader reader = new InputStreamReader(System.in); –The object reader is an instance of the InputStreamReader class and is bound to the system input stream System.in –Will convey data from the keyboard to the program BufferedReader input = new BufferedReader(reader); –The object input is an instance of the BufferedReader class and is bound to the reader object. –BufferedReader offers useful services like readLine()

How input works System.out.print(“Prompt for input:”); –The user needs to know that input is expected. String inputString = input.readline(); –The readline() method of BufferedReader reads a line of input from the keyboard. –It reads up to a \n character (does not include the newline character in the String object it returns). See TextInput.java

Converting text to a number The readLine() method of BufferedReader returns a String object Need to convert it to an integer if integer input is required: Integer integerObject = new Integer(text); int integerPrimitive = integerObject.intValue(); Can combine into one statement: int number = new Integer(text).intValue(); Similar for converting to a double (use the Double class) See IntegerInput.java

Program Statements We will now examine some other program statements Chapter 3 focuses on: –the flow of control through a method –decision-making statements –operators for making complex decisions –repetition statements –software development stages