Java Programming, 3e Concepts and Techniques Chapter 3 Manipulating Data Using Methods.

Slides:



Advertisements
Similar presentations
Pemrograman VisualMinggu …2… Page 1 MINGGU Ke Dua Pemrograman Visual Pokok Bahasan: Console Application Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan.
Advertisements

Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
The Web Warrior Guide to Web Design Technologies
Class 7.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Chapter 2 - Introduction to Java Applications
Chapter 2: Using Data.
Java review and more. Class Header  Public class Welcome  Case sensitive  Body of the class must be enclosed by braces.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Chapter 5: Keyboard I/O & Simple GUI’s Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Chapter 2 - Introduction to Java Applications
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
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.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Java Programming, Second Edition Chapter Five Input and Selection.
Input, Output, and Processing
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
1 Java Programming 1 Introduction to Basic GUI Lesson 4.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to Programming with RAPTOR
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
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.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
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, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
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.
Creating a Java Application and Applet
Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Topics Designing a Program Input, Processing, and Output
Chapter 3 Syntax, Errors, and Debugging
“Form Ever Follows Function” Louis Henri Sullivan
Objectives You should be able to describe: Interactive Keyboard Input
2.5 Another Java Application: Adding Integers
Section 64 – Manipulating Data Using Methods – Java Swing
Java Programming: From Problem Analysis to Program Design,
Java Programming: Guided Learning with Early Objects
Chapter 2 - Introduction to Java Applications
Manipulating Data Using Methods
Topics Designing a Program Input, Processing, and Output
Chapter 5 Processing Input with Applets
Presentation transcript:

Java Programming, 3e Concepts and Techniques Chapter 3 Manipulating Data Using Methods

Chapter 3: Manipulating Data Using Methods 2 Chapter Objectives Identify, declare, and use primitive data types Use the System class to create data streams Instantiate the BufferedReader class in code Use the readLine() method to handle user input

Chapter 3: Manipulating Data Using Methods 3 Chapter Objectives Convert strings to numbers using the parse() method Use assignment statements to store data with proper identifiers Use operators and parentheses correctly in numeric and conditional expressions Round an answer using the round() method of the Math class

Chapter 3: Manipulating Data Using Methods 4 Chapter Objectives Use Swing components to build the GUI for a Swing program Use the exit() method to close a Swing program Implement an ActionListener to handle events Add interface components to an applet

Chapter 3: Manipulating Data Using Methods 5 Chapter Objectives Use the init() and paint() methods to load the applet interface Use the actionPerformed() method Run and test an interactive applet Manage Java source code files and Java class files

Chapter 3: Manipulating Data Using Methods 6 Introduction Data are collections of raw facts or figures A program performs operations on input data to output information Input data can come from a variety of sources –The program itself –Users of the program –External files

Chapter 3: Manipulating Data Using Methods 7 The Body Mass Index Calculator An interactive program –Accepts the weight and height from the user –Calculates the BMI to gauge total body fat –Displays the result Three versions –Input/Output using the command prompt –Input/Output using dialog boxes –Web environments use an applet interface

Chapter 3: Manipulating Data Using Methods 8 (b) console application using dialog boxes (a) console application in a command prompt window (c) applet

Chapter 3: Manipulating Data Using Methods 9

10 Problem Analysis Convert user input to metric measurements Calculate the BMI Display the result

Chapter 3: Manipulating Data Using Methods 11 Design the Solution Design the three kinds of user interfaces with storyboards Design the logic of the program –Use pseudocode for sequential flow for all programs –Use an event diagram for the applet Validate the design –Compare the program design with the original requirements

Chapter 3: Manipulating Data Using Methods 12

Chapter 3: Manipulating Data Using Methods 13 Coding the Program Import the java.io package –Provides classes to support system input and output Add a throws IOException clause to the method header –Warns the compiler that the possibility of input or output errors exists –Gives the program the opportunity to handle input or output errors during run-time without aborting

Chapter 3: Manipulating Data Using Methods 14 Coding the Program

Chapter 3: Manipulating Data Using Methods 15 Storing Data Java is a strongly typed language –Variables must be declared with a data type –Variable locations can hold only that data type Java has two categories of data types –Primitive data types hold single data items Integers, characters, floating point, and booleans are primitive types –Reference data types hold a value that refers to the location of the data All Objects and arrays are reference types

Chapter 3: Manipulating Data Using Methods 16

Chapter 3: Manipulating Data Using Methods 17 Declaring Variables

Chapter 3: Manipulating Data Using Methods 18 User Input – Streams and the System Class The act of data flowing in and out of a program is called a stream The System class creates three streams when a program executes

Chapter 3: Manipulating Data Using Methods 19 User Input – Streams and the System Class Data from input streams are first sent to a buffer The java.io package contains several stream classes –InputStreamReader Decodes the bytes from the System.in buffer into characters –BufferedReader Increases efficiency by temporarily storing the input received from another class, such as InputStreamReader Aids in platform independence by simplifying the process of reading text and numbers from various input sources

Chapter 3: Manipulating Data Using Methods 20 Using the BufferedReader class Call the BufferedReader constructor to instantiate a BufferedReader object The argument of the BufferedReader() method instantiates an InputStreamReader BufferedReader() returns a reference to the input data from System.in

Chapter 3: Manipulating Data Using Methods 21

Chapter 3: Manipulating Data Using Methods 22 User Prompts, Inputs, and Conversions The readLine() method reads a line of input text and returns a String containing the line The returned String must be explicitly converted if the data is to be used as another data type Each primitive data type has a wrapper class allowing the primitive to be treated as an object The wrapper classes provides a parse() method to convert Strings to primitives, and vice versa –Example: height = dataIn.readLine(); inches = Integer.parseInt(height);

Chapter 3: Manipulating Data Using Methods 23 Assignment Statements General syntax: location = value

Chapter 3: Manipulating Data Using Methods 24 Arithmetic Operators

Chapter 3: Manipulating Data Using Methods 25 Arithmetic Operators The order of operator precedence is a predetermined order that defines the sequence in which operators are evaluated in an expression Addition, subtraction, multiplication, and division can manipulate any numeric data type When Java performs math on mixed data types, the result is always the larger data type Casts allow programmers to force a conversion from one primitive type to another

Chapter 3: Manipulating Data Using Methods 26 Comparison Operators A comparison operation results in a true or false value that can be stored in a boolean variable

Chapter 3: Manipulating Data Using Methods 27 Numeric Expressions Numeric expressions evaluate to a number Only numeric primitive data types may be used in a numeric expression A value and variable must be separated by an arithmetic operator Unless parentheses supercede, an expression is evaluated left to right with the following rules of precedence: –Multiplication and/or division –Integer division –Modular division –Addition and/or subtraction

Chapter 3: Manipulating Data Using Methods 28 Conditional Expressions Conditional expression evaluate to either true or false Comparison operators, values, variables, methods, and Strings may be used in a conditional expression Two operands must be separated by a comparison operator Unless parentheses supercede, an expression is evaluated left to right with relational operators (, >=) taking precedence over equality operators (==, !=)

Chapter 3: Manipulating Data Using Methods 29 Parentheses in Expressions Parentheses may be used to change the order of operations –The part of the expression within the parentheses is evaluated first Parentheses can provide clarity in complex expressions –Numeric and conditional expressions should be grouped with parentheses Parentheses can be nested –Java evaluates the innermost expression first and then moves on to the outermost expression

Chapter 3: Manipulating Data Using Methods 30 Construction of Error-Free Expressions Java may not be able to evaluate a validly formed expression due to the following logic errors: –Dividing by zero –Taking the square root of a negative value –Raising a negative value to a non-integer value –Using a value too great or too small for a given data type –Comparing different data types in a conditional expression

Chapter 3: Manipulating Data Using Methods 31 The Math Class

Chapter 3: Manipulating Data Using Methods 32 Using Variables in Output

Chapter 3: Manipulating Data Using Methods 33 Compiling, Running, and Documenting the Application Compile the Body Mass Index Calculator program Execute the program Test the program by entering the sample input data supplied in the requirements phase at the prompts Verify the results Print the source code and screen images for documentation

Chapter 3: Manipulating Data Using Methods 34 Using Swing Components Save the previous version of the Body Mass Index Calculator with a new filename Import the javax.swing.JOptionPane class –Contains methods to create dialog boxes for input, confirmation, and messages Delete the IOException and BufferedReader code –The swing dialog boxes buffer data from the user and handle IO errors

Chapter 3: Manipulating Data Using Methods 35 Swing Dialog Boxes Dialog boxes are created with the JOptionPane “show” methods The showInputDialog() and showConfirmDialog return a String containing the user input

Chapter 3: Manipulating Data Using Methods 36 Swing Dialog Boxes

Chapter 3: Manipulating Data Using Methods 37 Closing Programs that use Swing System.exit() terminates an application that displays a GUI –The command prompt window closes when this method is called System.exit accepts an integer argument that serves as a status code –0 indicates successful termination –1 indicates abnormal termination

Chapter 3: Manipulating Data Using Methods 38 Saving, Compiling, and Running the Swing Version Verify that the file name matches the class name at the beginning of the code Compile the source code Test with the same sample data for all versions to compare output results If incorrect or unrealistic data is entered by the user, errors will occur –Errors and exception handling will be discussed in a later chapter

Chapter 3: Manipulating Data Using Methods 39 Moving to the Web The applet version of the Body Mass Index Calculator has four kinds of objects – Image, Labels, TextFields, and Buttons Import three packages –Java.applet –Java.awt –Java.awt.event Implement an ActionListener interface in the class header –Informs the program to respond to user-driven events

Chapter 3: Manipulating Data Using Methods 40 Moving to the Web Every event class has one or more associated listener interfaces

Chapter 3: Manipulating Data Using Methods 41 Moving to the Web

Chapter 3: Manipulating Data Using Methods 42 Adding Interface Components to an Applet Label –Displays text in the applet window TextField –Displays a text box for users to enter text Buttons –Displays a command button for users to click

Chapter 3: Manipulating Data Using Methods 43 The init() Method Initializes the window color and graphic Adds components to the applet window Registers the Button’s ActionListener

Chapter 3: Manipulating Data Using Methods 44 The actionPerformed() Method When a click event occurs, the ActionListener’s actionPerformed() method is triggered –Input values are retrieved with getText() –Calculations are performed –Output is sent to a label with setText()

Chapter 3: Manipulating Data Using Methods 45 The paint() Method Draws the initialized image in the applet window

Chapter 3: Manipulating Data Using Methods 46 Creating an HTML Host Document for an Interactive Applet Compile the applet Write an HTML Host Document to execute the applet –Use the tag to specify the bytecode file, and width and height of the window Use the same sample data to test the applet Document the source code

Chapter 3: Manipulating Data Using Methods 47 File Management Coding and compiling an application creates several files on your storage device File naming conventions and the operating system’s capability of displaying icons can help the programmer maintain a logical order –Three java files named after the program purpose and user interface type –Three class files after compilation –HTML host document –Image file

Chapter 3: Manipulating Data Using Methods 48 Chapter Summary Identify, declare, and use primitive data types Use the System class to create data streams Instantiate the BufferedReader class in code Use the readLine() method to handle user input

Chapter 3: Manipulating Data Using Methods 49 Chapter Summary Convert strings to numbers using the parse() method Use assignment statements to store data with proper identifiers Use operators and parentheses correctly in numeric and conditional expressions Round an answer using the round() method of the Math class

Chapter 3: Manipulating Data Using Methods 50 Chapter Summary Use Swing components to build the GUI for a Swing program Use the exit() method to close a Swing program Implement an ActionListener to handle events Add interface components to an applet

Chapter 3: Manipulating Data Using Methods 51 Chapter Summary Use the init() and paint() methods to load the applet interface Use the actionPerformed() method Run and test an interactive applet Manage Java source code files and Java class files

Java Programming, 3e Concepts and Techniques Chapter 3 Complete