0% found this document useful (0 votes)
10 views3 pages

Final Oop 2022

OBJECT ORIENTED PROGRAMMING OOP

Uploaded by

ibrahgriez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Final Oop 2022

OBJECT ORIENTED PROGRAMMING OOP

Uploaded by

ibrahgriez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

QN 1.

} Random rand = new Random();

(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;

this.name = name; 6. Reset Row: ‘row’ is reset to an }


empty string for the next
this.id = id; public void printMessage(String s) {
iteration of the outer loop.
} System.out.println("This figure is " + s);
7. Print Table: After both loops
complete, the ‘table’ string,
public String getName() { }
which now contains a 3x3 grid
return name; of random numbers, is printed to public double area() {
the console.
} return width * height;
Here's the corrected version of the code with
public int getId() { proper syntax highlighting: }

return id; import java.util.Random; }


QN 3.
• Polymorphism and
public void moveRight(int d) {
inheritance: Defined
(i) Describe Java Virtual Machine (JVM) position.setX(position.getX() + d);
polymorphism and explained
The Java Virtual Machine (JVM) is a part of why inheritance is needed for it. }
the Java Runtime Environment (JRE). It is
responsible for executing Java bytecode, • Class assignments: Identified public void moveForward(int d) {
which is compiled from Java source code. legal and illegal assignments and
The JVM provides platform independence by explained why, with corrections position.setY(position.getY() + d);
allowing Java programs to run on any device where possible.
}
or operating system that has a compatible
JVM installed. It also manages system QN 4. (i) What is a class?
public Point getPosition() {
memory and provides various services such
A class in Java is a blueprint for creating
as garbage collection and security. return position;
objects. It defines a data structure that
(ii) Define Polymorphism and Why Java contains fields (attributes) and methods to
}
Supports Inheritance describe the behaviors and characteristics of
the objects created from the class.
}
Polymorphism in Java refers to the ability of
a variable, function, or object to take on (ii) Class Definition and Implementation
QN 5.
multiple forms. It allows one interface to be
Point Class
used for a general class of actions. The Part (i): Role of an Abstract Class
specific action is determined by the exact
public class Point {
nature of the situation. An abstract class in Java is a class that cannot
be instantiated on its own and is intended to
private int x;
Java supports inheritance to enable be subclassed. It may contain abstract
polymorphism. Inheritance allows a class to private int y; methods (methods without a body) which
inherit fields and methods from another class. must be implemented by subclasses. An
This means a subclass can be treated as an public Point(int x, int y) { abstract class can also contain concrete
instance of its superclass. Polymorphism methods (methods with a body) that provide
enables a single method to work in different this.x = x; common functionality to all subclasses. It
ways depending on the object it is acting serves as a blueprint for other classes.
upon, which is possible because of this.y = y;
inheritance. Part (ii): Java Version of Class ‘Car’
}
(iii) Class Assignments Legality public abstract class Car {
public int getX() {
Given the classes Person, Teacher, and private double price;
Student, with Teacher and Student as return x;
subclasses of Person: private int year;
}
Person p; public Car(double price, int year) {
public int getY() {
Teacher t; this.price = price;
return y;
Student s; this.year = year;
}
t = new Teacher(); }
public void setX(int x) {
p = t; public double getPrice() {
this.x = x;
s = (Student) t; return price;
}
s = (Student) p; }
public void setY(int y) {
p = new Student(); public int getYear() {
this.y = y;
t = new Person(); return year;
}
t = p; }
}
Correcting the illegal assignments: public abstract double
Robot Class calculateSalePrice();
t = (Teacher) new Person();
public class Robot { }
t = (Teacher) p;
private Point position; Part (iii): Java Version of Class
Summary ‘ClassicCar’
public Robot(int x, int y) {
public class ClassicCar extends Car {
• Fixes to interface and class: this.position = new Point(x, y);
Corrected method signature and public ClassicCar(double price, int year) {
implementation issues. }
super(price, year);
• Explanation of JVM: public void moveLeft(int d) {
Described its role and }
position.setX(position.getX() - d);
functionality.
public double calculateSalePrice() {
}
return 10000; // Fixed sale price for for (Car car : cars) {
classic cars

} sb.append(car.toString()).append("\n");

} }

Part (iv): Java Version of Class ‘SportCar’ return sb.toString();

public class SportCar extends Car { }

public SportCar(double price, int year) { }

super(price, year); Summary

}
• Role of Abstract Class:
Explained as a blueprint for
public double calculateSalePrice() {
other classes, containing both
int year = getYear(); abstract and concrete methods.

double price = getPrice(); • Car Class: Defined as an


abstract class with an abstract
if (year > 2000) { method ‘calculateSalePrice’.

return price * 0.75;


• ClassicCar Class: Implements
} else if (year > 1995) { ‘calculateSalePrice’ to return a
fixed value.
return price * 0.5;
• SportCar Class: Implements
} else { ‘calculateSalePrice’ with
conditional logic based on the
return price * 0.25; year.
}
• CarExhibition Class: Manages
} a collection of ‘Car’ objects and
calculates the total sale price.
}

Part (v): Java Version of Class


‘CarExhibition’

import java.util.ArrayList;

public class CarExhibition {

private ArrayList<Car> cars;

public CarExhibition() {

cars = new ArrayList<>();

public void addCar(Car car) {

cars.add(car);

public double getTotalPrice() {

double totalPrice = 0;

for (Car car : cars) {

totalPrice += car.calculateSalePrice();

return totalPrice;

public String toString() {

StringBuilder sb = new StringBuilder();

You might also like