CSC 211 -- Practice Final B

Work only 5 out of 6 problems.

  1. Answer all three parts a, b, and c.

    1. Explain the difference between an instance variable and a local variable.

    2. What is wrong with these statements?
      int[] a;
      for(i = 0; i <= a.length - 1; i++)
          a[i] = i;

    3. Write a main program that accepts an input string in a command line argument. Print out the characters of this string, one character per line.

  2. What is the output of the following Java program? Show your work.

    public static void main(String[] args)
    {
        String[] a = {"watermelon", "date",
              "lemon", "orange", "grapefruit", };
        String temp;
        int i;
        for(i = 1; i <= 4; i++)
        {
           temp = a[i];
           a[i] = a[i - 1];
           a[i - 1] = temp;
        }
        for(i = 0; i <= 4; i++)
           System.out.print(a[i].charAt(i));
    }

  3. What is the output of the following Java program. Hint: draw an object diagram to keep track of the data values in the objects.

    public class A
    {
        int a;
        String b;

        public A(int intVal, String strVal)
        {
           a = intVal + 5;
           b = "ss" + strVal;
        }

        public A(String strVal, int intVal)
        {
           a = intVal + 15;
           b = "tt" + strVal;
        }

        public void update()
        {
           a += 5;
           b += "uu";
        }

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

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

    }

    public class Main
    {
        public static void main(String[] args)
        {
           int i;
           A[] x = new A[3];
           x[0] = new A(5, "fox");
           x[1] = new A("goose", 7);
           x[2] = x[0];
           x[0].update();
           x[1].update(2);
           x[2].update();
           for(i = 0; i <= 2; i++)
              System.out.println(x[i]);
        }
    }

  4. Do both parts a and b.

    1. Write code for a GroceryItem class. The private instance variables are name (String), barCode (int), and price (double). Write code for a parameterized constructor that initalizes the instance variables, a toString method, a getter method getPrice, and a setter method setPrice.

    2. Write a main method that creates a GroceryItem object using data read from the text file c:\GroceryInfo.txt. You may assume one item of data per line, for example:
      2% Milk
      254365786
      2.65

      Then test the toString, getPrice, and setPrice methods.

  5. Write a Java application that creates an array of 100 double elements. Use a for loop to fill the array with 100 random values between 0 and 1. Then use another for loop to find the largest array value and print the result.

  6. Write a Java application that creates a user interface that contains a textbox and a button. When the button is clicked, the application should replace the string by "Yes" is the first or second character of the string in the textfield is the letter a. Otherwise replace the string in the textfield by "No".