Introduction to Java Programming

Assignment 4

Note that in the following multiple choice questions there may be more than one answer.

  1. Which of the following statements are true?
    1. A class is an instance of an object
    2. A method is a class of object
    3. An object is an instance of a class
    4. A method is an instance of a class

  2. Which of the following statements are true?
    1. Any variable declared within a class is called a local variable.
    2. Any variable used within a method is called a local variable.
    3. Any variable declared within a block is called a local variable.
    4. Any variable used within a object is called a local variable.

  3. Which of the following statements are true?
    1. Any variable particular to an instance of a class is an instance variable.
    2. Instance variables belonging to different instances are independent.
    3. Instance variables belonging to different instances are only identical if they have the same name.
    4. Any variable used within a object is called a instance variable.

  4. Which of the following statements are true?
    1. Every class must have a constructor.
    2. Failure to declare a constructor for a class results in a compiler error
    3. Every constructor that is declared must have at least one formal parameter.
    4. The purpose of a constructor is to create an instance of a class.

    [5 MARKS Q1-Q4]


  5. Consider the following program and answer the questions underneath
    public class IntObj
     {  
       void printval()  
       { System.out.println("My value is " + 1 );  }
    
     }
    
    public class Test
     { public static void main( String[] args )
      { IntObj iObj  = new IntObj();
        iObj.printval(); }
     }
    
    1. How many classes are defined?
    2. How many methods are defined?
    3. How many constructors are defined?
    4. What is the output?
    5. Give a step-by-step explanation of what happens when the program is run.
    6. What is the difference between a method invocation and a method definition? Give examples from the above program
    7. Explain the function of each word in the the following code
      IntObj iObj = new IntObj();

    [5 MARKS]


  6. Modify the definition of IntObj so that different instances can be initialised with different values and will print different messages. Test your program with the following:

    public class Test
     { public static void main( String[] args )
         { IntObj iObj1 = new IntObj(2),
                  iObj2 = new IntObj(1);
           iObj1.printval();
           iObj2.printval() ; }
     }
    
    Hint: In the class IntObj you need to declare an instance variable and you also need to define a constructor which will assign the instance variable

    [5 MARKS]


  7. Implement methods inc and decto increment and decrement the value of an IntObj by 1. Test with the following:
    public class Test2
     { public static void main( String[] args )
         { IntObj iObj1 = new IntObj(2);
           iObj1.printval();
           iObj1.inc();
           iObj1.printval() ; 
           iObj1.dec();
           iObj1.printval()}
     }
    

    [5 MARKS]


    1. Implement a non-void method val which returns the current value of an instance of IntObj
    2. Implement methods add, sub, and mult which carry out addition, subtraction and multiplication between instances of IntObjs.
    3. Show how all of these these are tested in a program with the following general orgnisation
    4. public class Test3
       { public static void main( String[] args )
           { IntObj iObj1 = new IntObj(2), //first integer
                    iObj2 = new IntObj(3); //second integer
                 
             int i = iObj1.mult(iObj1.val(), iObj2.val()); //code which performs multiplication
             .
             .
             //code to print the value]
             //other tests for addition and subtraction.
        }
      
  8. [10 MARKS]


    1. Using Input/Output redirection (see Kjell Ch 21), write a class Echo which reads the text file news.txt and prints the output to the terminal. Assume that the text file is organised in lines and terminated by a line containing nothing but the string  %EOF%.
    2. Display the number of lines in the file.
    3. Display the number of words in the file.
    4. Display the average word length.

        [10 MARKS]

 

Submission

Format: 1 email containing answers to questions and java programs as file attachments with the correct name ready for compilation

Deadline: Friday 9th February.

Late submissions will be penalised as follows: