Objectives You should be able to describe: Interactive Keyboard Input

Slides:



Advertisements
Similar presentations
Chapter 8 Improving the User Interface
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
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.
Java Programming, 3e Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
Chapter 3 Assignment and Interactive Input
Chapter 7 Improving the User Interface
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Introduction to C Programming
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Programming, 2E Introductory Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
Lesson 7: Improving the User Interface
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
©2004 Brooks/Cole Assignment and Interactive Input Java Shorthand Statements Mathematical Methods Conversion Methods Interactive Keyboard Input Interactive.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
A FIRST BOOK OF C++ CHAPTER 3 ASSIGNMENT AND INTERACTIVE INPUT.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Lecture 2 Objectives Learn about objects and reference variables.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
1 Class 6. 2 Objectives Objectives Identify, declare, and use primitive data types Use the System class to create data streams Use the Scanner class of.
Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
28 Formatted Output.
C Formatted Input/Output
Topics Designing a Program Input, Processing, and Output
Chapter 3 Syntax, Errors, and Debugging
Chapter 3 Assignment and Interactive Input.
Java Primer 1: Types, Classes and Operators
TMF1414 Introduction to Programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
Interactive Standard Input/output
Classes, Libraries & Packages
A First Book of ANSI C Fourth Edition
I/O Basics.
Java Programming: From Problem Analysis to Program Design, 4e
Starting JavaProgramming
Topics Introduction to File Input and Output
Chapter 2: Basic Elements of Java
A First Book of ANSI C Fourth Edition
Chapter 3: Introduction to Objects and Input/Output
A First Book of ANSI C Fourth Edition
Topics Designing a Program Input, Processing, and Output
Introduction to Java Applications
Topics Designing a Program Input, Processing, and Output
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Topics Designing a Program Input, Processing, and Output
Exception Handling Contents
C++ for Engineers and Scientists Second Edition
Topics Introduction to File Input and Output
Presentation transcript:

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Objectives You should be able to describe: Interactive Keyboard Input Interactive Dialog Input Creating a Class Library Formatted Output Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Objectives (continued) Mathematical Methods Common Programming Errors Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Interactive Keyboard Input Interactive data is entered: By user at keyboard Via graphical user interface (GUI) From file Data can be entered into program while it is running using System.in object Stream objects Called streams for short Transmit data as stream of individual data bytes Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Interactive Keyboard Input (continued) End-of-file (EOF) marker Special end-of-data value Numerical value that cannot be converted into legitimate character value Would like to read an entire line at once Use supporting classes: InputStreamReader BufferedReader Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Interactive Keyboard Input (continued) Figure 4.2: Generating the EOF value Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Interactive Keyboard Input (continued) InputStreamReader Automatically converts integer values of System.in stream to character values Can be constructed from System.in object InputStreamReader isr = new InputStreamReader(System.in); Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Interactive Keyboard Input (continued) BufferedReader Automatically constructs string from character values provided by InputStreamReader object BufferedReader br = new BufferedReader(isr); Display prompt Asks user to enter data Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Interactive Keyboard Input (continued) Calling readLine() Puts system in wait state Until user types data Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Interactive Keyboard Input (continued) Table 4.1: Keyboard Input Classes and Methods Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Interactive Keyboard Input (continued) Table 4.2: Java Conversion Routines Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

The StringTokenizer Class String of characters separated by delimiting character Delimiting characters Whitespace by default in Java Parsing the string Separating individual tokens from string Class StringTokenizer Used to parse strings Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

The StringTokenizer Class (continued) Figure 4.5: Parsing tokens from a string Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

The Scanner Class Provides simpler way to read numerical input No need to perform data type conversion Example: Scanner sc = new Scanner(System.in); double num1 = sc.nextDouble(); java.util.Formatter class Process more complex input Includes pattern matching algorithms Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

The Scanner Class (continued) Table 4.3: Commonly Used Scanner Class Input Methods Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

A First Look at User-Input Validation Well-constructed program Validates user input Does not crash due to unexpected input Crash Program termination caused by unexpected error Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

A First Look at User-Input Validation (continued) Figure 4.6: A NumberFormatException notification Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

A First Look at User-Input Validation (continued) Consists of: Validating entered data either during or immediately after data have been entered Providing user with way of reentering any invalid data To handle invalid input provide error processing code Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

A First Look at User-Input Validation (continued) Throwing error up to operating system Use reserved word throws with error name Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Interactive Dialog Input GUI method of entering user data: Method named showInputDialog()in JOptionPane class Creates dialog box that permits user to enter string Syntax: JOptionPane.showInputDialog(string); Example: s = JOptionPane.showInputDialog("Enter number:"); Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Interactive Dialog Input Figure 4.7: A sample showInputDialog() dialog Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Exception Handling Error handling in Java different from other high-level languages Exception handling Error occurs while method is running Method creates object that contains information about error Object immediately passed to Java Virtual Machine JVM attempts to locate code to handle exception Called throwing an exception Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Exception Handling (continued) Two fundamental types of errors: Result from inability of program to obtain required resource Result from flawed code Checked exception Java checks that exceptions will be handled Program must throw or handle exception Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Exception Handling (continued) Unchecked exception Java compiler does not check for handling code Error can always be prevented by programming Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Exception Handling (continued) Figure 4.4: Exception Handling Terminology Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Exception Handling (continued) try { // one or more statements } catch (exceptionName argument) finally Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Exception Handling (continued) try Identifies start of exception handling block of code Must be followed by one or more catch blocks catch Exception handler code finally Default set of instructions always executed whether or not any exception occurred Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Creating a Class Library Java provides extensive set of tested and reliable classes Increases with introduction of each new version Professional programmers create and share own libraries of classes Once they are tested Can be reused in other programs Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Formatted Output Display of both integer and floating-point numbers can be controlled by Java-supplied format() method In class java.text.DecimalFormat Especially useful in printing columns with numbers Example: DecimalFormat num = new DecimalFormat("000"); Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Formatted Output (continued) Required components for formatted output: Import statement for java.text package of classes Statement within main() method that uses new operator to create desired format string format() method call that applies format string to numerical value Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Formatted Output (continued) Table 4.5: Symbols for User-Defined Format Strings Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Formatted Output (continued) Table 4.6: Examples of Numerical Formats Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Formatted Output using printf Method of PrintStream class Syntax: printf(control string, arguments) Example: printf("The value of 6 plus 15 is %d%n", 6 + 15); Conversion control sequence Begins with % symbol Ends with conversion character Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Formatted Output using printf (continued) Table 4.7: Common Conversion Sequences for printf Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Width and Precision Specifiers Optional specifications for numbers: Field width Precision Example: System.out.printf("|%10.3f|", 25.67); Width: 10 Precision: 3 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Width and Precision Specifiers (continued) Table 4.8: Effect of Field Width and Precision Specifiers Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Flags Justification Explicit sign display printf format Normally right-justified for numbers Use – flag to left-justify Explicit sign display Normally displayed only for negative numbers Use + flag to display for all numbers printf format %[flags] [width] [precision] conversionCharacter Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Mathematical Methods Java provides standard preprogrammed methods within class named Math Methods are static and public Each Math class method is called by: Listing name of class A period Method’s name Passing data within parentheses following method’s name Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Mathematical Methods (continued) Table 4.9: Java’s Math Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Mathematical Methods (continued) Figure 4.17: Using and passing data to a Math class method Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Casts Java provides for explicit user-specified type conversions Use cast operator Unary operator Syntax: (dataType) expression Example: (int) (a * b) Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Conversion Methods Routines for converting string to primitive type and primitive type to string Referred to as wrapper classes Class structure wrapped around built-in: integer long float double Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Conversion Methods (continued) Table 4.11: Wrapper Class Conversion Routines Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Conversion Methods (continued) Figure 4.18: Conversions using a wrapper class method Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Common Programming Errors Forgetting to precede mathematical method with class name Math and period Not understanding difference between writing program for personal use and one intended for someone else’s use Being unwilling to test program in depth that is to be used by people other than yourself Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Summary Input from keyboard accomplished using readLine() method Must create InputStreamReader and BufferedReader Input dialog box method used for data input From class JOptionPane Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

Summary (continued) Exception Java provides Math class Error condition that occurs when program running Notification of exception immediately sent to Java Virtual Machine for processing Java provides Math class Contains methods for mathematical computations Java String class provides methods for converting strings into primitive numerical types Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition