View File - University of Engineering and Technology, Taxila

advertisement
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
LAB 04
Operators and Arrays
Operators
1. What is the value of i after executing the following code segments? Assume that the variables
have been initialized to i = 21, j = 42 and k = 11; Justify your answer.



i = 4 * (j + 16) % k
i=i-j+k*7
i = (i - j + k) * 7
2. What do each of the following print?

System.out.println(2 + "bc");

System.out.println(2 + 3 + "bc");

System.out.println((2+3) + "bc");

System.out.println("bc" + (2+3));

System.out.println("bc" + 2 + 3);

Explain each outcome.
3. Evaluate the expression 29+27*2/3-40.Store the result in int type variable named total.
4. Try it and see. -47 / 5 = -9 and -47 % 5 = -2. The quotient is always rounded toward zero.
5. Suppose that a and b are boolean values. Show that the expression (!(a && b) && (a || b)) ||
((a && b) || !(a || b)) is equivalent to true.
6. Try evaluating following expressions and print each result:

Int a=7;

Int b=6;
Object Oriented Programming
3rd Semester-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT

int c=a-b;

int d=a&b;

int e=a^b;

int f=(~a & b) |(a & ~b);

int g=~a &0x0f;

int e=~a & 0xf2;

byte Shft1=64,Shft2=0;

int i=Shft1<<2;

Shft2=(byte)(Shft1<<2);

byte Shft3=0xf1;

System.out.println(hex[Shft>>4]& 0xf1]);
Suppose int a= -1;

int b=a>>24;

a=a>>>24;
suppose b=1 and c=1;

a>>(b+3)

a|4+c>>b&5;

(a|(((4+c)>>b &7))’
7. Suppose that a and b are int values. Simplify the following expression: (!(a < b) && !(a > b))
8. Suppose that a variable a is declared as int a = 2147483647 (or equivalently,
Integer.MAX_VALUE). What do each of the following print?

System.out.println(a);
Object Oriented Programming
3rd Semester-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT

System.out.println(a + 1);

System.out.println(2 - a);

System.out.println(-2 - a);

System.out.println(2 * a);

System.out.println(4 * a);

Explain each outcome.
9. Suppose that a variable a is declared as double a = 3.14159. What do each of the following
print?

System.out.println(a);

System.out.println(a + 1);

System.out.println(8 / (int) a);

System.out.println(8 / a);

System.out.println(8 / a);

System.out.println((int) (8 / a);

Explain each outcome.
10. Write a program that takes two positive integers as command-line arguments and prints true if
either evenly divides the other.
11. Give the value of a after the execution of each of the following sequences:
int a = 1;
a = a + a;
a = a + a;
a = a + a;
boolean a = true; int a = 2;
a = !a;
a = a * a;
a = !a;
a = a * a;
a = !a;
a = a * a;
Explain each outcome
Arrays
Try to grasp the concept of Arrays with the help of following code.
Object Oriented Programming
3rd Semester-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
public class Array_1{
public static void main(String args[]){
int val[]=new int[10];
for (int i=0;i<10;i++)
{
val[i]=i;
System.out.println(val[i]);
}
}
12. What does the following code fragment print?
int [] a = new int[10];
for (int i = 0; i < 10; i++)
a[i] = 9 - i;
for (int i = 0; i < 10; i++)
a[i] = a[a[i]];
for (int i = 0; i < 10; i++)
System.out.println(a[i]);
13. What values does the following code put in the array a?
N = 10;
int[] a = new int[N];
a[0] = 0;
a[1] = 1;
for (int i = 0; i < N; i++)
a[i] = a[i-1] + a[i-2];
System.out.println(a[i]);
14. What does the following code fragment print?
int[] a = { 1, 2, 3 };
int[] b = { 1, 2, 3 };
System.out.println(a == b);
15. What happens when you try to compile a program with the following statement?
int[] a = new int[-17];
Task
1. Given the temperature t=32 (in Fahrenheit) and the wind speed v=100 (in miles per hour), the
National Weather Service defines the effective temperature (the wind chill) to be:
Object Oriented Programming
3rd Semester-SE
UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
W = 35.74 + 0.6215* t + (0.4275 *t - 35.75) v0.16
Prints out the wind chill
Hint:
Use Math.pow (a, b) = ab to compute v0.16. e.g Math.pow (v, 0.16)
2. Write a code fragment to transpose a square two-dimensional array in place without creating a
second array.
3. Write code fragments to create a two-dimensional array b[][] that is a copy of an existing twodimensional array a[][], under each of the following assumptions:
 a[][] is square
 a[][] is rectangular
 a[][] may be ragged
Ragged arrays. There is no requirement that all rows in a two-dimensional array have the same
length—an array with rows of nonuniform length is known as a ragged array. The possibility of
ragged arrays creates the need for more care in crafting array-processing code. For example, this
code prints the contents of a ragged array:
for (int i = 0; i < a.length) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
Object Oriented Programming
3rd Semester-SE
UET Taxila
Download