Do the following exercises using only the material we have studied
in Module 1. In particular, you may not make use of the if statement
or the ternary operator.
Some of the questions are a bit tricky. The idea is to stretch some on these
problems, so that you will find the quiz and exam questions easier to tackle.
The quiz and associated exam problems will resemble the questions you
see here, so work to master these and you should have no problem on the
quiz or exam.
Where there are multiple exercises within a problem, choose one or two and
leave the rest for homeowork on your own.
- In studio you wrote a program that determined if 3 integers were ordered. Write
a program (remember, without using if) that prints true if four
integers (n1, n2, n3, n4) are
ordered from lowest to highest. You may assume there are no duplicate values.
- Suppose that elapsed is an integer representing the number of seconds
that have passed since some important event took place in the past 24 hours.
Write a program that
takes the value of elapsed and expresses the results in hours and
seconds, using a format of your own choosing.
- Suppose that an int variable named cost maintains the
cost of an item in pennies. Using no data type other than
int or String, write code that prints out the value of cost in
the normal dollars and cents notation.
For example, if cost has
the value 12345, then your program should print
- We know that in an expression without parentheses, multiplication is performed
before addition. Write a Java program that proves this is the case, so that
from running your program, you know that multiplication does take precedence over
- It turns out that when you use the logical-or operator ||,
the second expression is not evaluated if the first one is true.
Thus, in the
expression (thing1 || thing2), if thing1 is true, then
the value of thing2 is not even considered.
- Consider why Java acts this way. Why is it OK for Java to ignore the
second expression if the first one is true?
- Can you think of how Java would similarly treat the logical-and
- Tricky! Returning to the logical-or operator,
write a program that proves Java ignores the second expression if the
first one is true.
Hint: An assignment of the form (a=b) is itself an expression with the value
b. The expression assigns the value of b to a, and then
evaluates to the value of b.
- An expression tree is shown below.
- Write the Java expression for the tree, using parentheses only
- When the expression is evaluated, what is the resulting value?
- What is the type of the resulting value?
- For each of the following expressions
- Draw the expression tree
- State the result computed by the expression
- State the type of the resulting expression (int or double)
- 3 * 4 + 7
- 3 + 4 * 7
- 3.0 + 4 / 7
- 1 + 2 + 3 + 1 * 2 * 3
- (50 - 5 * 4) / 10 - 7
- Determine the result of the following String expressions:
- "this " + "string " + "has " + "spaces "
- "this" + "string" + "has" + "no" + "spaces"
- "the sum of " + 6 + " + " + 10 + " is " + (6 + 10)
- "the sum of " + "6 + 10" + " is " + (6 + 10)
- 6 + 10 + " equals " + (6 + 10)
- "" + 6 + "+" + 10 + " equals " + 6 + 10
- Determine the result of the following boolean expressions:
- 3 > 7
- (3 > 7) || (4 < 9)
- 135 == 100 + 35
- (true && false) || (true || false)
- (3 > 7) || ((10 < 9) == (3 == 8))
- false || !(3 > 7)
- For each expression below, determine the type of the resulting
value. If the expression would cause an error, explain why. Otherwise
determine the value of the expression.
- 7 / 2
- 7.0 / 2
- ((7 / 2) == (7.0 / 2))
- "x = " + 6
- 6 + " = x"
- ((6 * 3) / 80) / (35 - 7 * 5)
- 1 / 2
- For each row in the table below, place an X in the
int or double
is the more appropriate type to represent the specified value.
|Value ||int? ||double?|
| Area of a circle || || |
| Number of robots you own || || |
| Your graduation year || || |
| Your height in meters || || |
| Your weight in micrograms || || |
| Number of facebook friends || || |
| Average age of your facebook friends || || |
- 1.2.25 on page 42 of the text
- 1.2.30 on page 43 of the text