Programming 2 Section 7 OOP (Encapsolation &inestractor)
Programming 2 Section 7 OOP (Encapsolation &inestractor)
7th Section
Object Oriented Programming
Object-oriented programming (OOP) is a way of writing computer programs which is using the idea of
"objects" to represent data and methods.
o Private
o Public
o Protected
o Internal/default
Int id
Double gpa
String name
Void DoAssignment(Boolean flag) {if true -> print “assignment not copied” else “copied”}
package problem1oop;
public class Student {
//Empty/Default constructor
public Student()
name="";
id=0;
gpa=0.0;
//Non-Empty constructor
this.name = name;
this.id = id;
this.gpa = gpa;
return name;
this.name = name;
}
return id;
this.id = id;
return gpa;
this.gpa = gpa;
if (task)
else
..
package problem1oop;
s4.setName("Yara");
s4.setId(20180366);
s4.setGpa(1.5);
s3.study();
s3.DoAssignment(false); }}
Problem – 2 (Fruits)
• Banana
• Melon
• Orange
Fruits
String name
String color
Int weight
Int price
package problem2.object.orianted.programming;
this.name = name;
this.color = color;
this.weight = weight;
this.price = price;
public Fruits() {
name = "";
color = "";
weight=0;
price=0;
return name;
return color;
this.color = color;
return weight;
this.weight = weight;
return price;
this.price = price;
}
System.out.println("Fruit Name is "+name+" Fruit color "+color+" Fruit weight is "+weight+" Fruit
price is "+price);
package problem2.object.orianted.programming;
Orange.setName("orange");
Orange.setColor("orange");
Orange.setWeight(12);
Orange.setPrice(10);
apple.PrintFruitsData();
Banana.PrintFruitsData();
Melon.PrintFruitsData();
Orange.PrintFruitsData();
}
Problem – 3 (Shapes)
Then create 2 objects in main class & call draw() method for each object
• Circle
• Square
Shape
String name
String color
package shapes;
public shape() {
name="";
color="";
this.name = name;
this.color = color;
return name;
this.name = name;
return color;
}
public void setColor(String color) {
this.color = color;
..
package shapes;
circle.Draw();
square.Draw();
}
}