examination java jg 310 sem 1 a

28
S249/201 DUBLIN INSTITUTE OF TECHNOLOGY KEVIN STREET, DUBLIN 8. _____________ BSc Information Systems/ Information Technology Stage 2 ______________ SEMESTER 1 EXAMINATIONS 2010 ______________ MARKING SCHEME Mr. J. Gilligan Dr. D Lillis Dr M Ali January 2010 2hrs Answer Question One and any two others Question One carries 30 marks, all others carry 35 marks

Upload: lakhwinder

Post on 13-Nov-2015

221 views

Category:

Documents


2 download

DESCRIPTION

info abt original

TRANSCRIPT

Describe three differences between Object Oriented and Traditional Imperative Programming

S249/201

DUBLIN INSTITUTE OF TECHNOLOGY

KEVIN STREET, DUBLIN 8.

_____________

BSc Information Systems/ Information Technology

Stage 2

______________

SEMESTER 1 EXAMINATIONS 2010______________

MArking Scheme

Mr. J. Gilligan

Dr. D Lillis

Dr M AliJanuary 2010

2hrs

Answer Question One and any two others

Question One carries 30 marks, all others carry 35 marks

1:a) Define what is meant by Object Oriented Programming and describe two ways in which it differs from traditional imperative programming.

(6marks)Sample AnswerObject-oriented programming may be seen as a collection of cooperating objects, as opposed to a traditional view in which a program may be seen as a group of tasks to compute ("subroutines"). In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects.

Each object can be viewed as an independent little machine with a distinct role or responsibility. The actions or "operators" on the objects are closely associated with the object.

For example, in object oriented programming, the data structures tend to carry their own operators around with them (or at least "inherit" them from a similar object or "class"). The traditional approach tends to view and consider data and behavior separately.Question RationaleThe students should be able to articulate the underlying philosophy of the object oriented paradigm.

Marking Scheme2 marks for Definition2 X 2 marks for each difference.b) State whether or not each of the following declarations is legal in JAVA ?In each case explain your answer.A. public MyClass {//...}

B. public protected int myVar;

C. friendly Button myButton;

D. Label myLabel

(4 marks)Answer

A ok

B wrong, cannot be public and protected

C wrong no friendly modifierD OK if Label is a defined class

Question Rationale

The objective of this question is to assess the students awareness of the access modifiers in Java.

Marking Scheme4 X 1 marks for correct answer

c) class TClass {

public static void main(String [] args) {

int arr[] = new int[args.length];

float var1 = 0;

for (int i = 0; i < args.length; i++)

arr[i] = (new Integer(args[i])).intValue();

for (int i = 0; i < arr.length; i++)

var1 = (i%2==0)?0:arr[i];

System.out.println(var1/args.length);

}

}

What would be the output of the above program given the following invocation:

> java classpath . TClass 20 45 60 85 100 205 300 405

Explain how you arrived at your answer.(10 marks)

Answer

Answer 50.625

The argument strings are each convereted to an integer value.

This program takes the last arg value and if its index is even it assigns 0 to var1 otherwise it assigns the integer equivalent of the last arg value 405 to var1.It then divides 405 by 8 the no of arguments which yields 50.625

And this is output.

Question RationaleThis question is intended to assess the students understanding of java code.There are some subtle elements in this program

It uses command line arguments.

It uses the ? operator

It uses casting.

The students will need to get these features in order to arrive at a correct outputMarking Scheme5 marks for correct answer5 marks for explaining how it arrivedd) Write a program to print out the following:1

1 2

1 2 3

1 2 3 4

1 2 3 4 5 6 7 8 9 10 (10 marks)

// First Program Trianglenos

public class Trianglenos{

public static void main(String args []){

int i;

String s1;

s1 = "1";

System.out.println(s1);

for(i=2;i < 11;i++) {

s1 = s1 + " " + i;

System.out.println(s1);

};

}

}

Question Rationale

The students previous language was C. We did exercises like these in order to establish the commonality of the Java Syntax with C. This exercise is designed to refelect that experience.Marking Scheme6 marks for basic attempt at program4 marks for good explanation or annotation.2:a) Describe briefly , the concept of Semantic Networks. In particular show how semantic nets can model abstract superclasses. Support Your Answer with appropriate illustration.

(6 marks)

Sample Answer

Semantic networks are a popular scheme which elegantly reflect these ideas.

A network consists of nodes repesenting objects, concepts and events and links between the nodes representing their interrelations.

The development of semantic networks had its origins in psychology. Ross Quillian in 1968 designed two semantic network based systems that were intended primariliy as psychological models of associateive memory.

Semantic Networks quickly found application in AI. B. Raphaels SIR system, also 1968, was one of the first programs to use this type of representation scheme.

SIR was a question Answering system and could answer questions requiring a variety of simple reasoning tasks and relationships

Example

While there are no fixed conventions, a number of important features of Semantic nets have emerged , that are widely used.

These have largely emerged because of the application of Semantic Nets to Object Oriented Theory. The central aspect of the object paradigm is how it defines objects.

The basic mechanism of representation is the articulation of class hierarchies.

Instances of Objects exist. In turn Objects belong to classes and these in turn can belong to other classes The central aspect of the object paradigm is how it defines objects.

The basic mechanism of representation is the articulation of class hierarchies.

Instances of Objects exist. In turn Objects belong to classes and these in turn can belong to other classesQuestion RationaleSemantic nets are the basis for the object model in object orieneted representation. The students should show awareness of their featuresMarking Scheme

3 marks for overview

1 mark for class hierarchy

2 for diagrams

b) Represent the following sentence as a Semantic Network..

I own a plastic green chair(7 marks)

Answer

Question Rationale

This is a practical application of this representation scheme. The students have done a practical on this topic and I expect a detailed net which accurately reflects the elements of this picture.

Marking Scheme

4 marks for a comprehensive representation

3 marks for relevant extra details included in the network such as implied class associations.

c) In a constructor of a derived class, how do you call the parent's constructor?

(2 marks)

Answer

Using super keywordQuestion Rationale

This is to do with basic syntactic realisation of inheritance construct in Java Marking scheme

2 marks for correct answer

d) Design an Account Class which has a balance field and three methodsi) Deposit , ii) Withdraw and iii) Get Balance:(6 marks)

Answerpublic class Account

{ protected double balance;

// Constructor to initialise balance

public Account( double amount ) { balance = amount; }

// Overloaded constructor for empty balance

public Account() { balance = 0.0; }

public void deposit( double amount )

{

balance += amount;

}

public double withdraw( double amount )

{ // See if amount can be withdrawn

if (balance >= amount) { balance -= amount; return amount; } else

// Withdrawal not allowed return 0.0; }

public double getbalance()

{ return balance; }

}

Question Rationale

This exercise is addresses basic Java User Class definition

Marking Scheme

3 marks for basic attempt

3 marks for completenessc ) Design and Implement a Savings Account Class which is a subclass of Account and has

i) Extra fields representing a default Interest Rate and a variable Interest Rateii) One Constructor which sets the variable interest rate to the default interest rate and the balance to 0 and a second constructor which takes the opening balance and interest rate value as parameters.ii) Extra method to calculate the monthly interest by multiplying the balance by variable Interest Rate divided by 12; this interest should be added to Account Balance.(8marks)Answerclass SavingsAccount extends Account

{

// Default interest rate of 7.95 percent (const)

private static double default_interest = 7.95;

// Current interest rate

private double interest_rate;

// Overloaded constructor accepting balance and an interest rate

public SavingsAccount( double amount, double interest)

{

balance = amount;

interest_rate = interest;

}

// Overloaded constructor accepting balance with a default interest rate

public SavingsAccount( double amount )

{

balance = amount;

interest_rate = default_interest;

}

// Overloaded constructor with empty balance and a default interest rate

public SavingsAccount()

{

balance = 0.0;

interest_rate = default_interest;

}

public void add_monthly_interest()

{

// Add interest to our account

balance = balance +

(balance * interest_rate / 100) / 12;

}

}

Question Rationale

This exercise is addresses basic Java User Class Derived Class definition

Marking Scheme

1 marks for correctly using extends and

2 for correctly accessing parent class

1 X 3 for each section

2 for completeness of implementation

d) Write a control program which uses the classes in parts b and c to create a Savings account and whichi) Deposits 250 euros

ii) Withdraws 80 euros

iii) Calculates the monthly interest on current Balance

After each action the Account balance should be displayed.(6 marks)

Answer

/* * * SavingsAccountDemo * Demonstration of SavingsAccount class * */

class SavingsAccountDemo

{ public static void main(String args[])

{

// Create an empty SavingsAccount

SavingsAccount my_SavingsAccount = new SavingsAccount();

// Deposit money

my_SavingsAccount.deposit(250.00);

// Print current balance

System.out.println ("Current balance " + my_SavingsAccount.getbalance());

// Withdraw money

my_SavingsAccount.withdraw(80.00);

// Print remaining balance

System.out.println ("Remaining balance " + my_SavingsAccount.getbalance());

my_SavingsAccount.add_monthly_interest();

System.out.println ("Remaining balance " + my_SavingsAccount.getbalance());

}

}Question Rationale

This exercise is addresses basic Java Control Program definitionThe student needs to be able to correctly use defined classes.Marking Scheme

2 marks for correct instantiation

1 mark X 3 marks for correct method invocation for each of the subtasks requested.1 mark for displaying balance3: a) Given the following abstract Singer Class:public abstract class Singer

{

private String nameOfSinger;

// This abstract method is to output the kind of songs the singer sings

public abstract void sings();

public String getSingerName()

{

return nameOfSinger;

}

public void setSingerName(String name)

{

nameOfSinger = name;

}

}

Create three subclasses of Singer which describe Jazz Singers, Pop Singers and Opera Singers.(6 marks)Answerpublic class PopSinger extends Singer

{

public void sings()

{

System.out.println("Sings Pop Songs");

}

}

public class JazzSinger extends Singer

{

public void sings()

{

System.out.println("Sings Jazz songs");

}

}

public class OperaSinger extends Singer

{

public void sings()

{

System.out.println("Sings Classical Songs"); }}

Question RationaleThis question looks at abstract java constructs and how these can be made concrete through the definition of subclasses.

Marking Scheme

2 marks X 3 for each subclass

b) Write a control program which demonstrates the use of the classes defined in part a above(6 marks)Answerpublic class UseSingers

{

public static void main(String[] args)

{

JazzSinger myJazzSinger = new JazzSinger();

PopSinger myPopSinger = new PopSinger();

OperaSinger myOperaSinger = new OperaSinger();

myJazzSinger.setSingerName("Murphy");

myPopSinger.setSingerName("Elsie");

myOperaSinger.setSingerName("Sammy");

System.out.print(myJazzSinger.getSingerName() + " says ");

myJazzSinger.sings();

System.out.print(myPopSinger.getSingerName() + " says ");

myPopSinger.sings();

System.out.print(myOperaSinger.getSingerName() + " says ");

myOperaSinger.sings();

}

}

Question Rationale

This exercise is addresses basic Java Control Program definitionThe student needs to be able to correctly use defined classes.Marking Scheme

2 marks for correct instantiation

1 mark X 3 marks for correct method invocation of each class sing method

1 mark for any extra detail.c) Define what is meant by a java interface;

(4 marks)

Answer

An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are declared using the interface keyword and may only contain method signatures and constant declarations (variable declarations which are declared to be both static and final).

Question Rationale

The interface facility in Java is a big mechanism in creating portable systems

The students should be able to describe this concept

Marking Scheme

3 marks for good articulation of interface concept, 1 mark for anything extra and relevant.d)

i) Define a java interface called Professional which has one method signature called work.

(4 marks)Answer

public interface Professional

{

public void work();

}

Question Rationale

This objective here is to show ability to correctly define a small interface

Marking Scheme

4 marks for correct interface definitionii) Define a User Class called ProfessionalJazzSinger which extends the JazzSinger class defined in a above and which implements the Professional interface.This class is to have an extra field to represent hours spent training and methods to get and set this.(9 marks)

Answer

public class ProfessionalJazzSinger extends JazzSinger implements Professional

{

private int hoursOfTraining;

public void setHoursOfTraining(int hrs)

{

hoursOfTraining = hrs;

}

public int getHoursOfTraining()

{

return hoursOfTraining;

}

public void work()

{

System.out.println("I am a JazzSinger who works");

System.out.println("I have " + hoursOfTraining +

" hours of professional training!");

}

}

Question Rationale

In the first part of this question an interface was defined. In this part we implement the interface.

Marking Scheme2 marks for correct use of implements keyword2 marks for correctly defining subclass

2 marks for the get and set methods

3 marks for definition of work method.

e) Write a control program which demonstrates the use of the ProfessionalJazzSingers class defined in part e above.(6 marks)Answer

public class DemoProfessionalJazzSingers

{

public static void main(String[] args)

{

ProfessionalJazzSinger dizzy = new ProfessionalJazzSinger();

ProfessionalJazzSinger jazzy = new ProfessionalJazzSinger();

dizzy.setSingerName("Simon");

jazzy.setSingerName("Sophie");

dizzy.setHoursOfTraining(40);

jazzy.setHoursOfTraining(300);

System.out.println(dizzy.getSingerName() + " says ");

dizzy.sings();

dizzy.work();

System.out.println();

System.out.println(jazzy.getSingerName() + " says ");

jazzy.sings();

jazzy.work();

}

}

public class DisplayJazzSinger

{

public static void main(String[] args)

{

JazzSinger myJazzSinger = new JazzSinger();

String JazzSingerString = myJazzSinger.toString();

System.out.println(JazzSingerString);

}

}

Question Rationale

This exercise is addresses basic Java Control Program definitionThe student needs to be able to correctly use defined classes.Marking Scheme

2 marks for correct instantiation

1 mark X 3 marks for correct method invocation of each class sing method

1 mark for any extra detail.4:

a) Courses is an array which contains the following DIT course codes:

{DT211,DT249, DT228,DT210}

Write a java program which takes a course code as input and checks to see if that code is in the Courses array.

(10 marks)Answer

import javax.swing.*;

public class SearchList

{

public static void main(String[] args)

{

String[] deptName = {DT211,DT249, DT228,DT210};

String code;

int x;

boolean codeWasFound = false;

code = JOptionPane.showInputDialog(null,

"Enter a department name");

for(x = 0; x < deptName.length; ++x)

if(code.equals(deptName[x]))

codeWasFound = true;

if(codeWasFound)

JOptionPane.showMessageDialog(null, code +

" was found in the list");

else

JOptionPane.showMessageDialog(null, code +

" was not found in the list");

System.exit(0);

}

}

Question Rationale

The students previous language was C. We did exercises like these in order to establish the commonality of the Java Syntax with C. One key follow an was with arrays.

This exercise is designed to reflect that experience.

Marking Scheme

6 marks for basic attempt at program

4 marks for good explanation or annotation.

b) Compare and contrast vectors and arrays in java

(4 marks)Answer

Vectors are much like arrays in that Vectors are

homogeneous - vectors typically store only one kind of element;

indexed - vectors permit clients to access values using integers that indicate position;

efficient - vectors provide fast access to values.

Vectors differ from arrays in that they are expandable - you can continue to add elements to a vector, even if the vector has grown beyond its original size. Question Rationale

This is to evaluate students understanding of the array and vector classes of java.

They are similar in many respects but different in the crucial ability to expand of vectors and in the methods employed.

Marking scheme

2 for similarities

2 for differences.

c) Define a class Person which has fields , first name and surname and defines a toString method.(5 marks)Answer

public class Person

{

private String firstName;

Provate String surname;

public Person(String firstName, String surname)

{

this.firstName = firstName;

this.surname = surname;

}

public String toString()

{return firstName + " " + surname;

}

}

Question Rationale

This exercise is addresses basic Java User Class definition

Marking Scheme

3 marks for basic attempt

2 marks for completenessd) Write a java class which uses vectors to represent a crowd of people as defined by the Person class in part c above

(8 marks)

import java.util.*;

class Crowd

{

private Vector people;

public Crowd()

{

people = new Vector();

}

public Crowd(int numPersons)

{

people = new Vector(numPersons);

}

public boolean add(Person someone)

{

return people.add(someone);

}

Person get(int index)

{

return (Person)people.get(index);

}

public int size()

{

return people.size();

}

public int capacity()

{

return people.capacity();

}

public Iterator iterator()

{

return people.iterator();

}

}Question Rationale

This exercise is addresses basic Java User Class Derived Class definitionSignificantly it also involves the use of a vector.Marking Scheme

1 marks for correctly using extends and

2 for correctly accessing parent class

3 marks for correctly using vector2 for completeness of implementation

e) Write a control program which uses the crowd class and which does the following:

i) Adds five people to the crowd

ii) Outputs the 3rd person in the crowd

iii) Removes the 2nd person from the crowd

iv) Outputs the size of the crowd.

(8 marks)Answerpublic class Crowddemo{

public static void main(String[] args)

{

Crowd crowd1 = new Crowd() Person p1 = new Person(JR, Ewing);

crowd1.add(p1);

Person p2 = new Person(Bobby , Ewing);

crowd1.add(p2); Person p3 = new Person(SueEllen, Ewing);

crowd1.add(p3);

Person p4 = new Person(Granny , Ewing);

crowd1.add(p4); Person p5 = new Person(Chuck , Ewing);

crowd1.add(p5);

Person p6 = crowd1.get(2);

System.out.println(p6.toString());

crowd1.remove(1);

System.out.println(size is + crowd1.size());}}Question Rationale

This exercise is addresses basic Java Control Program definitionThe student needs to be able to correctly use defined classes.Marking Scheme

2 marks for correct instantiation

1 mark X 4 marks for correct method invocation of each class sing method

2 marks for any extra detail.

Dog

bone

likes