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 are the differences between constructors and methods?
  2. 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;
      }
  3. 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();
  4. 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.
  5. 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.)

  6. What types of array can be sorted using the Java.util.Arrays.sort method?  Does this sort method create a new array?
  7. 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}, };
  8. 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;
        }
    }
  9. 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() );
       }
    }
  10. If a class contains only private data fields and no “set” methods, is the class considered to be immutable?
  11. If a class contains only data fields that are both private and primitive, and no “set” methods, is the class considered to be immutable?
  12. 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;
        }
    }
  13. 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).