COP 2800 (Java Programming I) Homework Assignment #4

 

Due: by the start of class on the date shown on the syllabus

Description:

Answer the following questions as briefly (but completely) as possible:

  1. What is a checked exception, and what is an unchecked exception?
  2. What is NullPointerException?
  3. Which of the following statements (if any) will throw an exception?  If no exception is thrown, what is the output?
    System.out.println( 1 / 0 );
    System.out.println( 1.0 / 0 );
  4. Point out the problem in the following code.  Does the code throw any exceptions?
    long value = Long.MAX_VALUE + 1;
    System.out.println( value );
  5. What are the differences between constructors and methods?
  6. What is wrong with each of the following programs?
    1. public class ShowErrors {
         public static void main ( String [] args ) {
            ShowErrors t = new ShowErrors( 5 );
         }
      }
    2. public class ShowErrors {
         public static void main ( String [] args ) {
            ShowErrors t = new ShowErrors();
            t.x();
         }
      }
    3. public class ShowErrors {
         public void method1 () {
            Circle c;
            System.out.println( "What is radius "
               + c.getRadius() );
            c = new Circle();
         }
      }
    4. public class ShowErrors {
         public static void main(String[] args) {
            C c = new C(5.0);
            System.out.println(c.value);
         }
      }
      
      class C {
         int value = 2;
      }
  7. Which of the following statements are valid?
    1. int i = new int(30);
    2. double d[] = new double[30];
    3. char[] r = new char(1..30);
    4. int i[] = (3, 4, 3, 2);
    5. float f[] = {2.3, 4.5, 6.6};
    6. char[] c = new char();
  8. Given an array of doubles, write Java statements to do the following:
    1. Assign the value 5.5 to the last element in the array.
    2. Display the sum of the first two elements of the array.
    3. Write a loop that computes the sum of all elements in the array.
    4. Write a loop that finds the minimum element in the array.
    5. Randomly generate an index and display the element of this index in the array.
    6. Use an array initializer to create another array with the initial value 3.5, 5.5, 4.52, and 5.6.
  9. Use the following illustration as an example, show how to apply the binary search approach to a search first for key 10 and then key 12, in the list:
    [2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79].
    key is 11
             0   1   2   3   4   5   6   7   8   9  10  11  12
    11<50  [ 2,  4,  7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79]
           low=0                   mid=6                   hi=12
    
    11>7   [ 2,  4,  7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79]
           low=0    mid=2      hi=5
    
    11=11  [ 2,  4,  7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79]
                       low=3    hi=5
                           mid=4

    (Note how binary search eliminates half of the list from further consideration after each comparison.)

  10. What types of array can be sorted using the Java.util.Arrays.sort method?  Does this sort method create a new array?
  11. Which of the following statements are valid?
    1. int[][] r = new int[2];
    2. int[] x = new int[];
    3. int[][] y = new int [3][];
    4. int[][] z = {{1, 2}};
    5. int[][] m = {{1, 2}, {2, 3}};
    6. int[][] n = {{1, 2}, {2, 3}, };
  12. How do you do the following tasks?
    1. Create an ArrayList for storing double values?
    2. Append an object to a List?
    3. Insert an object at the beginning of a List?
    4. Find the number of objects in a List?
    5. Remove a given object from a List?
    6. Remove the last object from a List?
    7. Check whether a given object is in a List?
    8. Retrieve an object at a specified index from a List?
  13. Identify the errors in the following code fragment:
    ArrayList<String> list = new ArrayList<String>();
    list.add( "Denver" );
    list.add( "Austin" );
    list.add( new java.util.Date() );
    String city = list.get( 0 );
    list.set( 2, "Dallas" );
    System.out.println( list.get(2) );
  14. Explain why the following code fragment displays [1, 3] rather than [2, 3].
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(2);
    list.add(3);
    list.remove(1);
    System.out.println( list );
  15. Describe the difference between passing a parameter of a primitive type and passing a parameter of a reference type.  Then show the output of the following program:
    class Test {
        public static void main ( String [] args ) {
            Count myCount = new Count();
            int times = 0;
            for ( int i = 0; i < 100; i++ )
                increment( myCount, times );
            System.out.println( "count is " + myCount.count );
            System.out.println( "times is " + times );
        }
        public static void increment ( Count c, int times ) {
            c.count++;
            times++;
        }
    }
    
    class Count {
        public int count;
        public Count ( int c ) {
            count = c;
        }
        public Count () {
            count = 1;
        }
    }
  16. What is wrong in the following code?
    public class Test {
       public static void main ( String [] args ) {
          java.util.Date[] dates = new java.util.Date[10];
          System.out.println( dates[0] );
          System.out.println( dates[0].toString() );
       }
    }
  17. If a class contains only private data fields and no “set” methods, is the class considered to be immutable?
  18. If a class contains only data fields that are both private and primitive, and no “set” methods, is the class considered to be immutable?
  19. What is wrong in the following code?
    public class C {
        private int p;
    
        public C () {
            System.out.println( "C's no-arg constructor invoked" );
            this(0);
        }
    
        public C ( int p ) {
            p = p;
        }
    
        public void setP ( int p ) {
            p = p;
        }
    }
  20. What is wrong in the following code?
    public class Test {
        private int id;
        public void m1 () {
            this.id = 45;
        }
        public void m2 () {
            Test.id = 45;
        }
    }

To be turned in:

Email your homework assignment, by copy-and-paste (no attachments please), to (homework submission).  If possible use the “text” and not the “HTML” mode of your email program.  Please use the subject similar to “Java Programming I Homework Assignment #4 Submission”, so I can tell which emails are submitted homework assignments.

Homework assignments will not be returned.  Please do not send as attachments.  Refer to the Homework and the Submitting Assignments sections of your syllabus for more information.

Confused?  Send questions about the homework assignment to (homework questions).  Please use a subject similar to “Java Programming I Homework Assignment #4 Questions” so I can tell which emails are questions about assignment quiz (and not submissions).