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 NullPointerException?
  3. 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;
      }
  4. Suppose that the class F is defined as shown below.  Let f be an instance of F.  Which of the following statements are correct?
    public class F {
      int i;
      static String s;
      void iMethod () {
      }
      static void sMethod () {
      }
    }
    1. System.out.println(f.i);
    2. System.out.println(f.s);
    3. f.iMethod();
    4. f.sMethod();
    5. System.out.println(F.i);
    6. System.out.println(F.s);
    7. F.iMethod();
    8. F.sMethod();
  5. Add the static keyword in the place of ?, if appropriate, in the code below.
    public class Test {
        private int count;
        public ? void main ( String [] args ) {
            ...
        }
        public ? int getCount () {
            return count;
    }
    public ? int factorial ( int n ) {
        int result = 1;
        for ( int i = 1; i <= n; i++ )
            result *= i;
        return result;
    }
    }
  6. 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;
        }
    }
  7. 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() );
       }
    }
  8. If a class contains only private data fields and no “set” methods, is the class considered to be immutable?
  9. If a class contains only data fields that are both private and primitive, and no “set” methods, is the class considered to be immutable?
  10. 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;
        }
    }
  11. 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;
        }
    }
  12. Can each of the following statements be compiled?
    1. Integer i = new Integer("23");
    2. Integer i = new Integer(23);
    3. Integer i = Integer.valueOf("23");
    4. Integer i = Integer.parseInt("23", 8);
    5. Double d = new Double();
    6. Double d = Double.valueOf("23.45");
    7. int i = (Integer.valueOf("23")).intValue();
    8. double d = (Double.valueOf("23.4")).doubleValue();
    9. int i = (Double.valueOf("23.4")).intValue();
    10. String s = (Double.valueOf("23.4")).toString();
    1. How do you convert an integer into a string?
    2. How do you convert a numeric string into an integer?
    3. How do you convert a double number into a string?
    4. How do you convert a numeric string into a double value?

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).