August 1999
JP219 : JAVA PROGRAMMING

QUESTION 1 (Compulsory)

Total Marks: 20 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 1

(a) (i) Explain what is meant by the term Java Virtual Machine.

(ii) Explain why the concept of the virtual machine aids the portability of code.

[3]

[2]

(i) The Java Virtual Machine is
  • An imaginary machine ;
  • Which is emulated in software ;
  • In a real machine.

 

(ii) Portability is easier because
  • Code is complied for a generic machine specification;
  • Thus removing specifics of the hardware on which it it to be run.

 

(b) Java is an Object Oriented programming language. Explain what is meant by the following terms in the context of object oriented programming:

(i) Encapsulation

(ii) Inheritance

(iii) Overloading


[1]

[1]

[1]

(i) Encapsulation is when data and methods operating on that data are described in the same structure.

 

(ii) Inheritance is when a new data type of created which incorporates all the members of a parent data type.

 

(iii) Overloading is when two methods in a class have the same name, but different parameterizations.

 

(c) Identify the difference between a class and an object. [2]
  • A class is a description of a data type;
  • An object is a particular instance of a class.

 

(d) (i) Define a public java class MyAddress, which contains 2 member variables, HouseNumber, which is an integer, and StreetName, which is a string.

(ii) Write a constructor function for your class, which will take in two parameters that will initialize the member variables of your class.

(iii) Write a function ShowMeHome, which is a public member of your class MyAddress, with a void return type, which will print out the values of HouseNumber and StreetName.

(iv)Write a small fragment of Java code which will construct an object of type MyAddress, and cause it to print out its members.

[3]

[2]

[3]



[2]

(i) A sample definition of MyAddress follows :

public class MyAddress {
   int HouseNumber;
   String StreetName;
}

 

(ii) A sample definition of MyAddress constructor follows:

MyAddress (int num, String Street) {
   HouseNumber = num;
   StreetName = street;
}

 

(iii) A sample definition of ShowMeHome follows:

void ShowMeHome( ) {
   System.Out.printIn(HouseNumber + StreetName);
}

 

(iv) A sample definition of the code follows:

MyAddress MyHome - new MyAddress (1, "my address");
MyHome.ShowMeHome( );