You are on page 1of 15

Programming 2

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.

• Classes & Objects


A class is a blueprint for creating objects, providing initial values for state (variables or
attributes), and implementations of behavior (functions or methods).

An object refers to a particular instance of a class.


• Access Modifiers
Access modifiers are keywords in object-oriented languages that set the accessibility of classes,
methods, & variables.

o Private

o Public

o Protected

o Internal/default

• Constructors(default & other)


A constructor is a special type of subroutine called to create an object. It prepares the new
object for use.

• Getters & setters + this


Getter is a method that retrieves a value, and a setter is a method that modifies a value.
Problem – 1(Student)

Write a Java program to create a class called Student as following with


building your constructor where you can pass the every student data when
creating an object.

Making a function study printing a message

“student name is studying” with no return type

DoAssignment(Boolean flag) print “Assignment is not copied” if true,


“Assignment Copied” if false.
Student

Int id

Double gpa

String name

Setters & getters

Void study() {student name + “is studying”}

Void DoAssignment(Boolean flag) {if true -> print “assignment not copied” else “copied”}

package problem1oop;
public class Student {

private String name;

private int id;

private double gpa;

//Empty/Default constructor

public Student()

name="";

id=0;

gpa=0.0;

//Non-Empty constructor

public Student(String name, int id, double gpa) {

this.name = name;

this.id = id;

this.gpa = gpa;

public String getName() {

return name;

public void setName(String name) {

this.name = name;
}

public int getId() {

return id;

public void setId(int id) {

this.id = id;

public double getGpa() {

return gpa;

public void setGpa(double gpa) {

this.gpa = gpa;

public void study()

System.out.println("Hey there I am "+name +" & I am studying for the Quiz");

public void DoAssignment(Boolean task)

if (task)

System.out.println("I didn't Copy the assignment");


}

else

System.out.println("I did Copy the assignment so Horray I am getting a ZERO!");

..

package problem1oop;

public class Problem1OOP {

public static void main(String[] args) {

Student s1 = new Student ("Radwan",20183560,1.5);

Student s2 = new Student ("Hatem", 20181838,1.5);

Student s3 = new Student ("Elsadat Zaki", 20184102,1.5);

Student s4 = new Student();

s4.setName("Yara");

s4.setId(20180366);

s4.setGpa(1.5);

s3.study();

s3.DoAssignment(false); }}
Problem – 2 (Fruits)

Write a Java program to create a class called Fruits as following with


building your empty constructor & another constructor where you can pass
every student data when creating an object.

Then create 4 objects in main class


• Apple

• Banana

• Melon

• Orange

Fruits

String name

String color

Int weight

Int price

Setters & getters

Void PrintFruitsData() {print name+color+weight+price}

package problem2.object.orianted.programming;

public class Fruits {


private String name;

private String color;

private int weight;

private int price;

public Fruits(String name, String color, int weight, int price) {

this.name = name;

this.color = color;

this.weight = weight;

this.price = price;

public Fruits() {

name = "";

color = "";

weight=0;

price=0;

public String getName() {

return name;

public void setName(String name) {


this.name = name;

public String getColor() {

return color;

public void setColor(String color) {

this.color = color;

public int getWeight() {

return weight;

public void setWeight(int weight) {

this.weight = weight;

public int getPrice() {

return price;

public void setPrice(int price) {

this.price = price;
}

public void PrintFruitsData()

System.out.println("Fruit Name is "+name+" Fruit color "+color+" Fruit weight is "+weight+" Fruit
price is "+price);

package problem2.object.orianted.programming;

public class Problem2ObjectOriantedProgramming {

public static void main(String[] args) {

Fruits apple = new Fruits("apple","Red",10,2);

Fruits Banana = new Fruits("Banana","Yellow",6,1);

Fruits Melon = new Fruits("Melon","Green",30,5);

Fruits Orange = new Fruits();

Orange.setName("orange");

Orange.setColor("orange");

Orange.setWeight(12);

Orange.setPrice(10);

apple.PrintFruitsData();
Banana.PrintFruitsData();

Melon.PrintFruitsData();

Orange.PrintFruitsData();

}
Problem – 3 (Shapes)

Write a Java program to create a class called Shape as following with


building your empty constructor & another constructor where you can pass
every student data when creating an object.

Then create 2 objects in main class & call draw() method for each object
• Circle

• Square

Shape

String name

String color

Setters & getters

Void draw(){ print “drawing the shape name & color”}

Void erase() {print “erase shape”}

package shapes;

public class shape {

private String name;

private String color;


public void Area()

public shape() {

name="";

color="";

public shape(String name, String color) {

this.name = name;

this.color = color;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public String getColor() {

return color;

}
public void setColor(String color) {

this.color = color;

public void Draw()

System.out.println("Drawing a " +color + " "+ name);

public void erase()

System.out.println("Erasing a " + name);

..

package shapes;

public class Shapes {

public static void main(String[] args) {

shape circle = new shape("Circle","Blue");

shape square = new shape("Square","Red");

circle.Draw();

square.Draw();

}
}

You might also like