SlideShare a Scribd company logo
1 of 52
Java Programming, 3e
Concepts and Techniques
Chapter 3
Manipulating Data
Using Methods
2Chapter 3: Manipulating Data Using Methods
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
3Chapter 3: Manipulating Data Using Methods
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
4Chapter 3: Manipulating Data Using Methods
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
5Chapter 3: Manipulating Data Using Methods
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
6Chapter 3: Manipulating Data Using Methods
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
7Chapter 3: Manipulating Data Using Methods
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
8Chapter 3: Manipulating Data Using Methods
(b) console application using dialog boxes
(a) console application in a command prompt window
(c) applet
9Chapter 3: Manipulating Data Using Methods
10Chapter 3: Manipulating Data Using Methods
Problem Analysis
• Convert user input to metric measurements
• Calculate the BMI
• Display the result
11Chapter 3: Manipulating Data Using Methods
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
12Chapter 3: Manipulating Data Using Methods
13Chapter 3: Manipulating Data Using Methods
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
14Chapter 3: Manipulating Data Using Methods
Coding the Program
15Chapter 3: Manipulating Data Using Methods
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
16Chapter 3: Manipulating Data Using Methods
17Chapter 3: Manipulating Data Using Methods
Declaring Variables
18Chapter 3: Manipulating Data Using Methods
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
19Chapter 3: Manipulating Data Using Methods
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
20Chapter 3: Manipulating Data Using Methods
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
21Chapter 3: Manipulating Data Using Methods
22Chapter 3: Manipulating Data Using Methods
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);
23Chapter 3: Manipulating Data Using Methods
Assignment Statements
• General syntax: location = value
24Chapter 3: Manipulating Data Using Methods
Arithmetic Operators
25Chapter 3: Manipulating Data Using Methods
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
26Chapter 3: Manipulating Data Using Methods
Comparison Operators
• A comparison operation results in a true or false value
that can be stored in a boolean variable
27Chapter 3: Manipulating Data Using Methods
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
28Chapter 3: Manipulating Data Using Methods
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 (==, !=)
29Chapter 3: Manipulating Data Using Methods
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
30Chapter 3: Manipulating Data Using Methods
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
31Chapter 3: Manipulating Data Using Methods
The Math Class
32Chapter 3: Manipulating Data Using Methods
Using Variables in Output
33Chapter 3: Manipulating Data Using Methods
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
34Chapter 3: Manipulating Data Using Methods
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
35Chapter 3: Manipulating Data Using Methods
Swing Dialog Boxes
• Dialog boxes are created with the JOptionPane “show”
methods
• The showInputDialog() and showConfirmDialog return a
String containing the user input
36Chapter 3: Manipulating Data Using Methods
Swing Dialog Boxes
37Chapter 3: Manipulating Data Using Methods
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
38Chapter 3: Manipulating Data Using Methods
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
39Chapter 3: Manipulating Data Using Methods
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
40Chapter 3: Manipulating Data Using Methods
Moving to the Web
• Every event class has one or more associated
listener interfaces
41Chapter 3: Manipulating Data Using Methods
Moving to the Web
42Chapter 3: Manipulating Data Using Methods
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
43Chapter 3: Manipulating Data Using Methods
The init() Method
• Initializes the window color and graphic
• Adds components to the applet window
• Registers the Button’s ActionListener
44Chapter 3: Manipulating Data Using Methods
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()
45Chapter 3: Manipulating Data Using Methods
The paint() Method
• Draws the initialized image in the applet window
46Chapter 3: Manipulating Data Using Methods
Creating an HTML Host Document
for an Interactive Applet
• Compile the applet
• Write an HTML Host Document to execute
the applet
– Use the <APPLET> 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
47Chapter 3: Manipulating Data Using Methods
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
48Chapter 3: Manipulating Data Using Methods
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
49Chapter 3: Manipulating Data Using Methods
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
50Chapter 3: Manipulating Data Using Methods
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
51Chapter 3: Manipulating Data Using Methods
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

More Related Content

What's hot

Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: ModularityNicole Ryan
 
Testing part 2 bb
Testing part 2 bbTesting part 2 bb
Testing part 2 bbRavi Prakash
 
CIS110 Computer Programming Design Chapter (10)
CIS110 Computer Programming Design Chapter  (10)CIS110 Computer Programming Design Chapter  (10)
CIS110 Computer Programming Design Chapter (10)Dr. Ahmed Al Zaidy
 
Intake 38 2
Intake 38 2Intake 38 2
Intake 38 2Mahmoud Ouf
 
Programming Logic and Design: Working with Data
Programming Logic and Design: Working with DataProgramming Logic and Design: Working with Data
Programming Logic and Design: Working with DataNicole Ryan
 
Course Breakup Plan- C
Course Breakup Plan- CCourse Breakup Plan- C
Course Breakup Plan- Cswatisinghal
 
CIS110 Computer Programming Design Chapter (6)
CIS110 Computer Programming Design Chapter  (6)CIS110 Computer Programming Design Chapter  (6)
CIS110 Computer Programming Design Chapter (6)Dr. Ahmed Al Zaidy
 
Prelim Project OOP
Prelim Project OOPPrelim Project OOP
Prelim Project OOPDwight Sabio
 
Taking your machine learning workflow to the next level using Scikit-Learn Pi...
Taking your machine learning workflow to the next level using Scikit-Learn Pi...Taking your machine learning workflow to the next level using Scikit-Learn Pi...
Taking your machine learning workflow to the next level using Scikit-Learn Pi...Philip Goddard
 
Array sorting
Array sortingArray sorting
Array sortingALI RAZA
 
Intake 38 3
Intake 38 3Intake 38 3
Intake 38 3Mahmoud Ouf
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08Terry Yoast
 
Intake 38 4
Intake 38 4Intake 38 4
Intake 38 4Mahmoud Ouf
 

What's hot (17)

Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: Modularity
 
Ppt chapter05
Ppt chapter05Ppt chapter05
Ppt chapter05
 
Testing part 2 bb
Testing part 2 bbTesting part 2 bb
Testing part 2 bb
 
CIS110 Computer Programming Design Chapter (10)
CIS110 Computer Programming Design Chapter  (10)CIS110 Computer Programming Design Chapter  (10)
CIS110 Computer Programming Design Chapter (10)
 
Intake 38 2
Intake 38 2Intake 38 2
Intake 38 2
 
Programming Logic and Design: Working with Data
Programming Logic and Design: Working with DataProgramming Logic and Design: Working with Data
Programming Logic and Design: Working with Data
 
Course Breakup Plan- C
Course Breakup Plan- CCourse Breakup Plan- C
Course Breakup Plan- C
 
CIS110 Computer Programming Design Chapter (6)
CIS110 Computer Programming Design Chapter  (6)CIS110 Computer Programming Design Chapter  (6)
CIS110 Computer Programming Design Chapter (6)
 
Pptchapter04
Pptchapter04Pptchapter04
Pptchapter04
 
Prelim Project OOP
Prelim Project OOPPrelim Project OOP
Prelim Project OOP
 
Taking your machine learning workflow to the next level using Scikit-Learn Pi...
Taking your machine learning workflow to the next level using Scikit-Learn Pi...Taking your machine learning workflow to the next level using Scikit-Learn Pi...
Taking your machine learning workflow to the next level using Scikit-Learn Pi...
 
Ppt chapter03
Ppt chapter03Ppt chapter03
Ppt chapter03
 
Array sorting
Array sortingArray sorting
Array sorting
 
Intake 38 3
Intake 38 3Intake 38 3
Intake 38 3
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
 
Intake 38 4
Intake 38 4Intake 38 4
Intake 38 4
 
Stack&heap
Stack&heapStack&heap
Stack&heap
 

Viewers also liked

Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsIt Academy
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritanceMahmoud Alfarra
 
9781111530532 ppt ch07_passing_primitivetypeasobjects
9781111530532 ppt ch07_passing_primitivetypeasobjects9781111530532 ppt ch07_passing_primitivetypeasobjects
9781111530532 ppt ch07_passing_primitivetypeasobjectsTerry Yoast
 
Chapter 1.3
Chapter 1.3Chapter 1.3
Chapter 1.3sotlsoc
 
9781111530532 ppt ch06
9781111530532 ppt ch069781111530532 ppt ch06
9781111530532 ppt ch06Terry Yoast
 
Head First Java Chapter 2
Head First Java Chapter 2Head First Java Chapter 2
Head First Java Chapter 2Tom Henricksen
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Thuan Nguyen
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03Terry Yoast
 
Chapter 14
Chapter 14Chapter 14
Chapter 14Terry Yoast
 
Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Thuan Nguyen
 
Intermediate Java Programming
Intermediate Java ProgrammingIntermediate Java Programming
Intermediate Java ProgrammingYowhans Kifle
 
Code complete chapter 19, 20 organize
Code complete chapter 19, 20 organizeCode complete chapter 19, 20 organize
Code complete chapter 19, 20 organizehanstar17
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQLlubna19
 
Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Thuan Nguyen
 
Big Java Chapter 1
Big Java Chapter 1Big Java Chapter 1
Big Java Chapter 1Maria Joslin
 

Viewers also liked (20)

Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
02basics
02basics02basics
02basics
 
9781111530532 ppt ch07_passing_primitivetypeasobjects
9781111530532 ppt ch07_passing_primitivetypeasobjects9781111530532 ppt ch07_passing_primitivetypeasobjects
9781111530532 ppt ch07_passing_primitivetypeasobjects
 
Chap10
Chap10Chap10
Chap10
 
Chap03
Chap03Chap03
Chap03
 
JavaYDL3
JavaYDL3JavaYDL3
JavaYDL3
 
Chapter 1.3
Chapter 1.3Chapter 1.3
Chapter 1.3
 
9781111530532 ppt ch06
9781111530532 ppt ch069781111530532 ppt ch06
9781111530532 ppt ch06
 
Head First Java Chapter 2
Head First Java Chapter 2Head First Java Chapter 2
Head First Java Chapter 2
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17
 
JavaYDL16
JavaYDL16JavaYDL16
JavaYDL16
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16
 
Intermediate Java Programming
Intermediate Java ProgrammingIntermediate Java Programming
Intermediate Java Programming
 
Code complete chapter 19, 20 organize
Code complete chapter 19, 20 organizeCode complete chapter 19, 20 organize
Code complete chapter 19, 20 organize
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
 
Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05
 
Big Java Chapter 1
Big Java Chapter 1Big Java Chapter 1
Big Java Chapter 1
 

Similar to Chapter 03

Intro to LV in 3 Hours for Control and Sim 8_5.pptx
Intro to LV in 3 Hours for Control and Sim 8_5.pptxIntro to LV in 3 Hours for Control and Sim 8_5.pptx
Intro to LV in 3 Hours for Control and Sim 8_5.pptxDeepakJangid87
 
Nose Dive into Apache Spark ML
Nose Dive into Apache Spark MLNose Dive into Apache Spark ML
Nose Dive into Apache Spark MLAhmet Bulut
 
3-Tier Architecture Step By Step Exercises
3-Tier Architecture Step By Step Exercises3-Tier Architecture Step By Step Exercises
3-Tier Architecture Step By Step ExercisesMiranda Anderson
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm. Abdul salam
 
Universal metrics with Apache Beam
Universal metrics with Apache BeamUniversal metrics with Apache Beam
Universal metrics with Apache BeamEtienne Chauchot
 
Lab exam question_paper
Lab exam question_paperLab exam question_paper
Lab exam question_paperKuntal Bhowmick
 
online examination portal project presentation
online examination portal project presentationonline examination portal project presentation
online examination portal project presentationShobhit Jain
 
Guiding through a typical Machine Learning Pipeline
Guiding through a typical Machine Learning PipelineGuiding through a typical Machine Learning Pipeline
Guiding through a typical Machine Learning PipelineMichael Gerke
 
Spark Summit EU talk by Ram Sriharsha and Vlad Feinberg
Spark Summit EU talk by Ram Sriharsha and Vlad FeinbergSpark Summit EU talk by Ram Sriharsha and Vlad Feinberg
Spark Summit EU talk by Ram Sriharsha and Vlad FeinbergSpark Summit
 
lecture_3.ppt
lecture_3.pptlecture_3.ppt
lecture_3.pptFarhan646135
 
Scalable Similarity-Based Neighborhood Methods with MapReduce
Scalable Similarity-Based Neighborhood Methods with MapReduceScalable Similarity-Based Neighborhood Methods with MapReduce
Scalable Similarity-Based Neighborhood Methods with MapReducesscdotopen
 
CIS110 Computer Programming Design Chapter (9)
CIS110 Computer Programming Design Chapter  (9)CIS110 Computer Programming Design Chapter  (9)
CIS110 Computer Programming Design Chapter (9)Dr. Ahmed Al Zaidy
 
Problem-solving and design 1.pptx
Problem-solving and design 1.pptxProblem-solving and design 1.pptx
Problem-solving and design 1.pptxTadiwaMawere
 
College_Tech-seminar_2024_Indrajith.pptx
College_Tech-seminar_2024_Indrajith.pptxCollege_Tech-seminar_2024_Indrajith.pptx
College_Tech-seminar_2024_Indrajith.pptxIndrajithN1
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxSandeep Singh
 
Python for Data Analysis.pdf
Python for Data Analysis.pdfPython for Data Analysis.pdf
Python for Data Analysis.pdfJulioRecaldeLara1
 

Similar to Chapter 03 (20)

Intro to LV in 3 Hours for Control and Sim 8_5.pptx
Intro to LV in 3 Hours for Control and Sim 8_5.pptxIntro to LV in 3 Hours for Control and Sim 8_5.pptx
Intro to LV in 3 Hours for Control and Sim 8_5.pptx
 
8.unit-1-fds-2022-23.pptx
8.unit-1-fds-2022-23.pptx8.unit-1-fds-2022-23.pptx
8.unit-1-fds-2022-23.pptx
 
Nose Dive into Apache Spark ML
Nose Dive into Apache Spark MLNose Dive into Apache Spark ML
Nose Dive into Apache Spark ML
 
3-Tier Architecture Step By Step Exercises
3-Tier Architecture Step By Step Exercises3-Tier Architecture Step By Step Exercises
3-Tier Architecture Step By Step Exercises
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
 
Unit 1 dsa
Unit 1 dsaUnit 1 dsa
Unit 1 dsa
 
Universal metrics with Apache Beam
Universal metrics with Apache BeamUniversal metrics with Apache Beam
Universal metrics with Apache Beam
 
Lab exam question_paper
Lab exam question_paperLab exam question_paper
Lab exam question_paper
 
online examination portal project presentation
online examination portal project presentationonline examination portal project presentation
online examination portal project presentation
 
Guiding through a typical Machine Learning Pipeline
Guiding through a typical Machine Learning PipelineGuiding through a typical Machine Learning Pipeline
Guiding through a typical Machine Learning Pipeline
 
Spark Summit EU talk by Ram Sriharsha and Vlad Feinberg
Spark Summit EU talk by Ram Sriharsha and Vlad FeinbergSpark Summit EU talk by Ram Sriharsha and Vlad Feinberg
Spark Summit EU talk by Ram Sriharsha and Vlad Feinberg
 
lecture_3.ppt
lecture_3.pptlecture_3.ppt
lecture_3.ppt
 
06 procedures
06 procedures06 procedures
06 procedures
 
Python for data analysis
Python for data analysisPython for data analysis
Python for data analysis
 
Scalable Similarity-Based Neighborhood Methods with MapReduce
Scalable Similarity-Based Neighborhood Methods with MapReduceScalable Similarity-Based Neighborhood Methods with MapReduce
Scalable Similarity-Based Neighborhood Methods with MapReduce
 
CIS110 Computer Programming Design Chapter (9)
CIS110 Computer Programming Design Chapter  (9)CIS110 Computer Programming Design Chapter  (9)
CIS110 Computer Programming Design Chapter (9)
 
Problem-solving and design 1.pptx
Problem-solving and design 1.pptxProblem-solving and design 1.pptx
Problem-solving and design 1.pptx
 
College_Tech-seminar_2024_Indrajith.pptx
College_Tech-seminar_2024_Indrajith.pptxCollege_Tech-seminar_2024_Indrajith.pptx
College_Tech-seminar_2024_Indrajith.pptx
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
 
Python for Data Analysis.pdf
Python for Data Analysis.pdfPython for Data Analysis.pdf
Python for Data Analysis.pdf
 

More from Chaffey College

Strings Objects Variables
Strings Objects VariablesStrings Objects Variables
Strings Objects VariablesChaffey College
 
Social networks and games
Social networks and gamesSocial networks and games
Social networks and gamesChaffey College
 
The games factory 2 alien wars
The games factory 2 alien warsThe games factory 2 alien wars
The games factory 2 alien warsChaffey College
 
Ch 8 introduction to data structures
Ch 8 introduction to data structuresCh 8 introduction to data structures
Ch 8 introduction to data structuresChaffey College
 
Ch 8 data structures in alice
Ch 8  data structures in aliceCh 8  data structures in alice
Ch 8 data structures in aliceChaffey College
 
Power point unit d
Power point unit dPower point unit d
Power point unit dChaffey College
 
Power point unit c
Power point unit cPower point unit c
Power point unit cChaffey College
 
Power point unit b
Power point unit bPower point unit b
Power point unit bChaffey College
 
Power point unit a
Power point unit aPower point unit a
Power point unit aChaffey College
 
Ch 6 text and sound in alice
Ch 6 text and sound in aliceCh 6 text and sound in alice
Ch 6 text and sound in aliceChaffey College
 
Ch 5 boolean logic
Ch 5 boolean logicCh 5 boolean logic
Ch 5 boolean logicChaffey College
 
Ch 5 boolean logical in alice
Ch 5  boolean logical in aliceCh 5  boolean logical in alice
Ch 5 boolean logical in aliceChaffey College
 
Game maker objects
Game maker objectsGame maker objects
Game maker objectsChaffey College
 

More from Chaffey College (20)

Strings Objects Variables
Strings Objects VariablesStrings Objects Variables
Strings Objects Variables
 
Ruby Chapter 2
Ruby Chapter 2Ruby Chapter 2
Ruby Chapter 2
 
Social networks and games
Social networks and gamesSocial networks and games
Social networks and games
 
Serious games
Serious gamesSerious games
Serious games
 
The games factory 2 alien wars
The games factory 2 alien warsThe games factory 2 alien wars
The games factory 2 alien wars
 
Target markets
Target marketsTarget markets
Target markets
 
Ch 8 introduction to data structures
Ch 8 introduction to data structuresCh 8 introduction to data structures
Ch 8 introduction to data structures
 
Ch 8 data structures in alice
Ch 8  data structures in aliceCh 8  data structures in alice
Ch 8 data structures in alice
 
Ch 7 recursion
Ch 7 recursionCh 7 recursion
Ch 7 recursion
 
Intro to gml
Intro to gmlIntro to gml
Intro to gml
 
Power point unit d
Power point unit dPower point unit d
Power point unit d
 
Power point unit c
Power point unit cPower point unit c
Power point unit c
 
Power point unit b
Power point unit bPower point unit b
Power point unit b
 
Power point unit a
Power point unit aPower point unit a
Power point unit a
 
Gamegraphics
GamegraphicsGamegraphics
Gamegraphics
 
Gamesound
GamesoundGamesound
Gamesound
 
Ch 6 text and sound in alice
Ch 6 text and sound in aliceCh 6 text and sound in alice
Ch 6 text and sound in alice
 
Ch 5 boolean logic
Ch 5 boolean logicCh 5 boolean logic
Ch 5 boolean logic
 
Ch 5 boolean logical in alice
Ch 5  boolean logical in aliceCh 5  boolean logical in alice
Ch 5 boolean logical in alice
 
Game maker objects
Game maker objectsGame maker objects
Game maker objects
 

Recently uploaded

Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel AraĂşjo
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vĂĄzquez
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

Chapter 03

  • 1. Java Programming, 3e Concepts and Techniques Chapter 3 Manipulating Data Using Methods
  • 2. 2Chapter 3: Manipulating Data Using Methods 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
  • 3. 3Chapter 3: Manipulating Data Using Methods 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
  • 4. 4Chapter 3: Manipulating Data Using Methods 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
  • 5. 5Chapter 3: Manipulating Data Using Methods 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
  • 6. 6Chapter 3: Manipulating Data Using Methods 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
  • 7. 7Chapter 3: Manipulating Data Using Methods 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
  • 8. 8Chapter 3: Manipulating Data Using Methods (b) console application using dialog boxes (a) console application in a command prompt window (c) applet
  • 9. 9Chapter 3: Manipulating Data Using Methods
  • 10. 10Chapter 3: Manipulating Data Using Methods Problem Analysis • Convert user input to metric measurements • Calculate the BMI • Display the result
  • 11. 11Chapter 3: Manipulating Data Using Methods 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
  • 12. 12Chapter 3: Manipulating Data Using Methods
  • 13. 13Chapter 3: Manipulating Data Using Methods 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
  • 14. 14Chapter 3: Manipulating Data Using Methods Coding the Program
  • 15. 15Chapter 3: Manipulating Data Using Methods 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
  • 16. 16Chapter 3: Manipulating Data Using Methods
  • 17. 17Chapter 3: Manipulating Data Using Methods Declaring Variables
  • 18. 18Chapter 3: Manipulating Data Using Methods 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
  • 19. 19Chapter 3: Manipulating Data Using Methods 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
  • 20. 20Chapter 3: Manipulating Data Using Methods 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
  • 21. 21Chapter 3: Manipulating Data Using Methods
  • 22. 22Chapter 3: Manipulating Data Using Methods 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);
  • 23. 23Chapter 3: Manipulating Data Using Methods Assignment Statements • General syntax: location = value
  • 24. 24Chapter 3: Manipulating Data Using Methods Arithmetic Operators
  • 25. 25Chapter 3: Manipulating Data Using Methods 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
  • 26. 26Chapter 3: Manipulating Data Using Methods Comparison Operators • A comparison operation results in a true or false value that can be stored in a boolean variable
  • 27. 27Chapter 3: Manipulating Data Using Methods 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
  • 28. 28Chapter 3: Manipulating Data Using Methods 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 (==, !=)
  • 29. 29Chapter 3: Manipulating Data Using Methods 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
  • 30. 30Chapter 3: Manipulating Data Using Methods 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
  • 31. 31Chapter 3: Manipulating Data Using Methods The Math Class
  • 32. 32Chapter 3: Manipulating Data Using Methods Using Variables in Output
  • 33. 33Chapter 3: Manipulating Data Using Methods 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
  • 34. 34Chapter 3: Manipulating Data Using Methods 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
  • 35. 35Chapter 3: Manipulating Data Using Methods Swing Dialog Boxes • Dialog boxes are created with the JOptionPane “show” methods • The showInputDialog() and showConfirmDialog return a String containing the user input
  • 36. 36Chapter 3: Manipulating Data Using Methods Swing Dialog Boxes
  • 37. 37Chapter 3: Manipulating Data Using Methods 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
  • 38. 38Chapter 3: Manipulating Data Using Methods 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
  • 39. 39Chapter 3: Manipulating Data Using Methods 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
  • 40. 40Chapter 3: Manipulating Data Using Methods Moving to the Web • Every event class has one or more associated listener interfaces
  • 41. 41Chapter 3: Manipulating Data Using Methods Moving to the Web
  • 42. 42Chapter 3: Manipulating Data Using Methods 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
  • 43. 43Chapter 3: Manipulating Data Using Methods The init() Method • Initializes the window color and graphic • Adds components to the applet window • Registers the Button’s ActionListener
  • 44. 44Chapter 3: Manipulating Data Using Methods 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()
  • 45. 45Chapter 3: Manipulating Data Using Methods The paint() Method • Draws the initialized image in the applet window
  • 46. 46Chapter 3: Manipulating Data Using Methods Creating an HTML Host Document for an Interactive Applet • Compile the applet • Write an HTML Host Document to execute the applet – Use the <APPLET> 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
  • 47. 47Chapter 3: Manipulating Data Using Methods 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
  • 48. 48Chapter 3: Manipulating Data Using Methods 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
  • 49. 49Chapter 3: Manipulating Data Using Methods 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
  • 50. 50Chapter 3: Manipulating Data Using Methods 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
  • 51. 51Chapter 3: Manipulating Data Using Methods 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
  • 52. Java Programming, 3e Concepts and Techniques Chapter 3 Complete