Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Arab Academy for Sciience and T

Technology and Maritim


me Transpo
ort
Colleege of Com
mputing andd Informatiion Techno
ology
Lecturrer: Dr. Walid M. Ally
Coursse: Object- Oriented
d Programmiing (CS243)

Sheet 5
I- Arrray

1-How do you declare and creeate an arrayy? How do you access elements off an array? What is thee representattion of the
third eleement in ann array nameed a? Whatt is the array
y index typee? What is the
t lowest in
ndex?How can you knoow the
length of
o an array??

2. Indiccate true or false for thee following statements:


Every elemment in an arrray has thee same type..
The array size
s is fixed d after it is declared.
d
The array size
s is fixed d after it is created.
c
The elemennts in an arrray must be of primitive data type..

3. Whicch of the folllowing stattements are valid array


y declaration
ns?
int i = neww int(30);
double d[] = new dou uble[30];
char[] r = new
n char(1 1..30);
int i[] = (3,, 4, 3, 2);
float f[] = {2.3, 4.5, 6..6};
char[] c = new char());

4-(Prinnting distincct numbers) Write a pro ogram that rreads in ten numbers an nd displays distinct
numberrs (i.e., if a nnumber app pears multipple times, it is displayed
d only oncee). Hint: Reaad a number
and storre it to an arrray if it is new.
n If the number is aalready in thhe array, disscard it. Aftter the
input, th
he array conntains the distinct
d nummbers.
II pass by Value vs. pass by reference
1- Show the printout of the following codes:

i- ii-
class A class Circle
{ {
int i; double radius;
static int j; Circle(double r) {
A(int i) radius = r;
{ }}
this.i=i; public class Test
j++; {
} public static void increment(Circle x) {
void m1(A a1) x.radius++;
{ }
a1.i++; public static void increment(double i) {
a1.j++; i++;
} }
} public static void main(String[] args) {
class B Circle c1 = new Circle(1.0);
{ Circle c2 = new Circle(2.0);
public static void main (String [] arg) System.out.println("Before calling: c1 = " + c1.radius +" c2
{ = " + c2.radius);
A a1=new A(4); increment(c1);
A a2=new A(7); double r=c2.radius;
a1.m1(a1); increment(r);
System.out.println("a1:"+"i="+a1.i+" j="+a1.j); System.out.println("After calling: c1 = " + c1.radius +" c2 =
System.out.println("a2:"+"i="+a2.i+" j="+a2.j); " + c2.radius);
} }
} }

iii-
public static void main (String [] arg)
{
int [] list={1,2,3,4,5};
m(list);
for(int i=0;i<list.length;i++)
System.out.println(list[i]);
}

public static void m(int [] arr)


{
for(int i=0;i<arr.length;i++)
arr[i]=arr[i]+5;
}
}

You might also like