1 class 6. 2 objectives objectives identify, declare, and use primitive data types use the system...
TRANSCRIPT
![Page 1: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/1.jpg)
1
Class 6Class 6
![Page 2: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/2.jpg)
2
ObjectivesObjectivesIdentify, declare, and use primitive
data typesUse the System class to create data
streamsUse the Scanner class of functions
to handle user input
![Page 3: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/3.jpg)
3
Chapter ObjectivesChapter ObjectivesConvert strings to numbers using the
parse() methodUse assignment statements to store data
with proper identifiersUse operators and parentheses correctly
in numeric and conditional expressionsRound an answer using the round()
method of the Math class
![Page 4: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/4.jpg)
4
IntroductionIntroductionData are collections of raw facts or figuresA program performs operations on input
data to output informationInput data can come from a variety of
sources
– The program itself
– Users of the program
– External files
![Page 5: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/5.jpg)
5
VariablesVariables
Variables are Variables are like storage like storage locations in locations in the computer’s the computer’s memory.memory.
![Page 6: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/6.jpg)
6
Naming Rules of VariablesNaming Rules of VariablesFirst character must be one of the letters
a-z, A-Z, or an underscore ( _ ) or a $
After first character use a-z, A-Z, 0-9, underscore ( _ ) or $
Any length
Keep them meaningful
![Page 7: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/7.jpg)
7
Variable Rules (con’t)Variable Rules (con’t)Case sensitive
– ItemsOrder does not equal itemsorder
Cannot declare two variables of the same name in a method
Cannot give a variable the same name as a method
![Page 8: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/8.jpg)
8
Intrinsic Data TypesIntrinsic Data TypesJAVA has eight intrinsic data types, which form the basis for all other data types (i.e., classes):
1. boolean:- a 1 byte value that is assigned a value of either true or false (both are JAVA defined values). Boolean values have no direct conversion to integer values, and are initialized to false.
![Page 9: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/9.jpg)
9
Intrinsic Data TypesIntrinsic Data Types2. byte:
- a 1 byte integer, capable of representing numbers from numbers from -128 to +127. It is initialized to 0.
3. char:- a 2 byte integer that is normally used to represent character values using the Unicode system (the first 128 values of which correspond to ASCII values). It is initialized to \u0000.
![Page 10: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/10.jpg)
10
Intrinsic Data TypesIntrinsic Data Types4. short:
- a 2 byte integer, able to represent numbers between -32K and +32K. It is initialized to 0.
5. int:- a 4 byte integer, able to represent numbers between -2 billion and +2 billion. It is initialized to 0.
![Page 11: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/11.jpg)
11
Intrinsic Data TypesIntrinsic Data Types6. long:
- an 8 byte integer, capable of representing numbers between -2 and +2 . It is initialized to 0.
7. float:- a 4 byte IEEE format real number, giving about 7 decimal digits of precision. It is initialized to 0.0f.
63 63
![Page 12: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/12.jpg)
12
Intrinsic Data TypesIntrinsic Data Types
8. double:- an 8 byte IEEE format real number, giving about 15 decimal digits of precision. It is initialized to 0.0d.
![Page 13: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/13.jpg)
13
Key JAVA OperatorsKey JAVA OperatorsOperator
Description
Example
( )
Parenthesis
(X+2)/3
![Page 14: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/14.jpg)
14
Key JAVA OperatorsKey JAVA OperatorsOperator
Description
Example
*
Multiply
X*2
![Page 15: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/15.jpg)
15
Key JAVA OperatorsKey JAVA OperatorsOperator
Description
Example
/
Divide
X/12
![Page 16: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/16.jpg)
16
Key JAVA OperatorsKey JAVA OperatorsOperator
Description
Example
%
Modulus
7%3
![Page 17: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/17.jpg)
17
Key JAVA OperatorsKey JAVA OperatorsOperator
Description
Example
+
Add
X+7
![Page 18: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/18.jpg)
18
Key JAVA OperatorsKey JAVA OperatorsOperator
Description
Example
-
Subtract
X-6
![Page 19: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/19.jpg)
19
Key JAVA OperatorsKey JAVA OperatorsOperator
Description
Example
>
Greater than
X>3
![Page 20: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/20.jpg)
20
Key JAVA OperatorsKey JAVA OperatorsOperator
Description
Example
<
Less than
X<3
![Page 21: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/21.jpg)
21
Key JAVA OperatorsKey JAVA OperatorsOperator
Description
Example
>=
Greater than or equal
X>=3
![Page 22: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/22.jpg)
22
Key JAVA OperatorsKey JAVA OperatorsOperator
Description
Example
<=
Less than or equal
X<=3
![Page 23: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/23.jpg)
23
Key JAVA OperatorsKey JAVA OperatorsOperator
Description
Example
==
Equals
X==3
![Page 24: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/24.jpg)
24
Key JAVA OperatorsKey JAVA OperatorsOperator
Description
Example
!=
Not equal
X!=3
![Page 25: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/25.jpg)
25
Key JAVA OperatorsKey JAVA OperatorsOperator
Description
Example
=
Assignment operator
Y=X+3
![Page 26: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/26.jpg)
26
The Body Mass Index CalculatorThe 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
![Page 27: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/27.jpg)
27
The Body Mass Index CalculatorThe Body Mass Index Calculator
Three versions
– Input/Output using the command prompt
– Input/Output using dialog boxes
– Web environments use an applet interface
![Page 28: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/28.jpg)
28
(b) console application using dialog boxes
(a) console application in a command prompt window
(c) applet
![Page 29: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/29.jpg)
29
![Page 30: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/30.jpg)
30
Problem AnalysisProblem Analysis Convert user input to metric measurements Calculate the BMI Display the result
![Page 31: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/31.jpg)
31
Design the SolutionDesign the SolutionDesign the three kinds of user interfaces
with storyboardsDesign the logic of the program
– Use pseudocode for sequential flow for all programs
– Use an event diagram for the appletValidate the design
– Compare the program design with the original requirements
![Page 32: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/32.jpg)
32
![Page 33: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/33.jpg)
33
Coding the ProgramCoding the ProgramImport 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
![Page 34: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/34.jpg)
34
Coding the ProgramCoding the Program
![Page 35: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/35.jpg)
35
Storing DataStoring DataJava 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
![Page 36: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/36.jpg)
36
![Page 37: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/37.jpg)
37
Declaring VariablesDeclaring Variables
![Page 38: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/38.jpg)
38
User Input – Streams and the System User Input – Streams and the System ClassClass 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
![Page 39: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/39.jpg)
39
User Input – Streams and the System User Input – Streams and the System ClassClass 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
![Page 40: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/40.jpg)
40
Using the BufferedReader classUsing 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
![Page 41: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/41.jpg)
41
![Page 42: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/42.jpg)
42
User Prompts, Inputs, User Prompts, Inputs, and Conversionsand 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);
![Page 43: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/43.jpg)
43
Assignment StatementsAssignment StatementsGeneral syntax: location = value
![Page 44: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/44.jpg)
44
Arithmetic OperatorsArithmetic Operators
![Page 45: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/45.jpg)
45
Arithmetic OperatorsArithmetic 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
![Page 46: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/46.jpg)
46
Comparison OperatorsComparison Operators A comparison operation results in a true or false
value that can be stored in a boolean variable
![Page 47: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/47.jpg)
47
Numeric ExpressionsNumeric 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
![Page 48: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/48.jpg)
48
Conditional ExpressionsConditional 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 (==, !=)
![Page 49: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/49.jpg)
49
Parentheses in ExpressionsParentheses 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
![Page 50: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/50.jpg)
50
Construction of Error-Free Construction of Error-Free ExpressionsExpressions 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
![Page 51: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/51.jpg)
51
The Math ClassThe Math Class
![Page 52: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/52.jpg)
52
Using Variables in OutputUsing Variables in Output
![Page 53: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/53.jpg)
53
Compiling, Running, and Compiling, Running, and Documenting the ApplicationDocumenting the ApplicationCompile the Body Mass Index
Calculator programExecute the programTest the program by entering the
sample input data supplied in the requirements phase at the prompts
Verify the resultsPrint the source code and screen
images for documentation
![Page 54: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/54.jpg)
54
Using Swing ComponentsUsing Swing ComponentsSave the previous version of the Body
Mass Index Calculator with a new filenameImport 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
![Page 55: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/55.jpg)
55
Swing Dialog BoxesSwing Dialog Boxes Dialog boxes are created with the JOptionPane
“show” methods The showInputDialog() and showConfirmDialog
return a String containing the user input
![Page 56: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/56.jpg)
56
Swing Dialog BoxesSwing Dialog Boxes
![Page 57: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/57.jpg)
57
Closing Programs that use SwingClosing Programs that use SwingSystem.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
![Page 58: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/58.jpg)
58
Saving, Compiling, and Running the Saving, Compiling, and Running the Swing VersionSwing VersionVerify that the file name matches the
class name at the beginning of the codeCompile the source codeTest with the same sample data for all
versions to compare output resultsIf incorrect or unrealistic data is entered
by the user, errors will occur– Errors and exception handling will be
discussed in a later chapter
![Page 59: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/59.jpg)
59
Moving to the Web 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
![Page 60: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/60.jpg)
60
Moving to the WebMoving to the Web Every event class has one or more
associated listener interfaces
![Page 61: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/61.jpg)
61
Moving to the WebMoving to the Web
![Page 62: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/62.jpg)
62
Adding Interface Components Adding Interface Components to an Appletto 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
![Page 63: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/63.jpg)
63
The init() MethodThe init() Method Initializes the window color and graphic Adds components to the applet window Registers the Button’s ActionListener
![Page 64: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/64.jpg)
64
The actionPerformed() MethodThe 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()
![Page 65: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/65.jpg)
65
The paint() MethodThe paint() Method Draws the initialized image in the applet window
![Page 66: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/66.jpg)
66
Creating an HTML Host Document Creating an HTML Host Document for an Interactive Appletfor an Interactive AppletCompile the appletWrite 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
![Page 67: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/67.jpg)
67
File ManagementFile 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
![Page 68: 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](https://reader035.vdocuments.site/reader035/viewer/2022062322/5697bfee1a28abf838cb9758/html5/thumbnails/68.jpg)
68
Conclusion Conclusion of Class 4of Class 4