Professional Documents
Culture Documents
Data Types, Constants and Variables
Data Types, Constants and Variables
1) Problem Specification
- identify user requirements Problem Analysis
2) Problem Analysis
- identify inputs, outputs, formulas
3) Program Design Program Design
- write solution steps
(using pseudocode or flowcharts)
- go through dry-run to test the Implementation
solution steps using test samples
4) Implementation
- translate solution into computer program Program Testing
5) Program Testing
- use test samples to test the program 2
Chapter 3
Data Types, Constants and Variables
• Computer Memory
• Data Types
• Literals
• Identifiers
• Constants
• Variables
• Programming Style
• Case Study
3
Computer Memory
Memory
Address
Memory Cells
1000 0 1 0 1 0 1 1 0
1001 Content of
1 0 1 0 0 1 0 1
1002
memory
0 1 0 1 1 0 1 0
1003
1004
To store
1005
1006 program
1007 instructions
1008
1009 and
data 4
Memory and Variables
for data objects
Memory
Address
1000 0 1 0 1 0 1 1 0
1001 Content of
1 0 1 0 0 1 0 1
1002
memory
0 1 0 1 1 0 1 0
1003
Program logic
1004
10 (for variable1)
1005 DataType Variable Value
Memory
1006 20 (for variable2) int variable1=10;
To store 1007
1008 int variable2=20;
data
1009 30 (for variable3) int variable3;
variable3 = variable1 + variable2;
5
….
Chapter 3
Data Types, Constants and Variables
• Computer Memory
• Data Types
• Literals
• Identifiers
• Constants
• Variables
• Programming Style
• Case Study
6
Data Types
• A program needs to work with data.
• A data object is of a certain type that requires the
specified memory storage size. The data types in Java
are
- Primitive Data Types Why Variables & Data Type?
- Reference Data Types
• A data object in a Java program can be
- a constant
Declared with Data Type
- a variable
String
Reference Types Class Types
(Class Type)
Interface Type
int
Character Type
Integer Types short
byte
long
8
Primitive Data Types
9
Integer Data Types
10
Floating-point Data Types
11
Character and Boolean Data Types
Character Data Type
• Any data object which is
-an English letter (a,..,z,A,..,Z)
-an English punctuation mark (!, ?, etc.)
-a decimal digit (0,..,9), or
-a symbol such as a space,
-etc…
• A storage space of 2 bytes needed
Boolean Data Type
• boolean literals: true or false
12
Reference Data Types
String Type
• A reference data type is used to store an
address of an object that can be used to refer to
the object.
• String Data Type
– Strings are represented by a sequence of
characters within double quotes, Data Type Quiz
e.g. “Hui Siu Cheung”, “Hello Students!”
– Strings are not primitive data types
– Java provides the String class with methods
13
Chapter 3
Data Types, Constants and Variables
• Computer Memory
• Data Types
• Literals
• Identifiers
• Constants
• Variables
• Programming Style
• Case Study
14
Literals
15
Character Literals
• A character is stored as 16-bit (2 bytes) unsigned
values using Unicode encoding scheme which is
established by the Unicode Consortium to support
interchange, processing, and display of text in
different languages such as Chinese and Korean.
1 LF FF CR
2 ESC
4 ( ) * + , - . / 0 1
5 2 3 4 5 6 7 8 9 : ;
6 < = > ? @ A B C D E
7 F G H I J K L M N O
8 P Q R S T U V W X Y
9 Z [ \ ] ^ _ ' a b c
10 d e f g h i j k l m
11 n o p q r s t u v w
12 x y z { | } ~ DEL
17
Examples of Escape Sequence
Example:
Output:
The value for pi is 3.14159 23
Chapter 3
Data Types, Constants and Variables
• Computer Memory
• Data Types
• Literals
• Identifiers
• Constants
• Variables
• Programming Style
• Case Study
24
Variables
Variables are data objects that may change and be
25
A variable name cannot be any of the keywords in Java.
26
Each variable has a data type such as int, float
and char.
Variables are declared by declaration statements.
A declaration can be done with or without
initialization.
A declaration statement without initialization has the
form
Type Variable[, Variable];
Variable Names
where Type can be int, float, or char, etc. Quiz
Variable is the name of the variable.
[...] may be repeated zero or more times.
27
Program: Variables without Initialization
Specification: Computing Income Tax
CONSTANTS
public class DeclareVariable {
static final int DEDUCTION = 2000; // Constant
static final double TAX_RATE = 0.2; // Constant
public static void main(String[ ] args){ VARIABLES
// Define Variables
double incomeTax, taxableIncome, grossSalary;
int numOfDependents, numOfChildren, numOfParents;
Memory
Variable Names
1000 (Memory Address)
numOfChildren (4 bytes)
If variables are not
1024 initialized with initial
numOfParents (4 bytes) values, Java will initialize
1028 the variables automatically:
numOfDependents (4 bytes) -numerical types -> 0
-Boolean -> false
1036
grossSalary
-Reference data type -> null
(8 bytes)
1044
taxableIncome (8 bytes)
1050
incomeTax (8 bytes) 30
Program: Variables with Initialization
public class DeclareVarInit {
// constants int numOfChildren = 2, numOfParents = 2, numOfDependents;
double grossSalary, taxableIncome, incomeTax;
static final int DEDUCTION = 2000;
static final double TAX_RATE = 0.2; Memory
public static void main(String[] args) { Variable Names
1000 (Memory Address)
// variables
numOfChildren 2 (4 bytes)
double incomeTax, taxableIncome, grossSalary;
int numOfDependents, numOfChildren=2, 1024
numOfParents 2 (4 bytes)
numOfParents=2;
// assignment statements 1028
numOfDependents (4 bytes)
grossSalary = 100000;
numOfDependents = numOfChildren + 1036
incomeTax);
} 31
}
Initialization can also be done as part of a declaration. This
means a variable is given a starting value when it is declared.
32
Chapter 3
Data Types, Constants and Variables
• Computer Memory
• Data Types
• Literals
• Identifiers
• Constants
• Variables
• Programming Style
• Case Study
33
Programming Style
• Make programs simple, readable and easy to
understand.
• Using Proper Programming Style
- should use indentation, blank spaces, blank lines.
- e.g. using indentation as follows:
public class DeclareVarInit { Use Generate CSD
// constants
static final int DEDUCTION = 2000; in jGRASP
static final double TAX_RATE = 0.2;
public static void main(String[] args){
// variables
int numOfChildren=2, numOfParents=2, numOfDependents;
double grossSalary, taxableIncome, incomeTax;
// assignment statements
grossSalary = 100000;
numOfDependents = numOfChildren + numOfParents;
taxableIncome =
grossSalary - numOfDependents*DEDUCTION;
incomeTax = taxableIncome * TAX_RATE;
System.out.println("The income tax is " + incomeTax);
}
} 34
Using Generate CSD in jGRASP
35
Programming Style
• Using Meaningful Names
- Class names – The first letter of each word in the class
name should be capitalized (e.g. CurrencyExchange).
- At the beginning of the program, put comments to say
what the program will do; your name and date.
Subsequently when the program is modified/extended,
put comments to record the modifications, who made
it, and the date of the modifications.
- If the purpose of a portion of the program is not very
clear at a glance, e.g. a nested loop or nested-if
statement, put comments to say what the program is
doing.
37
Chapter 3
Data Types, Constants and Variables
• Computer Memory
• Data Types
• Literals
• Identifiers
• Constants
• Variables
• Programming Style
• Case Study
38
Case Study
Finding Area and Circumference of a
Circle
Problem Specification
Circle
r Area
Radius(r) Circumference
Outputs:
The area is: 78.53975
The circumference is: 31.4159
42
Implementation
import java.util.Scanner;
public class ComputeCircle {
static final double PI = 3.14159; CONSTANT
public static void main(String[ ] args) {
double radius, area, circumference; VARIABLES
Scanner sc = new Scanner(System.in);
44
Key Terms
• data type
• primitive data type
• byte type, short type, int type, long type
• float type, double type
• character type
• boolean type
• reference data type
• String type
• literal
• identifier
• constant
• variable declaration
• declaration statement
• assignment operator, assignment statement
45
Review Exercise: Linear Interpolation
Problem Specification
Intercept: c y1 x 2 y 2 x 1
x 2 x1
Write a program to read in two points. Then, it finds the
slope and intercept of the straight line given by the
coordinates of the two points on the straight line. In addition,
the program will also read in the x-coordinate of a point,
and then find the y-coordinate of the point on the straight
46
line. ”
Problem Analysis
Required inputs: ______, ______ and _______
Required output: ___________ and _________
Formulas: slope = ________________,
intercept = ______________,
y = ___________________
System Design
Initial Algorithm: (Exercise)
Algorithm in Pseudocode
main()
READ __, __, __, ___, ___
COMPUTE ___ = ________________
COMPUTE ___ = ________________
COMPUTE ___ = ________________
PRINT _____. _____. _____ 47
Implementation
Import java.util.Scanner;
public class LinearInterpolate {
public static void main(String[] args){
// Declare variables
__ = ______;
__ = ______;
__ = ______;
// Print slope, intercept, y-ccordinate
49
Further Reading
50