Title: Dependability Modelling of Layered Systems
1Variables and Data Types
2Variables must have a type and a name. int count
type
name
- Two kinds of variables
- Primitive type
- Object Reference type
3Primitive Types
Type Bit Depth Value Range boolean (JVM
specific) true or false char 16 bits 0 to
65535 byte 8 bits -128 to 127 short 16
bits -32268 to 32767 int 32 bits -2147483648
to 2147483647 long 64 bits -huge to
huge float 32 bits varies double 64
bits varies
4Primitive Declarations with assignments
int x x 234 byte b 89 double d
34.98 char c f int z x boolean found
true long b 3456789 float f 32.5f
5Which conversions are legal ?? int x 24 byte b
x byte b 127 int j b
6- Primitive byte, short, int, long, float,
double, char, boolean. -
- int x
- x 6
6
x
7public class Circle int radius
Circle(int j) radius j
int getRadius() return radus
- Object Reference (Non-primitive)
- Circle y
- y new Circle(5)
a Circle object
5
radius
y
8- Primitive byte, short, int, long, float,
double, char, boolean. - int x
- x 6
- int y x
- Reference (Non-primitive)
- Circle p
- p new Circle(5)
- Circle q p
6
x
6
y
p and q are aliases
a Circle object
5
radius
p
q
9Arrays
- objects that help us organize large amount of
information - An array is a structure that can hold multiple
values of the same type. - After creation, an array is a fixed-length
structure. - is an object in Java
10Arrays Example
public class ArrayTest public static void
main(String args) int ia new
int4 for (int i 0 i lt ia.length
i) iai i
for (int i 0 i lt ia.length i)
System.out.println( iai )
Answer 0 1 2 3
11Bounds Checking
- Once an array is created, it has a fixed size
- the index value must be in range 0 to N-1
- The Java interpreter throws an ArrayIndexOutOfBoun
dsException if an array index is out of bounds - This is called automatic bounds checking
12Bounds Checking
- common to introduce off-by-one errors when using
arrays - int codes new int100
- for (int index0 index lt 100 index)
- codesindex index50
- int codes new int100
- for (int index0 index lt codes.length index)
- codesindex index50
13Alternate Array Syntax
- Therefore the following two declarations are
equivalent - float prices
- float prices
- The first format generally is more readable and
should be used
14Initializer lists Example
public class Primes public static void main
(String args) int primeNums 2, 3, 5,
7, 11, 13, 17, 19 System.out.println
("Array length " primeNums.length)
System.out.println (First few prime numbers
are") for (int prime primeNums)
System.out.print (prime " ")
Answer Array length 8 First few prime numbers
are 2 3 5 7 11 13 17 19
15Exercise
int marks marks new int3 marks0 23
marks1 45 marks2 90 System.out.println(
marks0 ) System.out.println( marks1
) System.out.println( marks2 )
Output ??
Answer 23 45 90
16Exercise
System.out.println( marks1 ) System.out.println
( marks2 ) System.out.println( marks3 )
Answer
Which statement will generate ArrayIndexOutOfBound
sException ??
17Example
int marks 23, 45, 90
Compilation error will occur if int marks
marks 23, 45, 90
18Exercise 1
int values 45, 67, 79, 98, 24 Q1. What is
the index of 79 ? Q2. What is the expression that
refers to 79? Q3. What is the value of expression
values.length ? Q4. What is the index of
last element ? Q5. What is the value of values4
?
2
values2
5
4
24
19Exercise 2
Find and correct the error int b new
int10 for( int j 0 j lt b.length j
bj 1
should be lt instead of lt
20Arrays of Objects
- The elements of an array can be object
references - String words new String3
- reserves space to store 3 references to String
objects - DOES NOT create the String objects
- each object must be instantiated separately
21Arrays of Objects
words0 new String(planes) words1 new
String(trains) words2 new
String(automobiles)
planes
words
trains
auotmobiles
22Arrays of Objects Use of Initializer list
String places Toronto, Ottawa String
places new String(Toronto),
new String(Ottawa)
Toronto
places
Ottawa
23Example-1
public class Grade private String level
public Grade(String level)
this.level level public void
setLevel(String l) level l public
String getLevel() return level public
String toString() return level
24public class GradeTest public static void
main(String args) Grade grades
new Grade(A),
new Grade(A),
new Grade(A-)
for( Grade g grades )
System.out.println( g )
Output ??
Answer A A A-
25Example-2
Tunes
UML Class Diagram
CD
CDCollection
1
26public class CD private String title,
singer public CD(String title, String
singer) this.title title this.singer
singer public String toString()
return title \t singer
27public class CDCollection private CD cds
private int count public
CDCollection() cds new CD2
count 0
28 public addCD(String title, String singer)
if( count cds.length )
increaseSize() cdscount new CD(
title, singer) count
29 private void increaseSize() CD
temp new CDcds.length 2 for(int j
0 j lt cds.length j) tempj
cds j cds temp
30 public String toString() String
result CD list \n
for(int j 0 j lt count j)
result cds j .toString() \n
return result
31public class Tunes public static void main(
String args ) CDCollection music new
CDCollection()
music.addCD(Storm Front, Billy Joel)
music.addCD(GraceLand, Paul Simon)
music.addCD(Double Live, Garth Brooks)
System.out.println( music )
Answer CD list Storm Front Billy
Joel Graceland Paul Simon Double Live
Garth Brooks
Output ??
32Two-dimensional Arrays
33Two-dimensional Arrays
- two-dimensional array is an array of arrays in
Java - int scores new int43
- System.out.println( scores02 )
Output ??
Answer 79
23
82
79
23
82
79
32
31
20
scores
32
31
20
22
55
65
34
22
55
65
43
61
34
43
61
34Two-dimensional Arrays
- initializer list can be used to instantiate
two-dimensional arrays - int scores 23, 82, 79,
- 32, 31, 20,
- 22, 55, 65,
- 34, 43, 61
23
82
79
scores
32
31
20
22
55
65
34
43
61
35public static void main (String args)
int table new int23 for (int row0
row lt table.length row) for (int
col0 col lt tablerow.length col)
tablerowcol row col
36 for (int row0 row lt table.length row)
for (int col0 col lt tablerow.length
col) System.out.print(tablerow
col "\t") System.out.println()
37 for (int row0 row lt table.length row)
for( int value tablerow )
System.out.print( value \t )
System.out.println()
38int table new int45
39Multi-dimensional Arrays
- Any array with more than one dimension is called
multi-dimensional array - Each dimension has its own length constant
- Since each dimension is an array of array
references, the arrays within one dimension can
be of different lengths - these are sometimes called ragged arrays