Review Sheet for Midterm 1
This review sheet is not complete. All material covered in class or in
reading assignments through the end of class on Monday, Oct 1, will be
covered on the test.
1. Write a line of code that creates an object that can be used to write text to the file "numbers.dat".
PrintStream outStr = new PrintStream(new File("numbers.dat"));
2. What is a checked exception? How do we advertise that our code might throw an exception (if we don't intend to handle it)?
A checked exception is one that we must handle in some way, or
advertise that our code might throw, in order for our program to
compile. We advertise that a method might throw an exception by adding
"throws Exception" to the header of the method.
3. Write a Java program that reads 100 integers and prints the min, max and average to the screen.
public class Numbers
public static void main(String args)
// set up for input
Scanner stdin = new Scanner(System.in);
System.out.print("Please enter 100 ints: ");
int min = stdin.nextInt(); // set min to first num read
int max = min;
int sum = min;
// read the remaining 99 values
for(int i = 0; i < 99; i++)
int num = stdin.nextInt();
if(num < min) min = num;
if(num > max) max = num;
sum += num;
System.out.println("Max: " + max);
System.out.println("Min: " + min);
System.out.println("Avg: " + sum/100.0);
4. Write a program that takes a sentence from the command line (ie, 0
or more command line arguments) and prints the reversed sentence to the
This is NOT a complete program.
public static void main(String args)
for(int i = args.length-1; i >=0; i--)
System.out.print(args[i] + " ");
5. Write a program that reads an input file "test.txt" and prints each floating-point token to the screen.
We did a similar example in class.
6. Write a class that represents a Point in 2-dimensional space. The
class should store the point's x and y coordinates. Include methods
- compute and return the point's distance from the origin (0,0)
- getter methods
- setter methods
- override the toString(), clone() and equals() methods from the Object class
- constructor that takes the point's x and y coordinates
- constructor that creates a point that represents the origin
8. Write a Java program that reads the name of a file from the user,
and prints the number of lines in the file that contain the word
public class Happy
public static void main(String args) throws IOException
Scanner scan = new Scanner(System.in);
System.out.print("What is the file name? ");
String file = scan.next();
// create scanner associated with file
Scanner fileRead = new Scanner(new File(file));
int num = 0; // number of lines containing "happy"
String line = fileRead.nextLine();
if(line.indexOf("happy") >= 0) num++;
System.out.println(num + " lines contain the word happy");
9. Declare and initialize a constant the represents the median price of a house in Austin.
public final int HOUSE_PRICE = 200000;
10. Give an example of an explicit cast, and explain when it is necessary to use an explicit cast.
11. Explain the difference between comparing 2 strings with == and the
equals() method. Does it make sense to compare two floating-point
values with ==?
12. For your class Point in #6, write a subclass called XAxisPoint that represents points with y-coordinate equal to 0.
13. What is the value and type of the result?
d. Math.pow(3, 5)
g. 15 + 2 + " hellos"
h. 15 * 2 / 4
i. (5 < 4) || Character.isDigit(2)
14. Write Java code that prints the characters in an array
String words = ...; // assume words has been initialized
to the screen, one character per line.
15. Review the online notes we've covered in class and re-work the examples.
16. Give an example illustrating the use of the canRead() method in the File class.