You are on page 1of 25

Cloud Computing Labs

Lab# 9 Java Type Casting and


Arrays in Java
Lecturers: Ali Munther, Muthanna
Jabbar
Fourth Class .
Department: Informatics Systems Management
(ISM) 2021
1
Lab Outline Outline

➢Conversion Between Java Data Types


➢Arrays in Java
❑One Dimension Arrays
❑Two Dimension Arrays

2
Outline

Conversion Between
Java Data Types

3
Conversion
Outline
Between Types
➢ The process of converging from one type to another is called
casting. It needed when some method return data type in
different form we needed.
There two type of casting :
➢ Widening casting (implicit): e.g. int i = 'A';
Byte Short
int long float double
char

➢ Narrowing casting (Explicit): (lowerType) higherValue


e.g. int i=(int)123.456;

Byte Short int long float double

4
Conversion Between
Outline
Types: Example 1

5
Conversion Between
Outline
Types: exercise

❖Exercise: Write the java Program to generates a


random integer in the range 2 to 100 and then
tests it for primality (number is prime or not).
❖Hints:
❖use the library java.util.Random
❖Create an Random object inside main
❖Use nextFloat(); method (this method is inside
Rundom object)
❖Use Math.floor() method

6
Java
Outline
Arrays

Arrays

One dimension Array's

7
Java Programming
Outline Language

❖ In this type of array's one variable will hold a number of values.


1
❖ For example : A = 𝑛 (σ𝑛𝑖=1 𝑥𝑖 ) ,
It is clear that 𝑥𝑖 will hold (n) values and finally will divide the ( sum ) of
𝑥𝑖 by the value of (n) to get the average, as shown:
1
A= (σ𝑛𝑖=1 𝑥𝑖 )
𝑛
1
A= 𝑛
( 𝑥1 + 𝑥2 + 𝑥3 + ⋯ + 𝑥𝑛 )
So each value will be stored in :
X[1], X[2], X[3], … , X[n]
❖ Suppose we want to represent a set of 5-numbers, say (35, 40, 20, 57, 19), by an array
variable number, then we need to create the variable number as follows;
" int number [] = new int[5];

8
Outline

❖ The above code line will order the computer to reserves five storage locations as shown
below: X[0]

X[1]

X[2]

X[3]

X[4]

❖ The values to the array elements will be assigned as follows:


X[0] = 35;
X[1] = 40;
X[2] = 20;
X[3] = 57;
X[4] = 19;

9
Java Programming
Outline Language

❖ Notes :
1. In Java subscription starts with the value [0].
2. The following are valid statements;
❑ aNumber = number[0] + 10;
❑ number[4] = number[0] + number[2];
❑ number[2] = x[5] + y[10];
❑ value[6] = number[i] * 3;

10
Java Programming
Outline Language

❑creating an array:
❑ To create an array we need to follow the steps bellow:
➢ Declaration of the array.
➢ Creating the memory locations.
➢ Putting values into the memory locations.
➢ Implementing any type operations on the array.

11
Java Programming
Outline Language
Declaration of an array:
❖ Array's in Java can be decleared in two forms:
✓ " type " " arrayname[]";
✓ " type " [] " arrayname ";
For example:
✓ int number[];
✓ float average[];
✓ int [] counter;
✓ float [] marks;
▪ Note :
Do not enter the size of the array in the declaration section.

12
Java Programming
Outline Language

▪ Creating the array:


❖ The following code line will use the " new " operator to create
the array:
" arrayname = new type[size];"
❖ Example:
✓ number = new int[5];
✓ average = new float[10];
❖ it is also possible to combine the two previous steps in one step
as shown below:
✓ " int number[] = new int[5]; "

13
Java Programming
Outline Language

Initialization of an array:
❖ Up to now we created an array an allocated the necessary memory for it, to initialize
the array we need to write the following code line:
" arrayname[memory location] = value "
❖ For example:
✓ number[0] = 35;
✓ number[1] = 40;
✓ and so on until all values are enterd to the array.

14
Java Programming
Outline Language
▪ Note :
❖ In Java the array must start with (0), num[0] but not num[1] or any other numbers.
❖ Unlike C, Java protect array's from ( overruns) and ( underruns), trying to access an array
bound, its boundaries will generate error massage.
❖ We can also initialize arrays automatically in the same way as the ordinary variables when
they are declared, as shown:
" type arrayname[] = { list of values}; "
For example:
int number[] = {35, 40, 20, 57, 19};

❖ Also we can assign an array object to another as shown:


int a[] = {1, 2, 3, };
int b[];
b = a;

as a result of the above code line the both arrays will have the same values.
15
Java Programming
Outline Language
❖ Loops are used to initialize the arrays to make it easy to enter the values to the array, as
shown below:

for ( int i = 0; i < 100; i++)


{
if ( i < 50 )
sum [i] = 0;
else
sum [i] = 1;
}

‫) وعداها سوف تكون القيمة واحد لكل القيم‬49 ‫ الى‬0 ( ‫في هذا المثال المخرج سوف يكون صفر لكل العناصر التي تبدا من‬
.)99 - 50 ( ‫التي في النطاق‬

16
Java Programming
Outline Language
▪ Note :
We can extract the size of an assay directly by implementing the following code
lene;
" int aSize = a.length; "

▪ H.W. :
Write a code to create two arrays and implement
the following operations :
1. Summation [] = A[] + B[].
2. Multiplication [] = A[] * B[].

17
Java Programming
Outline Language

Arrays

Two dimension Array's

18
Java Programming
Outline Language
• Two dimensional arrays:
▪ So far we have learned how to store a list of values using Java, but what if we have a
table of values we need to store it as shown below:
Item 1 Item 2 Item 3
Sales Personal 1 310 275 365
Sales Personal 2 210 190 325

Sales Personal 3 405 235 240

Sales Personal 4 260 300 380

▪ The above table contains a 12 values, three each line, so it is not useful to declare an
array for each line, so we need to declare a two dimension array, the way of handling it
is not very different from the type of one dimensional array as it will be shown below.

19
Java Programming
Outline Language
▪ For creating 2D arrays, we must follow the same steps as that of 1D arrays, we
may create a 2D array like the following code lines:

<Data type> <array name> [ ] [ ];


<array name> [ ] [ ] = new <data type> [r] [c];

Note that the <data type> is the kind of values the array will store, after giving
the data type give the array name and don’t declare any size for it, in the next
line we will use the "new" operator to initialize the size of the array, more over
"r" is the number of "rows", and "c" is the size of the columns.

20
Java Programming
Outline Language

Example:
int myArray [ ] [ ];
myArray = new int [3] [4];
OR
int table [ ] [ ] = new int[3] [4];

This will create a table that can store 12 integer values, four across and three down.

21
Java Programming
Outline Language
▪ Like the one dimensional array's the two dimensional array's may be initialized by
the following their declaration with a list of initial values enclosed in braces, for
example:
The following gives the same result:
1. int table [2] [3] = (0,0,0,1,1,1);
2. int table [] [] = { {0,0,0} , {1,1,1} };
3. int table [] [] = {
{0,0,0},
{1,1,1}
};

22
Java Programming
Outline Language

• Variable size array:


The declaration of this type of arrays is as follows:
" int X[] [] = new int[3] [];“

X[0] [1]
X[0] = new int [2];
X[1][3]
X[1] = new int [4];
X[2][2]
X[2] = new int [3];

23
Java Programming
Outline Language

• H.W.:
Create an array of 2D and build the following
array:

1 0 0 0 0

0 1 0 0 0

0 0 1 0 0

0 0 0 1 0

0 0 0 0 1

24
THANK YOU

25

You might also like