Final Oop 2022
Final Oop 2022
(i) Objects are used to model things from a } String table = "";
problem domain.
class Manager extends Employee { String row = "";
(ii) The term polymorphism refers to the fact
that a variable can hold objects of different private String department; for (int i = 1; i < 4; i++) {
types.
public Manager(String name, int id, String for (int j = 1; j < 4; j++) {
(iii) In an interface, all methods are abstract. department) {
int number = rand.nextInt(50);
(iv) The keyword super is used to call super(name, id);
methods in the superclass. row += number + " ";
this.department = department;
(v) Protected is an access control keyword }
that permits access from the subclass, but }
table += row + "\n";
denies access from anywhere not in a class or
public String getDepartment() {
its subclass.
row = "";
return department;
(vi) To cut up an input string into separate
}
words is called tokenizing.
}
System.out.println(table);
(vii) Inheritance allows us to define one class
}
as an extension of another. Part (iii): Interface Issues and Fixes
Part (ii): Code Explanation
(viii) A subclass can override a superclass Issues:
method by declaring a method with the same This piece of code generates a 3x3 table of
signature as the superclass method. random integers between 0 and 49 and prints 1. Method Implementation in
it. Here's what it does step by step: Interface: The ‘printMessage’
(ix) The class variable is declared as static. method contains an
1. Initialize Random and String implementation, which is not
(x) The dynamic type of a variable is the type
Variables: A ‘Random’ object allowed in traditional interfaces.
of the object that is currently stored in the
‘rand’ is created to generate
variable. 2. Typo in Method Name: The
random numbers. Two string
variables, ‘table’ and ‘row’, are method ‘arca’ should likely be
QN 2.
initialized to hold the table and ‘area’.
Part (i): Class Definitions Issues and Fixes the current row, respectively.
Corrected Interface:
Issues: 2. Outer Loop (Rows): The outer
‘for’ loop runs from 1 to 3 public interface Figures {
Accessing Private Members Directly in (inclusive), iterating three times
void printMessage(String s);
Subclass: In the ‘Manager’ class, the to generate three rows.
constructor attempts to access the private double area();
members ‘name’ and ‘id’ directly from the 3. Inner Loop (Columns): The
‘Employee’ class. Private members cannot be inner ‘for’ loop also runs from 1
}
accessed directly by subclasses. to 3 (inclusive), iterating three
times to generate three numbers Part (iv): Class Implementing Corrected
Incorrect Return Type of in each row. Interface
‘getDepartment’: The ‘getDepartment’
method should return a ‘String’ instead of 4. Generate Random Number: public class Rectangle implements Figures {
‘void.’ Within the inner loop, a random
integer between 0 and 49 is private double width;
Fixed Code: generated using
‘rand.nextInt(50)’ and appended private double height;
class Employee { to ‘row’ with a space.
public Rectangle(double width, double
private String name; 5. Append Row to Table: After height) {
completing the inner loop, the
private int id; constructed ‘row’ string is this.width = width;
appended to ‘table’ with a
public Employee(String name, int id) { newline character. this.height = height;
} sb.append(car.toString()).append("\n");
} }
}
• Role of Abstract Class:
Explained as a blueprint for
public double calculateSalePrice() {
other classes, containing both
int year = getYear(); abstract and concrete methods.
import java.util.ArrayList;
public CarExhibition() {
cars.add(car);
double totalPrice = 0;
totalPrice += car.calculateSalePrice();
return totalPrice;