REPEAT EXAMINATIONS SOLUTIONS 2004/2005 MODULE: Object-Oriented Programming for Engineers EE219 COURSE: B.Eng. in Electronic Engineering B.Eng. in Telecommunications Engineering B.Eng. in Digital Media Engineering INSTRUCTIONS: Answer FOUR questions. All questions carry equal marks. Question 1. (a) The corrected code is: #include<iostream.h> class Question1Parent int a; public: Question1Parent(int); virtual void display(); ; class Question1: Question1Parent int b; public: Question1(int,int); virtual void display(); ; Question1Parent::Question1Parent(int x): a(x) Question1::Question1(int x, int y): Question1Parent(x), b(y) void Question1Parent::display() cout << "a = " << a << endl; Page 1 of 8
void Question1::display() Question1Parent::display(); cout << "b = " << b << endl; int main(void) Question1 a(1,2),b(3,4),c(5,6); a.display(); The errors are: 1. Question1Parent(), b(y) missing parameter for constructor 2. missing ; from class definition 3. Question1Parent missing Question1Parent:: 4. c() cannot be called in main() as the default constructor has been replaced. 5. void is missing from the display() method. 6. the Question1Parent constructor is missing its type in the definition. 7. the return in main() is int, where there is no return type [2 points each = 14total] (b) End-of-line comments allow the compiler to ignore from // to the end of the line. Block comments are started by /* and ended by */ End of line comments can be nested and block comments cannot. You should use end of line comments where possible as block comments are very useful for commenting out a section of code when debugging. x = x * 4.533; // explain what you are doing // end of line commenting /* An example of block commenting */ y = 5 * 3; /* TODO: This section needs to be fixed! Derek 25/12/99 j = j * 32.7 + 6; k = k * j + 1; */ (c) The code would be: [5 marks] Account *ptr; ptr = &a; a->display(); [6 marks 2 for each line] Question 2. (a) #include<string> #include<iostream> Page 2 of 8
using namespace std; class Account string owner; int number; float balance; public: Account(string, int, float); virtual void display(); ; class Deposit: public Account float interestrate; public: Deposit(string, int, float, float); Deposit(Account, float); virtual void display(); ; Account::Account(string theowner, int actnum, float thebalance): owner(theowner), number(actnum), balance(thebalance) void Account::display() cout << "The owner is: " << owner << endl; cout << "The account number is: " << number << endl; cout << "The balance is: " << balance << endl; Deposit::Deposit(string theowner, int actnum, float thebalance, float rate): Account(theOwner, actnum, thebalance), interestrate(rate) Deposit::Deposit(Account theaccount, float rate): Account(theAccount), interestrate(rate) void Deposit::display() Account::display(); cout << "The interest rate is: " << interestrate << endl; (b) [15 marks 3 per method] int main(void) Account a("derek Molloy", 12345, 1000.00f); Deposit d(a, 0.05f); d.display(); [6 marks] (c) Default parameters can be added to the end of a parameter list. If a value is to be passed to a particular default parameter, then the preceding default parameters must also be passed. For example: void decrement(int &avalue, int amount=1) Page 3 of 8
avalue = avalue - amount; [4 marks] Question 3. (a) What are abstract classes? How does the notation differ from C++ to Java? An abstract class is a class that is incomplete, in that it describes a set of operations, but is missing the actual implementation of these operations. Abstract classes: Cannot be instantiated. So, can only be used through inheritance. For example: In the Vehicle class example previously the draw() method may be defined as abstract as it is not really possible to draw a generic vehicle. By doing this we are forcing all derived classes to write a draw() method if they are to be instantiated. virtual string getaccounttype() = 0; The assignment = 0 allows the method to be defined as abstract. This means that no implementation will be present for that method, in this case the getaccounttype() method. While abstract classes are not much different to abstract classes in C++, there are some points to note. Once again, abstract methods are a place-holder that define a set of operations within the class that have not yet been implemented. In Java you can actually set a class to be abstract, even if it has no abstract methods. This would prevent this class from being instantiated. However, if one or more methods in a class are abstract then the class must be defined as abstract. [12 marks] Page 4 of 8
[6 marks for description] [3 cpp notation] [3 java notation] (b) Explain pass-by-value and pass-by-reference. Write a short segment of code to demonstrate the difference? 8 float addinterest(float val, float rate) 9 10 return val + (val * (rate/100)); 11 12 13 void main() 14 15 float balance = 5000; 16 float irate = 5.0; 17 18 balance = addinterest(balance, irate); 19 20 cout << "After interest your balance is " 21 << balance << " Euro." << endl; 22 The previous code segment passed values to the function using pass by value. In this case you are really passing the value of the variables balance and irate, so in this case the numbers 5000 and 5.0. It is only possible to have one return value when passing by value. If we wish to have multiple return values, or wish to modify the source then we can pass by reference. This example is the same as the previous example except this time we are passing by reference: Passing by reference is like passing a copy of the name of the variable to the method. void addinterest(float &val, float rate) 9 10 val = val + (val * (rate/100)); 11 12 13 void main() 14 15 float balance = 5000; 16 float irate = 5.0; 17 18 addinterest(balance,irate); 19 20 cout << "After interest your balance is " 21 << balance << " Euro." << endl; 22 [13 marks] [4 marks for each segment] [5 marks for discussion] Question 4. Page 5 of 8
(a) The String class provides a convenient mechanism for working with strings in Java. String objects in Java are immutable - once assigned a value, it cannot be changed. The compiler converts string constants to String objects directly. String object comparisons are achieved using the compareto() and equals() methods. The StringBuffer object can be used instead of a String object when more flexibilty is required in the object. The StringBuffer class is mutable and so can grow in size as the application runs. We can convert directly between a String object and a StringBuffer object when required. StringBuffer buffer = new StringBuffer(s); buffer.insert(5, " to the"); String t = buffer.tostring(); (b) Explain the concept of packages in Java. How are they used? [8 marks] Packages are groups of similar classes, that can be, but are not necessarily related to each other through an inheritance structure. Packages are classes organised into directories on a file system. Packages are '.' separated words, where the package refers to the directory that the class files are in. For example java.awt (the directory /java/awt/ is a package that stores the classes for creating buttons, textfields, etc. A package can also contain more packages in a subfolder. For example, classes for events, within the awt package. java.awt.event contains Use the import keyword to load in packages. Multiple classes can be loaded using the * character. So, for example, import java.awt.*; imports all the classes in the awt package, but please note, it does not include classes in the sub-packages, so, you must explicitly import java.awt.event.*;. In a source file, if no package name is defined on an import e.g. import SomeClass; then it is assumed that the class SomeClass is in the same directory as the source file. (c) Explain the Java terms super and this. [5 marks] super - allows us to refer to the parent class directly. So, super.display() would call the display() method, written or inherited by the parent class. If the parent class of the parent class with a display() method also had a display() method, we do not have access to that method. this - allows us to refer to the object of this class that we are currently within. It allows us to pass a reference to the current object and it also allows us to access the states of the class that are currently out of scope. (b) Explain the Java classes Object class and Class class. [6 marks] [3 marks each] By default, every class extends the Object class, even if you don't extend any class. The Object class provides methods that are inherited by all Java classes, such as equals(), getclass(), tostring() and more. The Class class (called class descriptors) are automatically created and associated with the objects to which they Page 6 of 8
refer. For example, the getname() and tostring() methods return the String containing the name of the class or interface. We could use this to compare the classes of objects. (c) What is a Java interface? [6 marks] [3 marks each] A Java Interface is a way of grouping methods that describe a particular behaviour. They allow developers to reuse design and they capture similarity, independent of the class hierarchy. We can share both methods and constants using Interfaces, but there can be no states in an interface. Methods in an interface are implicitly public and abstract. Constant states in an interface are implicitly public, static and final. [3 marks] Question 5. (a) Write the Java code and HTML code for the applet below. /* * Created on Oct 22, 2004 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ import java.awt.*; import java.applet.*; import java.awt.event.*; /** * @author Derek Molloy * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class TestClass extends Applet implements ActionListener private Button rev= new Button("Reverse Text"); private Button clr = new Button("Clear Text"); private TextField t = new TextField(20); public void init() this.add(t); this.add(rev); this.add(clr); rev.addactionlistener(this); clr.addactionlistener(this); Page 7 of 8
private void reversetext() int len = t.gettext().length(); char[] newtext = new char[len]; for (int i=0; i<len; i++) newtext[i] = t.gettext().charat(len-i-1); t.settext(new String(newText)); public void actionperformed(actionevent e) if (e.getsource().equals(rev)) reversetext(); else t.settext(""); <html><title>test</title> <body> <center> <applet width=500 height=100 code="testclass.class"></applet> </center> </html> Marks [3 for html][3 for buttons][2 for textfield][3 for counting][2 for layout][3 for events] [2 for update][3 for actionperformed] [20 in total] Java provides us with the Canvas class that acts like any other component, but also allows you to draw graphics directly to it - just like an artist's canvas. The other advantage of using a component is that you can take advantage of the layout managers to control how the applet/application will place the component as it is moved or resized. The Canvas component specifies its co-ordinate system at (0,0) for the top left-hand corner. Canvas components can handle mouse events like the Applet class. You must subclass Canvas to add the behaviour you require, modifing the paint() method. So, for example: [5 marks] Page 8 of 8