CSC 211 -- Practice Final A

Work as many of these problems as you can.

  1. Draw a variable trace table and predict the output of this code. The trace table should have the columns s.a, s.b, t.a, t.b, u.a, u.b.

    public class A
    {
        private int a;
        private String b;

        public A(int x)
        {
           a = x + 2;
           b = "xyz";
        }

        public A(String y)
        {
           a = 100;
           b = y + "3";
        }

        public A(int x, String y)
        {
           a = x + 4;
           b = x + y + "5";
        }

        public void update()
        {
           a += 6;
        }

        public void update(int z)
        {
           b += String.valueOf(z);
        }

        public String toString()
        {
           return a + " " + b;
        }
    }

     

     

    public class Main
    {
        public static void main(String[] args)
        {
           A s = new A(9);
           A t = new A("apple");
           A u = new A(7, "peach");
           s.update();
           t.update(8);
           System.out.println(s);
           System.out.println(t);
           System.out.println(u);
       }
    }

  2. Write short main methods.
    1. Explain the differences between these pairs of reserved words:
      (public, private), (Swing, AWT), (FileReader, BufferedReader), (extends, implements).
    2. Write a main method that reads two ints from the text file c:\numbers.txt, computes their sum, and prints the result.
    3. Write a main method that reads two ints as command line arguments, computes their sum, and prints the result in the terminal window.

  3. Designing, writing and testing an AirlineFlight class.
    1. Design the instance variables and methods of an AirlineFlight class.
    2. Write the Java code for some of the instance variables and methods of the AirlineFlight class that you designed in Part a.
    3. Write a main method that tests the AirlineFlight class in Part b.
    4. Write a main method that prints all the two hop flights from Chicago to San Diego.

  4. Consider a Die class with a roll method that randomly sets the face instance variable to 1, 2, 3, 4, 5, or 6. The Die class also has a getFace accessor method, but no toString method. Write a main program that creates a Die object and stores 100 outcomes of a Die roll in an int array defined like this:
    int[] total = new int[7];
    (Only use the array elements with indices 1, 2, 3, 4, 5, 6.) Then go through the array and print how many times each outcome occurs.

  5. Write a user interface with a button and a textfield. When the button is clicked, change the text in the textfield to uppercase.

  6. Write a user interface with five textfields and a button. When the button is clicked, compute the average length of the words in the first five textfields and display the result in the sixth textfield.