Homework Assignment 1

  1. Why is the finalize method in Java a poor substitute for a destructor method?

  2. What is a virtual machine? Why are programs executed by virtual machines slower than programs executed by the machine itself?

  3. Write the Java statement that declares MARCH to be an integer constant with a value of 3.

  4. Why is it not possible in Java to write a swap routine that swaps two variables of the same primitive type?

  5. Write a java program whose main class is named Search.java that reads items from the console and then reads strings and tries to locate the corresponding item. The program specifications are as follows:

    1. Your program will read items from the console. Each item will consist of a name, which is a string, and an age, which is an integer.

    2. The items will be terminated by a "//" string. As soon as this string is encountered, print the number of people and their average age, using one fractional digit.

    3. Following the "//" string will be an arbitrary number of strings, which will be terminated by the string "done". Use each string as a key to try to locate an item with that name. If such an item is found, then print the string and the person's age. Otherwise print the string and the message "Not found."

    4. Use a hash table from the java.util package to store the items.

    Java 1.5 introduced two new classes, Formatter and Scanner that simplify the printing of formatted output and the reading of input from the console. You can find more information about these two classes on pp. 519-541 of the Schildt text. Even better, you really do not have to know how to use a Formatter class for this problem because Java 1.5 supports C's printf function. You can now write code like:

        String name = "Brad";
        float golfHandicap = 18.235;
        System.out.printf("name = %s handicap = %5.1f\n", name, golfHandicap);
    and get output that looks like:
        name = Brad handicap =  18.2
    The Scanner class does not have a particularly good way of detecting EOF, which is why the string "done" is used to notify your program that the input is about to be terminated. In order to use the Scanner class, make sure you place the following statement at the very top of your .java file:
        import java.util.Scanner;
  6. You are given the following declarations in Java (you should assume that each class would be placed in a different file):
    package LinkedList;
    class ListNode {
        protected int value;
        String name;
    package LinkedList;
    public class List {
        ListNode header;
        protected ListNode sentinelNode;
        public List() {
            header = new ListNode();
    1)      header.value = 10;
    2)      header.name = "brad";
    	sentinelNode = new ListNode();
    package LinkedQueue;
    class Queue extends LinkedList.List {
        public Queue() {
    3)      header.value = 20;
    4)	sentinelNode.value = 30;
    Answer the following yes/no questions about the above code and for each answer explain why you answered as you did:

    1. Is it legal to access the value variable in statement 1?
    2. Is it legal to access the name variable in statement 2?
    3. Is it legal to access the header variable in statement 3?
    4. Is it legal to access the sentinelNode variable in statement 4?
    5. Is it legal to access the value variable in statements 3&4? Hint the answer and reason is the same in both cases.

  7. Answer this question only after writing the program in the next question.

    1. In the next problem, could the TreeNode class be written as an interface? Why or why not?
    2. In the next problem, could the OperatorNode class be written as an interface? Why or why not?

  8. Using Java write an expression evaluator that takes an expression in prefix notation, evaluates it, and prints an answer. Your program will continue to read expressions and evaluate them until the user hits Ctrl-D. Here is a sample session:

    >>> + 3 6
    >>> / 2 + -3 6
    >>> + + / 24 * 2 4 - 3 5 6
    >>> Ctrl-D

    Your program should read in one expression at a time, build an expression tree for each expression, and then evaluate the expression tree and print out the answer.

    For example, given the expression:

    / 2 + -3 6
    your program should construct the expression tree:
                     /     \
    		 2      +
    		      /   \
    		     -3    6
    Each of your nodes should be an object that is an instance of some class. A good inheritance hierarchy for this program would be:
    		|               |
    	   OperatorNode    OperandNode
            |         |          |           |
         PlusNode MinusNode MultiplyNode DivideNode
    The actual expression tree from above would then look like:
    		|                    |
    	    OperandNode           PlusNode
    			     |                |
    			OperandNode      OperandNode
    Part of this assignment involves figuring out what methods to define and where to place them, and figuring out which classes should be abstract and which should be concrete.

    Place all of your classes in a package named formula. The class containing the main method should be named ExpressionTree.

    Your program may assume that the input is always correct and has the correct number of operands.


    1. If you have a question about what an expression should evaluate to, type the following:
      java -jar /home/bvz/cs365/bin/ExpressionTree.jar

    2. Use a Scanner to read lines using the Scanner's nextLine method. The nextLine method returns a string that can be passed to the constructor for another Scanner object. You can use the second Scanner object to read the numbers and operator symbols. The nextLine method will return the exception Java.util.NoSuchElementException on EOF (when the user presses Ctrl-D in Unix). You should consult the documentation for the Scanner class and consult the sample scanner program if you have questions about how the Scanner class operates.

    3. Use System.out.printf to print out your formatted results