Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

OopTh Ass 01

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

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

Bahria University Semester 02 (Fall 2023)

ASSIGNMENT 01
Marks: 05

NAME: MUHAMMAD SAIM


CLASS: BS CS 2-B
REG. NO. 02-134231-022
COURSE: OOP

Marks Obtained: ______________________

Theoretical Assignment

Read Carefully:
• The deadline for this assignment is before or on 06/11/23

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

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

• You have to answer and submit in SOFTCOPY as well as HARDCOPY of the given draft. Submit
SOFTCOPY on your LMS and HARDCOPY to your CR (CR will submit all assignments to Faculty on
the submission date). Both Hardcopy and Softcopy are mandatory.

1|Page
CS Department, BUKC 2 Semester 02 (Fall 2023)
CSC-210: OOP Assignment 01

QUESTION (Do as Directed) (2.5+2.5 = 5 Marks)


(CLO1, PLO1, C2)

1. Infer the four pillars of object-oriented programming (OOP) - encapsulation, inheritance,


polymorphism, and abstraction - contribute to building robust and maintainable software
systems? Provide examples for each pillar to illustrate their importance.

ANSWER# 01:

ENCAPSULATION:
Definition :
Encapsulation is hiding something within the class boundaries with attributes and behaviors.
Importance :
Encapsulation helps in creating self-contained, reusable, and modular code, which is essential for
building robust software.
Example :
(JAVA code)
public class Book {
private String title;
private String author;
private int publicationYear;

public Book(String title, String author, int publicationYear) {


this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}

public void open() {


// Code to open the book
}

public void close() {


// Code to close the book
}

// Getters and setters for attributes


CS Department, BUKC 3 Semester 02 (Fall 2023)
CSC-210: OOP Assignment 01

INHERITANCE:
Definition :
Inheritance allows one class to use properties of other class. The class that uses the properties called
child class and the inherited class called parent class.
Importance :
It make easier to extend and manage the code
Example :
(JAVA code )
public class ElectronicDevice {
protected String brand;
protected String model;

public ElectronicDevice(String brand, String model) {


this.brand = brand;
this.model = model;
}

public void powerOn() {


// Common method to power on the electronic device
}
}

public class Laptop extends ElectronicDevice {


// Laptop-specific attributes and methods
}

public class Smartphone extends ElectronicDevice {


// Smartphone-specific attributes and methods
}

POLYMORPHISM:
Definition :
Polymorphism easily says a single class or object’s ability to play as many forms. In other words, there
can be more characteristics with one class according to the situation.
CS Department, BUKC 4 Semester 02 (Fall 2023)
CSC-210: OOP Assignment 01

Importance :
Polymorphism enhances flexibility and extensibility, as you can write code that can work with various
objects without knowing their exact types.
Example :
(JAVA code)
interface MusicPlayer {
void play();
}

class MediaPlayer implements MusicPlayer {


@Override
public void play() {
// Implementation for playing media
}
}

class Spotify implements MusicPlayer {


@Override
public void play() {
// Implementation for playing music from Spotify
}
}

List<MusicPlayer> players = new ArrayList<>();


players.add(new MediaPlayer());
players.add(new Spotify());

for (MusicPlayer player : players) {


player.play();
}

ABSTRACTION:
Definition :
Abstraction hides unnecessary things and focus on whats is relevant.
Importance :
CS Department, BUKC 5 Semester 02 (Fall 2023)
CSC-210: OOP Assignment 01

Abstraction promotes clarity and reduces complexity, making it easier to design, understand, and
maintain software systems.
Example :
(JAVA code)
public abstract class Shape {
public abstract double calculateArea();
}

public class Circle extends Shape {


private double radius;

public Circle(double radius) {


this.radius = radius;
}

@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}

public class Square extends Shape {


private double sideLength;

public Square(double sideLength) {


this.sideLength = sideLength;
}

@Override
public double calculateArea() {
return sideLength * sideLength;
}
}

2. Summarize the concept of the Constructor, and keep the following statement in consideration to
elaborate your answer. Also, write a small program to copy a constructor.
Polynomial p = new Polynomial(new Term(2.0, 2), new Term(3.0, 1), new Term(-1.0, 0));
CS Department, BUKC 6 Semester 02 (Fall 2023)
CSC-210: OOP Assignment 01

The Polynomial class has a constructor that takes a variable number of Term arguments. It uses the
Arrays.asList method to convert the array of terms into a list. It also has a method evaluate that takes
a variable x and returns the value of the expression evaluated at x. The evaluate method iterates
through the list of terms and computes the value of each term using the Math.pow method and adds
it to the result.

 Also derive the polynomial equation which the class Polynomial is handling.

****************************

CODE # 02:
package assignment;

public class Assignment {

public static void main(String[] args) {


Polynomial p1 = new Polynomial(new Term(2.0, 2), new Term(3.0, 1), new Term(-1.0, 0));
System.out.println("The sum of all the terms of polynomial equation 2x^2 + 3x - 1 solved at x =
5.0 is " + p1.eval(5.0));

Polynomial p2 = new Polynomial(p1); // copying constructor


System.out.println("\np2 returns the same terms as p1: ");
for (Term t: p2.list) {
System.out.println("Coefficient: " + t.coefficient + " Exponent: " + t.exponent);
}
}
}
package assignment;

import java.util.List;
import java.util.Arrays;

public class Polynomial {

List<Term> list; //

public Polynomial(Term... terms) {// tree dots is the concept of varags which means variable
arguments it binds all of the arguments in a single array
this.list = Arrays.asList(terms); // returns an array of fixed sized and then assigned to list
}

public Polynomial(Polynomial poly) { // for copying a constructor


this.list = poly.list;
CS Department, BUKC 7 Semester 02 (Fall 2023)
CSC-210: OOP Assignment 01

public double eval(double x) {


double result = 0.0;
for (Term term : list) { // it iterates through the whole List
result += term.coefficient * Math.pow(x, term.exponent); // it means f(x) it will solve for
whatever value of x will be summed/solve at that value of x
}
return result;
}
}
package assignment;

public class Term {


double coefficient;
int exponent;

public Term(double coefficient, int exponent) {


this.coefficient = coefficient;
this.exponent = exponent;
}

OUTPUT # 02:

You might also like