Professional Documents
Culture Documents
Sheet 5
Sheet 5
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??
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]);
}