Professional Documents
Culture Documents
Assignment 01 (Oop)
Assignment 01 (Oop)
Marks: 05
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:
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;
String name;
int ID;
4
String address;
int contact;
this.name=name;
this.ID=ID;
this.address=address;
this.contact=contact;
this.name=original.name;
this.ID=original.ID;
this.address=original.address;
this.contact=original.contact;
5
}
package assignment2;
this.number=number;
this.exponent=exponent;
return number;
return exponent;
package assignment2;
6
import java.util.Arrays;
import java.util.List;
float answer;
double x;
public Polynomial(Terms...termList){
this.termList=Arrays.asList(termList);
for(Terms term:termList){
return answer;
package assignment2;
import java.util.Scanner;
double x=input.nextDouble();
System.out.println("P(x)=" + p.evualuate(x));
OUTPUT: