15-110 Sections G-J / Spring 2010 / Quiz 1
3 New Parts (4 Total Parts) / 30 Minutes

·         All code examples compile and run without errors (except where explicitly noted otherwise).

·         When writing code, write just a few lines of code rather than entire methods.

·         You should assume that “page” and “scanner” are declared and initialized as expected.

·         You may abbreviate System.out.println as “sop”.

1)      Quiz1 Part1 was taken in recitation last week.

2)      Short Answers / Tracing / Mystery Code

a)      For y>0, if (x%y) > x, then we know that x is ____________.

b)      The largest int value is about ________________, and _______________ occurs when operations would produce values larger than this amount.

c)      Assuming x and y are (possibly-negative) int values, write one line that prints true if the ratio x/y is positive and false otherwise.  Your code may not crash if y is zero.

d)      Assuming x is a (possibly-negative) int value, state, in 5 words or less, what this line of code does in general.
    System.out.println(x/5*5 == x);

e)      Indicate what the following will print:
    int x = 60, y = 63, z = 123456;
    System.out.println((x/y) + "," + (y/x));
    System.out.println((x%y) + "," + (y%x));

f)       Draw a picture of what the following will paint:
    int a=50, b=100, c=150, d=200, e=25;

3)      Writing Code (Basic Mechanics)

a)      Write a few lines of code that declare two int variables, read their values from the user, and then print out the larger of the two values entered.

b)      Write a few lines of code that center a blue circle of radius 20 in the top right quadrant of the window.

4)      Writing Code (Problem Solving)
Think of a sheet of graph paper as representing the streets and avenues in a city map.  A given point (x,y), where x and y are both non-negative integers, represents an intersection – for example, (3,5) is where 3rd Street meets 5th Avenue.  The “Manhattan Distance” is the distance between two such points (that is, two intersections) – it is the distance one must walk on the streets (not cutting through the buildings or parks) to get from one intersection to the other.  It is never negative, regardless of the direction walked.

a)      Write a few lines of code that read in four non-negative int values representing two intersections and print out the Manhattan Distance between those intersections.  For example, if you read in the numbers 3, 5, 2, 7, then print the Manhattan Distance from (3,5) to (2,7), which is 3 (one block from 3rd to 2nd Street plus two blocks from 5th to 7th Avenue).

b)      Say that hospitals are placed at every intersection (x,y) where both x and y are multiples of 5, including a hospital at (0,0).  Write a few lines of code that read in two non-negative int values representing an intersection and print the location of the nearest hospital (by Manhattan Distance).  For example, if you read in the numbers 8,16, then print the nearest hospital to (8,16), which is at (10,15).

5)      Bonus/Optional

a)      Assuming x is an int, for what values in general is this expression true?
((x>=0) && ((x%3) == (x%2)) && (x/6*6 != x))

b)      && (and) is a boolean function of two variables.  So is || (or).  How many unique boolean functions of two variables exist?  Explain.

c)      In some languages, there is no first-class boolean type.  Instead, booleans are represented using int values rather than true and false.  In one such scheme, 0 is false and 1 is true.  Using this scheme, and assuming x and y are boolean values stored as int values 0 or 1, write a line of code that sets z equal to the int value representing the boolean (x || y).