Disclaimer: The purpose of these exercises is two give you some practice material.
By no means assume this is the only material you need to know for the exam. Make sure
you check the code snippets posted on the web page. They can serve as additional
study material.
1. Write a Java Program that prints the even numbers from 1 up to specified limit. There
are two versions for this program: one that uses a while loop and another which uses
a do-while.
2. Modify the Cashier program discussed in class so that now we can recognize different
kinds of milk. After the user chooses milk, the program must inquire about the
particular type. You can have 1%, 2% milk, etc. Assign different prices to each
kind of milk.
3. Write a program that determines the letter grade of a student based on a numeric grade.
The program will define five letter grades (A, B, C, D, F) and four cutoffs: Acutoff,
Bcutoff, Ccutoff and Dcutoff. In your program you will use double type variables in order
to represent these cutoff values.
4. Write a program that reads the location of two images, doubles the size of each
image, and displays the images on the screen. You must use the picture infrastructure
in order to complete this exercise.
5. Suppose there are 100 images named file1.jpg, file2.jpg, file3.jpg, ...,
file100.jpg. Write a complete Java program (a class with main) that will display
a subset of those images. The program will read two values representing the first
and last image to display. For example, if the user inputs "1" and "3",
the images associated with the file1.jpg, file2.jpg and file3.jpg will be displayed.
6. Write a program that reads the location of an image and then doubles, flips and
posterizes the image. The program will display the resulting image.
7. General questions
a. Which of the following are valid primitive types in Java?
int
boolean
scientific
b. Which of the following are valid Java identifiers?
speed temperature 1house
c. What extension must a file containing Java code have?
8. Initialize the variables f, g, k to different values and then evaluate, for each set
of values, the conditional statements in the following piece of code. The variables
f, g, and k are double type variables.
// Initialize f, g, k to different values
if ((f <= g) || (f == 0.0)) {
if (!(g > k) || false) {
System.out.println("Case 1");
} else {
System.out.println("Case 2");
}
} else {
System.out.println("Case 3");
}
9. Initialize the integer variable b with different values and write down
the output
generated by the following code segment.
int i = 1;
while (i <= b) {
if (i == 2) {
i = i + 1;
}
System.out.println(i);
i = i + 1;
}
System.out.println(i);
10. Draw a memory diagram that will result after executing the following code segments.
a.
int x=0, y=10;x = 20;
y = x + 10;b.
String m1 = new String("Red");
String m2 = new String("Blue");
String m3 = null;
m3 = m1;
m2 = m3;
m1 = null;c.
String m1 = "Peter";
String m2 = "Rose";
String m3, m4, m5, m6;m3 = m4 = m5 = m6 = m1;
11. Recommended exercises from the textbook (Lewis & Loftus):
a. Chapter 1, Programming projects section (Starting on page 56): 1.2, 1.3, 1.4
b. Chapter 2, Programming projects section (Starting on page 123): 2.5, 2.6, 2.7,2.11
c. Chapter 3, Exercises, (Starting on page 198): 3.3, 3.4, 3.5, 3.6, 3.8, 3.9, 3.12, 3.14,
3.15, 3.16, 3.20d. Chapter 3, Programming projects (Starting on page 201): 3.2, 3.4