Introduction to Java Programming
Assignment 4
Note that in the following multiple choice questions there may be more than one answer.
- Which of the following statements are true?
- A class is an instance of an object
- A method is a class of object
- An object is an instance of a class
- A method is an instance of a class
- Which of the following statements are true?
- Any variable declared within a class is called a local variable.
- Any variable used within a method is called a local variable.
- Any variable declared within a block is called a local variable.
- Any variable used within a object is called a local variable.
- Which of the following statements are true?
- Any variable particular to an instance of a class is an instance variable.
- Instance variables belonging to different instances are independent.
- Instance variables belonging to different instances are only identical if they have the same name.
- Any variable used within a object is called a instance variable.
- Which of the following statements are true?
- Every class must have a constructor.
- Failure to declare a constructor for a class results in a compiler error
- Every constructor that is declared must have at least one formal parameter.
- The purpose of a constructor is to create an instance of a class.
[5 MARKS Q1-Q4]
- 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(); }
}
- How many classes are defined?
- How many methods are defined?
- How many constructors are defined?
- What is the output?
- Give a step-by-step explanation of what happens when the program is run.
- What is the difference between a method invocation and a method definition? Give examples from the above program
- Explain the function of each word in the the following code
IntObj iObj = new IntObj();
[5 MARKS]
- 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]
- 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]
-
- Implement a non-void method val which returns the current value of an instance of IntObj
- Implement methods add, sub, and mult which carry out addition, subtraction and multiplication between instances of IntObjs.
- Show how all of these these are tested in a program with the following general orgnisation
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.
}
[10 MARKS]
-
- 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%.
- Display the number of lines in the file.
- Display the number of words in the file.
- 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:
- 1 day late: 10% deduction
- 2 days late: 30% deduction
- 3 days or more: zero mark