1. Go back to the loop questions from the previous week and implement them all using for-loops instead of while loops!
2. What will the output be:
int x = 2, y = 7;
if (y < 1 && x++ < 7)
System.out.println("hello");
System.out.println("x = " + x);
Output: x = 2
3. What will the output be:
int x = 7;
int y = x++ + 3;
int z = 7 * --x + 4;
System.out.println("x = " + x + ", y = " + y + ", z = " + z);
Output: x = 7, y = 10, z = 53
4. What will the output be:
int x = 5;
x += 7;
x -= 2;
x /= 2;
x *= 8;
x %= 11;
System.out.println(x);
Output: 7
5. Write a chart showing the precedence of all of the following operators: = , < , ==, &&, ||, !=, ++, + (addition), +=, --, *
++, --
*
+
<
==, !=
&&
||
=, +=
6. If two operators occur in the same expression, and they are on the same level in the precedence chart (it is a “tie”), how do you decide which operator gets evaluated first?
Go from left to right (unless they are assignment operators, in which case go from right to left.)
7. Using parentheses indicate the order in which each of the following expressions will be evaluated or state that the expression represents an invalid expression. You may assume that all variables are of type int.
a. x / y * z % w
((x / y) * z) % w
b. x ++ + y ++
(x++) + (y++)
c. x + y + z – w % p * 2
x + y + z – ((w % p) * 2)
d. x < y || z > m && y <= 4
(x < y) || ((z > m) && (y <= 4))
8. List as many reasons as you can for why it is so important to invest lots of time in the “design” of a large-scale software projectbefore the project is coded. (5 reasons were listed in class.)
Here are some of the things a good design can improve:
· Efficiency (speed)
· Efficiency (memory)
· Ease of coding
· Ease of debugging
· Ease of expansion
9. Write psuedocode for the following program. The program will read in strings that represent names of students. It will keep prompting the user for more and more names until the user enters “STOP”. After that, the program will display the number of names entered, the name that is first alphabetically, and the name that is last alphabetically.
Let count = 0.
Do
Prompt “Enter a name: “
Input name from user
If count is still 0,
Let first = name
Let last = name
Otherwise if name != “STOP”
If name precedes first alphabetically
Let first = name
If last precedes name alphabetically
Let last = name
Increment count by one
While name is not “STOP”, keep looping
Output “Number of names entered = “ count
Output “First name alphabetically is “ first
Output “Last name alphabetically is” last
10. Write psuedocode for a program that computes the number of digits in an integer. For example, if the user enters 1792, the output will be “4”.
/* There is an easier way with logarithms… */
Prompt “enter an integer”
Input number from user.
Let compareValue = 10
Let counter = 1
While number >= compareValue
Increment counter
Let compareValue = compareValue * 10
Output: “Number of digits is” counter
11. Write psuedocode for a program that reads a sequence of integer values and decides whether or not it is a decreasing sequence. The program will first read in the number of values to process, followed by the values themselves. The output will be “Yes” if the sequence is decreasing, and “No” otherwise.
Prompt “How many values”
Input numValues from user
Let decreasingSequenceFlag = true
Prompt “Enter a value: “
Input recentValue
Let counter = 1
While counter <= numValues
Prompt “Enter a value: “
Input newValue
if newValue >= recentValue
decreasingSequenceFlag = false
recentValue = newValue
increment counter
if decreasingSequenceFlag = true
output “YES”
otherwise
output “NO”
12. Which of the following code fragments are OK, and which will cause problems?
a. int x = 52;
double y = x;
OK
b. double x = 14;
int y = x;
NOT OK
c. int x = 7;
long y = x;
OK
d. long x = 17L;
short y = x;
NOT OK
13. Show how to use “explicit casting” to force the troublesome examples in the previous question to work.
For part b:
int y = (int) x;
For part d:
short y = (short)x;