Tutorial 4 - Answers
1.Indicate the output that will be produced.
int x=5, y=10, z;
z = x++ * ++y;
System.out.println (x + " " + y + "
" + z);
Trace:
|
Steps |
x |
y |
z |
|
x=5; |
5 |
|
|
|
y=10; |
5 |
10 |
|
|
z; |
5 |
10 |
|
|
z=x++ * ++y; Break Down |
|
|
|
|
++y(do before evaluate) |
5 |
(1+y) 11 |
|
|
x++ (do after evaluation) |
5 |
11 |
|
|
x++ * ++y |
5 |
11 |
55 |
|
x++ (now can add 1 to x) |
(x+1) 6 |
11 |
55 |
OUTPUT: 6
11 55
2.Indicate the values stored in a and b AFTER the statement is
executed. Assume that these statements
are all executed on the same data:
int a = 3, b
= 9;
The first two are done for you:
|
Operation |
a=3 |
b=9 |
Operation |
a=3 |
b=9 |
|
++a; (a=1+a) |
4 |
9 |
b /= a; (b=b/a) |
3 |
3 |
|
a
+= b; (a = a + b) |
12 |
9 |
b
%= 2; (b=b%2) |
3 |
1 |
|
++b; (b=1+b) |
3 |
10 |
a
*= (b--); (a=a*b) (b=b-1) (after assignment) |
27 |
8 |
|
a
+= ++b; (a=a+ (1+b)) |
13 |
10 |
a
+= b++; (a=a+b) (b=b+1) |
12 |
10 |
3.Trace the following program and write the screen dialogue when this
program is executed. Assume the input
values are 4 and 5 respectively.
|
import java.io.*; public class Mystery { public static void main (String []args) throws IOException { int x, y; String inputString = ""; //read in string
from screen InputStreamReader reader = new
InputStreamReader(System.in); BufferedReader input = new BufferedReader(reader); System.out.println("What's up Doc?"); System.out.println("\nEnter the first integer:
"); inputString = input.readLine(); x = new Integer(inputString).intValue(); System.out.println("\nEnter the second
integer: "); inputString
= input.readLine(); y = new Integer(inputString).intValue(); if (x /2 ==0) x = x*5; else x = x/2; if (y/2 == 1) y = y*5; else y = y/2; x = x+y; y = y+x; System.out.println("\nThe mystery number is:
" + y); }//end main }//end class |
Variables x y 4 5 (4/2=2) ==0 FALSE do ELSE x=x/2 2 (y/2=2)==1 FALSE do ELSE y=y/2 2 x=x+y(2+2) 4 y=y+x(2+4) 6 |
Output:
What's up Doc?
Enter the first integer:
4
Enter the second integer:
5
The mystery number is: 6
4.For exercises 1 to 3, indicate the output that will be produced. Assume the following declarations:
final int MAX=25, LIMIT=100;
int num1 = 12, num2 = 25, num3 = 87;
|
Exercises |
Output |
|
Exercise 2 if (num2 > 18) if (num1 < 0)
System.out.println("apple"); else
System.out.println("orange"); System.out.println("pear"); |
25>18 TRUE do IF 12<0
FALSE do else print
orange print pear |
|
Exercise 2 if (LIMIT >= 4*num2) if
(MAX == 25) System.out.println("apple"); else System.out.println("orange"); else System.out.println("pear"); |
100>=100 TRUE do IF 25==25
TRUE do IF
print apple |
|
Exercise 3 if (num3 >= MAX) { if (MAX/num2 = = 1)
System.out.println("apple"); System.out.println("orange"); if (LIMIT-num3 > num1+2) System.out.println("pear"); else {
System.out.println("banana");
System.out.println("kiwi"); } } else if (num2*2 = = Max*2)
System.out.println("grapefruit"); else
System.out.println("lime"); System.out.println("coconut"); |
87>=25 TRUE do IF (25/25=1)==1 TRUE do IF print apple print orange (100-87=13)>14 FALSE do ELSE print banana print kiwi print coconut |
5.Write an application that asks the user to enter an integer (num) and
then prints "num is zero", "num is even" or "num is
odd" as appropriate based on the entered value of num.
For example, if the user entered in 7, then
"The number entered is odd" would be printed to the screen.
/*
Tutorial 4 - CSCI 1100/1202 Answers
This program reads in an int and tells if
it is zero, even or odd
*/
import
java.io.*;
public
class NumberValue
{
public static void main (String []args)
throws IOException
{
InputStreamReader reader = new
InputStreamReader (System.in);
BufferedReader input = new BufferedReader(reader);
String text = ""; //to read in
the line of text
int value; //capture value entered by
user
//read in information
System.out.print("Enter an integer:
");
text = input.readLine();
value = new Integer(text).intValue();
//check to see if value is zero, even or
odd (use the modulus operator)
if (value == 0)
System.out.println("The number
entered is zero.");
else
if (value%2==0) //the value is even
System.out.println("The number
entered is even");
else //the number is odd
System.out.println("The number
entered is odd");
}//end main
}//end
class