Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

ASSIGNMENT 01

Marks: 05

NAME: ABEHA TARIQ


CLASS: 2B
REG. NO.: 02-134231-003
COURSE: BS-CS
COURSE CODE: CSC-210

Marks Obtained: ______________________

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.

Object-Oriented Programming (OOP) is an approach to software development that uses


objects, or data structures consisting of data fields and methods together with their
interactions, to design applications and computer programs. The four pillars of OOPs allow
developers to better manage software complexity by organizing code into objects and
allowing them to interact in a structured way.
The four pillars of OOP are:
Abstraction, Encapsulation, Inheritance and Polymorphism.

1. Data abstraction is the process of hiding unnecessary details of an object’s


internal structure. By abstracting an object’s data, its structure and behavior can
be kept separate and more easily understood.
2. Encapsulation is the process of wrapping data and related functions into a single
unit (object). Encapsulation limits access to object data and methods, preventing
their misuse and ensuring their proper functioning.
3. Inheritance is the ability to create a new class (child class) from an existing one
(parent class). The child class typically inherits the attributes (members and
methods) of the parent class, although it can also redefine them.
4. Polymorphism is the ability of an object to take on multiple forms. This allows
objects of different classes to be used interchangeably, as long as they implement
a certain interface (have methods of the same name).

1
Four pillars of OOPs enable developers to create robust and maintainable code, by grouping
related data and behavior into objects that can be easily understood and reused. By
implementing these pillars in object-oriented programming languages, developers can
create reliable and scalable software solutions.

EXAMPLE CODES:

ABSTRACTION:

1. abstract class Bike{


2. abstract void run();
3. }
4. class Honda4 extends Bike{
5. void run(){System.out.println("running safely");}
6. public static void main(String args[]){
7. Bike obj = new Honda4();
8. obj.run();
9. }
10. }

ENCAPSULATION:
1. public class Student{
2. //private data member
3. private String name;
4. //getter method for name
5. public String getName(){
6. return name;
7. }
8. //setter method for name
9. public void setName(String name){
10. this.name=name
11. }
12. }
13. class Test{
14. public static void main(String[] args){
15. //creating instance of the encapsulated class
16. Student s=new Student();
17. //setting value in the name member

2
18. s.setName("ABC");
19. //getting value of the name member
20. System.out.println(s.getName());
21. }
22. }

INHERITANCE:

1. class Employee{
2. float salary=40000;
3. }
4. class Programmer extends Employee{
5. int bonus=10000;
6. public static void main(String args[]){
7. Programmer p=new Programmer();
8. System.out.println("Programmer salary is:"+p.salary);
9. System.out.println("Bonus of Programmer is:"+p.bonus);
10. }
11. }

POLYMORPHISM:

1. class Bike{
2. void run(){System.out.println("running");}
3. }
4. class Splendor extends Bike{
5. void run(){System.out.println("running safely with 60km");}
6.
7. public static void main(String args[]){
8. Bike b = new Splendor();//upcasting
9. b.run();
10. }
11. }

3
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

Polynomial p = new Polynomial(new Term(2.0, 2), new Term(3.0, 1), new Term(-1.0, 0));
constructor.

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.

A constructor in Java is a special type of method that is used to initialize objects. The
constructor is called when an object of a class is created. It can be used to set initial values
for objects attributes. There are two types of constructors: Default and Parametirized. A key
point to know is that the constructor name should be the same as the class name.

COPYING A CONSTRUCTOR:

package assignment2;

public class Copy {

String name;

int ID;

4
String address;

int contact;

public Copy(String name, int ID, String address, int contact){

this.name=name;

this.ID=ID;

this.address=address;

this.contact=contact;

public Copy(Copy original){

this.name=original.name;

this.ID=original.ID;

this.address=original.address;

this.contact=original.contact;

public class Assignment1{

public static void main(String[]args){

Copy copied = new Copy("Willaim Moriarty",111,"Baker Street",00442364611);

Copy copying = new Copy(copied);

5
}

POLYNOMIAL EQUATION CODE:

package assignment2;

public class Terms {

private double number;

private int exponent;

public Terms( double number, int exponent){

this.number=number;

this.exponent=exponent;

public double getNumber(){

return number;

public int getExponent(){

return exponent;

package assignment2;
6
import java.util.Arrays;

import java.util.List;

public class Polynomial {

float answer;

double x;

private List<Terms> termList;

public Polynomial(Terms...termList){

this.termList=Arrays.asList(termList);

public float evualuate(double x){

for(Terms term:termList){

answer=(float) (answer+ term.getNumber()* Math.pow(x,term.getExponent()));

return answer;

package assignment2;

import java.util.Scanner;

public class Assignment1 {


7
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

Terms t1 = new Terms(2.0,2);

Terms t2 = new Terms(3.0,1);

Terms t3 = new Terms(-1.0,0);

Polynomial p = new Polynomial(t1,t2,t3);

System.out.println(" enter value of x: ");

double x=input.nextDouble();

System.out.println("P(x)=" + t1.getNumber() + " x^ " + t1.getExponent()+" + "+


t2.getNumber()+" x^ "+t2.getExponent()+ " + "+t3.getNumber()+" x^ "+t3.getExponent());

System.out.println("P(x)=" + p.evualuate(x));

OUTPUT:

You might also like