Download Tut4 - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Shagra University
College of Computer & Information Sciences
First Semester 1434/1435
Java: Introduction to Programming with Java
TUTORIAL # 5
Chap5: Control Structure II
PART I
Q1.
Trace the following program:
public static void main( String args[] ){
Scanner console
=
new Scanner(System.in);
int a, b, c =0;
boolean found = false;
System.out.print("Enter the first integer: ");
a = console.nextInt();
System.out.print("Enter the second integer: ");
b = console.nextInt();
while (!found && a < b){
if (a > a * b && 10 < b)
found = 2 * a > b;
else
{ found = 2 * a < b;
if (found)
a = 3;
c = 15;
if (b > 0)
{
b = 0;
a = 1;}}}
System.out.println("a = "+a+" , b= "+b+" , c= "+c);}
for ( i = 1; i <= 5; i++ )
{
for ( j = 1; j <= 3; j++ )
{
for ( k = 1; k <= 4; k++ )
System.out.print( '*' );
System.out.println();
} // end inner for
System.out.println();
} // end outer for
1
char c = 'A';
while ( c < 'Z')
{
System.out.print(c);
++c;
if ( c >= 'J') break;
System.out.print(", "); }
int x,n=5,i=0;
double p =0;
while ( n != 0)
{ x=n % 2;
n = n/2;
if (x ==0)
continue;
i +=1;
p+= x*Math.pow(10,i);
}
System.out. printf("%.2f",p/10);
2
Q2.
Find and correct the errors in the following segments:
a. For ( x = 100, x >= 1, x++ )
System.out.println( x );
b. The following code should output the even integers from 2 to 100:
counter = 2;
do {
System.out.println( counter );
counter += 2;
} While ( counter < 100 );
c. The following code should output the odd integers from 19 to 1:
for ( x = 19; x >= 1; x += 2 )
System.out.println( x );
d. The following code should print whether integer value is odd or even:
switch ( value % 2 ) {
case 0:
System.out.println( "Even integer" );
case 1:
System.out.println( "Odd integer" );
}
Q3.
3
Q4.
Convert the following code so that it uses nested while statements instead of for statements:
int s = 0;
int t = 1;
for (int i = 0; i < 10; i++)
{
s = s + i;
for (int j = i; j > 0; j--)
{
t = t * (j - i);
}
s = s * t;
System.out.println(“T is “ + t);
}
System.out.println(“S is “ + s);
Q5.
Write an application that calculates the product of the odd integers from 1 to 15.
Q6.
Write a while loop that sums the integers from 1 to 10, excluding 3 and 6. Print the sum.
Q7.
One interesting application of computers is to display graphs and bar charts. Write an application that
reads five numbers between 1 and 30. For each number that is read, your program should display the
same number of adjacent asterisks. For example, if your program reads the number 7, it should display
Q8.
Write a do… while loop that prints the integers from 10 to 0, inclusive.
Q9.
Assume i = 1, j = 2, k = 3 and m = 2. What does each of the following statements print? Are the
parentheses necessary in each case?
System.out.println( i == 1 );
System.out.println( j == 3 );
System.out.println( i >= 1 && j < 4 );
System.out.println( m <= 99 & k < m );
System.out.println( j >= i || k == m );
System.out.println( k + m < j | 3 - j >= k );
System.out.println( !( k > m ) );
4