Solution Exam Questions Set 2 CSC2106 Object-Oriented Design & Programming
Solution Exam Questions Set 2 CSC2106 Object-Oriented Design & Programming
Marking guide
Section A General Knowledge (12 pts)
1. Describe the following characteristics concerning JAVA: Dynamic, robust, object
oriented.
2. What is a Class? Object?
3. Which class allows to create a windows in JAVA?
4. Enumerate the various types of layouts which can be created in JAVA
5. What is the role of new operator?
6. What is the difference between the classic Array and ArrayList in JAVA?
7. Which class allows to display a Table frame in Java?
8. To create a table frame, you need to implement a table model from an abstract class, give
the name of the abstract class and one method of such class.
Section B (14 pts)
1. (3 pts) Write Text‐Based Application using Object‐Oriented Approach to display your
name.
// filename: Name.java
// Class containing display() method, notice the class doesnt have a main() method
public class Name {
public void display() {
System.out.println("Mohamed Faisal");
}
}
// filename: DisplayName.java
// place in same folder as the Name.java file
// Class containing the main() method
public class DisplayName {
public static void main(String[] args) {
Name myname = new Name(); // creating a new object of Name class
myname.display(); // executing the display() method in the Name class
}
}
2. (5 pts) What are the values of the following variables?
(a) double var1 = 10 / 3;
(b) int var2 = (int) (2.5 * 2.6);
(c) boolean var3 = !(3 > 3);
(d) boolean var4 = (121 % 11 == 0) || (5 == 5);
(e)int var5 = 11 % 3;
Answer
(a) 3.0 double var1 = 10 / 3;
(b) 6 int var2 = (int) (2.5 * 2.6);
(c) true boolean var3 = !(3 > 3);
(d) true boolean var4 = (121 % 11 == 0) || (5 == 5);
(e) 2 int var5 = 11 % 3;
3. (3 points) Declare a variable to hold the amount of money in your bank account and
initialize it to
245.25. Name your Java variable in a meaningful way so that any programmer would know
what
value it holds. Your variable name should be at least two words.
answer
(a) str.length()
return type: int , value: 12
(b) str.equalsIgnoreCase("HOW ARE YOU")
return type: boolean , value: false
(c) str.indexOf("ou")
return type: int , value: 9
(d) str.lastIndexOf(" ")
return type: int , value: 7
(e) str.charAt(6)
return type: char , value: ‘e’
(f) str.substring(1, 6)
return type: String , value: “ow ar”
6. What is the difference between the classic Array and ArrayList in JAVA?
- Classic Array is a fixed-size data structure (the array size must always be mentioned) that stores
elements of the same type, while ArrayList is a dynamic-size data structure (that is, not a fixed sized
data structure) that can grow or shrink as needed.
- Classic Arrays can store both primitive types and objects, whereas ArrayList can only store objects.
- Classic Arrays use square brackets for declaration and have a fixed length, while ArrayList uses the
ArrayList class and can be resized.
8. To create a table frame, you need to implement a table model from an abstract class, give the name of the
abstract class and one method of such class.
The abstract class used for implementing a table model in Java is `AbstractTableModel`. One
method of this class is `getValueAt(int row, int column)`, which is used to retrieve the value at a
specific row and column in the table. This method needs to be overridden when implementing the
table model to provide the appropriate data for display in the table.
Section B (14 pts)
1. (3 pts) Write Text‐Based Application using Object‐Oriented Approach to display your
name.
// filename: Name.java
// Class containing display() method, notice the class doesnt have a main() method
public class Name {
public void display() {
System.out.println("Mohamed Faisal");
}
}
// filename: DisplayName.java
// place in same folder as the Name.java file
// Class containing the main() method
public class DisplayName {
public static void main(String[] args) {
Name myname = new Name(); // creating a new object of Name class
myname.display(); // executing the display() method in the Name class
}
}
2. (5 pts) What are the values of the following variables?
(a) double var1 = 10 / 3;
(b) int var2 = (int) (2.5 * 2.6);
(c) boolean var3 = !(3 > 3);
(d) boolean var4 = (121 % 11 == 0) || (5 == 5);
(e)int var5 = 11 % 3;
Answer
(a) 3.0 double var1 = 10 / 3;
(b) 6 int var2 = (int) (2.5 * 2.6);
(c) true boolean var3 = !(3 > 3);
(d) true boolean var4 = (121 % 11 == 0) || (5 == 5);
(e) 2 int var5 = 11 % 3;
3. (3 points) Declare a variable to hold the amount of money in your bank account and
initialize it to
245.25. Name your Java variable in a meaningful way so that any programmer would know
what
value it holds. Your variable name should be at least two words.
answer
(a) str.length()
return type: int , value: 12
(b) str.equalsIgnoreCase("HOW ARE YOU")
return type: boolean , value: false
(c) str.indexOf("ou")
return type: int , value: 9
(d) str.lastIndexOf(" ")
return type: int , value: 7
(e) str.charAt(6)
return type: char , value: ‘e’
(f) str.substring(1, 6)
return type: String , value: “ow ar”
Section C ( 8 pts):
1) Explain characteristics of Object-Oriented Programming.
2) Write a program to find average of all the numbers stored in an array.
3) What is ‘this’ and what are different uses of it? Explain with example.
4) Compare and contrast abstract class and interface.
Answers:
1) Characteristics of Object Oriented Programming include: Encapsulation, Inheritance,
Polymorphism, Abstraction.
- Encapsulation: The bundling of data and methods into a single unit (class) to hide
implementation details.
- Inheritance: The ability of a class to inherit properties and behaviors from another class,
promoting code reuse.
- Polymorphism: The ability to use a single interface to represent different types of objects,
enabling code flexibility.
- Abstraction: The process of simplifying complex systems by breaking them down into
manageable and reusable components.
2) Java program to find the average of numbers in a given array:
java
public class AverageCalculator {
public static void main(String[] args) {
int[] numbers = { 5, 10, 15, 20, 25 };
int sum = 0;
3) The keyword 'this' in Java refers to the current object within a method or constructor. It is
used to differentiate between instance variables and local variables when they have the same
names. Here are some different uses of 'this':
- To refer to instance variables: 'this' can be used to access or modify instance variables
within a class. For example:
java
public class Person {
private String name;
public void setName(String name) {
this.name = name;
}
}
- To invoke constructors: 'this' can be used to call one constructor from another constructor
within the same class. This is useful for constructor chaining. For example:
java
public class Rectangle {
private int width;
private int height;
public Rectangle() {
this(0, 0); // Calling another constructor with 'this'
}
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
}
- To pass the current object as an argument: 'this' can be used to pass the current object as
an argument to other methods or constructors. For example:
java
public class Circle {
private double radius;
public Circle() {
this(1.0); // Passing 'this' as an argument
}
public Circle(double radius) {
this.radius = radius;
}
public void printInfo() {
System.out.println("Radius: " + this.radius);
}
}
5) The wrapper class in Java is used to convert primitive data types into objects. It provides
a way to treat primitive types as objects. Examples of wrapper classes in Java include
Integer, Double, Boolean, etc. These classes provide methods to perform various operations
on the wrapped primitive values, such as converting strings to numbers or comparing values.
Hints: Set the content-pane to 4x2 GridLayout. The components are added from left-to-right, top-to-bottom.
Section E: (6 pts)
import java.io.*;
public class Employee {
Section E 6 pts
import java.io.*;
public class Employee {
// DEPARTMENT is a constant
public static final String DEPARTMENT = "Development ";
int S = 0;
public static void main(String args[]) {
salary = 1000;
for (int i= 0; i<10 ; i= i+2) {
S += i;
}
System.out.println(DEPARTMENT + "average salary:" + salary);
}
(a) Class Car is an abstract class. Explain the role of an abstract class. [1 marks]
(b) Write a Java version of class Car assuming it has this constructor:
public Car(double price, int year)
and that the method calculateSalePrice ( ) is abstract. [3 marks]
(c) Write a Java version of class ClassicCar assuming it has this constructor:
public ClassicCar (double price, int year) [2 marks]
and that the method calculateSalePrice ( ) returns 10,000 as the sale price of the car.
and that the method calculateSalePrice ( ) calculates the sale price of the car as follow:
if year > 2000 then the sale price is 0.75 * its original price; if year > 1995 then the sale price
is 0.5 * its original price; otherwise the sale price is 0.25 * its original price
e) Write a Java version of class CarExhibition assuming it has this constructor:
public CarExhibition( ) [4 marks]
where CarExhibition has cars of different types stored in an arraylist and getTotalPrice method that
returns the total prices of all cars in the exhibition.
0.5 mark
0.5 mark
2 mark
(e) CarExhibition Class Definition
import java.util.ArrayList;
import java.util.Iterator;
public class CarExhibition
{ private ArrayList cars;
public CarExhibition()
{ cars = new ArrayList(); }