You are on page 1of 4

Department of Computer Science CSC-210:Object-Oriented Programming

Bahria University, Karachi Campus Semester 02 (Spring 2021)

ASSIGNMENT 03
Marks: 05

NAME:__________________________________________

CLASS:__________________________________________

REG. No._________________________________________

COURSE:________________________________________

COURSE CODE: __________________________________

Read Carefully:
• The deadline for this assignment is before or on Tuesday 25-05-21.

WARNING: This is an individual assignment; you must solve it by yourself. Any form of
plagiarism will result in receiving zero in the assignment.

WARNING: Late submission will not be accepted. Any assignment submitted after the cutoff
time will receive zero.

• You have to answer and submit in HARDCOPY of the given draft on your CR. And CR will submit
your assignement to your faculty on her given deadline.

1|Page
CS Department, BUKC 2/2 Semester 2 (Spring 2021)
CSC-210: OOP Assignment 03

Q1. Read the following given code carefully and answer the asked questions below it:

package valueobjects;

public class Engine {

private Parameter consumption;

public Engine(Parameter parm) {


this.consumption = parm;
}

public Parameter getConsumption() {


return this.consumption;
}

@Override
public boolean equals(Object obj) {
if(obj != null && obj instanceof Engine){
Engine engineToCompare = (Engine)obj;

if(this.consumption.equals(engineToCompare.getConsumption())
)
return true;
}
return false;
}
}
CS Department, BUKC 3/2 Semester 2 (Spring 2021)
CSC-210: OOP Assignment 03

package valueobjects;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Parameter {

private double parameter;

public Parameter(double d) {
this.parameter = BigDecimal.valueOf(d).setScale(4,
RoundingMode.HALF_EVEN).doubleValue();
}

public double getParameter() {


return parameter;
}

@Override
public boolean equals(Object obj) {
if(obj != null && obj instanceof Parameter){
double extracted1 =
BigDecimal.valueOf(((Parameter)
obj).getParameter()).setScale(0,
RoundingMode.HALF_EVEN).doubleValue();
double extracted2 =
BigDecimal.valueOf(this.getParameter()).setScale(0,
RoundingMode.HALF_EVEN).doubleValue();
if(extracted1 == extracted2)
return true;
}
return false;
}
}
CS Department, BUKC 4/2 Semester 2 (Spring 2021)
CSC-210: OOP Assignment 03

QUESTIONS: (2.5 Marks)

1. Differentiate equals method of class Parameter and Engine.


2. Class Parameter is importing 2 classes from math package. What are the uses of both (only
in this code)?
3. Write an application class with following functionality:
a. Create 2 instances of Parameter class with parametrized constructor
b. Find both instances of class Parameter are equal or not
c. Create 2 instances of Engine using parametrized constructor using 2 instances of
class Parameter
d. Find both instances of class Engine are equal or not

Q2. Answer the following questions: (answer should not exceed 3 lines) (2.5 Marks)

1. Can you remove throws clause of a method while overriding it?


2. Can a method or a class be final and abstract at the same time?
3. Can we declare an abstract method as private also?
4. Is it possible to have two methods in a class with same method signature but different
return types?
5. Can we overload main() method?

************END**************

You might also like