Common Tasks

  1. Suppose x and y are of type int, and are already assigned values. Write a code snippet (in this case, two to three java statements) that will swap the values associated with x and y. So, for example, if x = 2 and y = 3 before the code snippet executes, then x = 3 and y = 2 will be true after it executes.

    Hint: Suppose you had some milk in a red glass and some juice in a green glass sitting in front of you. You wish to swap the contents of the glasses. That is to say, you wish the milk to be in the green glass, and the juice to be in the red glass. How can you accomplish this?   Swapping the values associated with two variables works in the same way -- with the variables playing the role of the glasses, and their values playing the role of the contents of those glasses (i.e., the milk and juice).

  2. Suppose x, y, and z are of type int. Using an appropriate function from the Math class, write a single Java statement which defines a variable m of type int and assigns it a value equal to the minimum of x, y, and z.

  3. Write two Java statements that, when executed together, use an appropriate class of the java.util package to generate a random integer between $0$ to $100$, inclusive.