Object-Oriented Programming EE219 Repeat 2004/2005 Page 1 of 8

Similar documents
SEMESTER ONE EXAMINATIONS SOLUTIONS 2003/2004

EE219 Object Oriented Programming I Repeat Solutions for 2005/2006

EE219 - Semester /2009 Solutions Page 1 of 10

EE219 Object Oriented Programming I (2005/2006) SEMESTER 1 SOLUTIONS

EE219 Object Oriented Programming I (2007/2008) REPEAT SOLUTIONS

EE219 Object Oriented Programming I (2006/2007) SEMESTER 1 SOLUTIONS

Introduction to Programming Using Java (98-388)

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

CSCE3193: Programming Paradigms

Friend Functions, Inheritance

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 9: Writing Java Applets. Introduction

Object Oriented Design

8. Polymorphism and Inheritance

SEMESTER ONE EXAMINATIONS SOLUTIONS 2004

Java - Applets. public class Buttons extends Applet implements ActionListener

Java Object Oriented Design. CSC207 Fall 2014

15CS45 : OBJECT ORIENTED CONCEPTS

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Lecture 18 CSE11 Fall 2013 Inheritance

CS Exam 1 Review Suggestions

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

DUBLIN CITY UNIVERSITY

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

Assumptions. History

Chapter 1 GUI Applications

Inheritance. Transitivity

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Inheritance. Chapter 7. Chapter 7 1

G51PRG: Introduction to Programming Second semester Applets and graphics

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

Chapter 1: Object-Oriented Programming Using C++

JAVA MOCK TEST JAVA MOCK TEST II

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Using the API: Introductory Graphics Java Programming 1 Lesson 8

Java. GUI building with the AWT

Brief Summary of Java

ICS 4U. Introduction to Programming in Java. Chapter 10 Notes

C ONTENTS PART I FUNDAMENTALS OF PROGRAMMING 1. and Java 3. Chapter 1 Introduction to Computers, Programs,

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Core JAVA Training Syllabus FEE: RS. 8000/-

Chapter 6 Introduction to Defining Classes

Midterm Exam 5 April 20, 2015

The Notion of a Class and Some Other Key Ideas (contd.) Questions:

Packages: Putting Classes Together

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Week 9. Abstract Classes

PROGRAMMING LANGUAGE 2

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Inheritance, and Polymorphism.

Polymorphism Part 1 1

STRUCTURING OF PROGRAM

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CMSC 132: Object-Oriented Programming II

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

Packages & Random and Math Classes

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

ITI Introduction to Computing II

Comp-304 : Object-Oriented Design What does it mean to be Object Oriented?

2. (True/False) All methods in an interface must be declared public.

Object-Oriented Programming (OOP) Fundamental Principles of OOP

About This Lecture. Data Abstraction - Interfaces and Implementations. Outline. Object Concepts. Object Class, Protocol and State.

ITI Introduction to Computing II

7. Program Frameworks

CS 1302 Chapter 9 (Review) Object & Classes

Question 1. 1(a)(vii) (*prttoobject).somemethod(); Page 1 of 18.

CSC1322 Object-Oriented Programming Concepts

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Overloading Example. Overloading. When to Overload. Overloading Example (cont'd) (class Point continued.)

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

We are on the GUI fast track path

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Object Oriented Design

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

CS11 Java. Fall Lecture 3

CS 11 java track: lecture 3

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Computer Programming

Chapter 6: Inheritance

CS3157: Advanced Programming. Outline

John Cowell. Essential Java Fast. How to write object oriented software for the Internet. with 64 figures. Jp Springer

Lecture Static Methods and Variables. Static Methods

Pointers. Developed By Ms. K.M.Sanghavi

CMSC 132: Object-Oriented Programming II

Introduction to Inheritance

B.Sc. (Hons.) Computer Science I B.Sc. (Hons.) Electronics. (i) Runtime polymorphism and compile time polymorphism

University of Swaziland Department Of Computer Science Supplementary Examination JULY 2012

Road Map. Introduction to Java Applets Review applets that ship with JDK Make our own simple applets

L4: Inheritance. Inheritance. Chapter 8 and 10 of Budd.

Advanced Internet Programming CSY3020

Chapter 10 Defining Classes

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Inheritance

Transcription:

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