introduction to oop with java - abu khleif · 2017-09-30 · introduction to oop with java...

53
Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 www.abukhleif.com

Upload: others

Post on 11-Aug-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Introduction to OOP with Java

Instructor: AbuKhleif, Mohammad Noor

Sep 2017

www.abukhleif.com

Page 2: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Inheritance and Polymorphism – Part 1Instructor: AbuKhleif, Mohammad Noor

Sep 2017

www.abukhleif.com

Lecture 11:

Page 3: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Instructor

• AbuKhleif, ‘Mohammad Noor’• Computer Engineer (JU 2012-2017)• Software Automation Engineer @ Atypon – John Wiley and

Sons Company - Jordan Branch

• Reach me at:• www.abukhleif.com• [email protected]• facebook.com/moh.noor94• twitter.com/moh_noor94

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

3

Page 4: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Course

• Java SE Basics• Object Oriented Programming• Course Page:

www.abukhleif.com/courses/java-101-sep-2017• Or, go to: www.abukhleif.com Courses Java 101 Course

– Sep 2017• Course Facebook Group:

www.facebook.com/groups/AKF2017Java

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

4

Page 5: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Motivations

• Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features.

• What is the best way to design these classes so to avoid redundancy?

• The answer is to use inheritance.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

5

Page 6: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Let’s Start!

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

6

Page 7: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

What is Inheritance?

• Classes are used to model objects of the same type.

• Different classes may have some common properties and behaviors.

• Inheritance allows you to:• Define a generalized class that includes the common properties

and behavior.

• Define specialized classes that extend the generalized class.

• Inherit the properties and methods from the general class.

• Add new properties and methods.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

7

Page 8: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Super classes and Sub classes

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

8

• A class (A) that is extended from another class (B) is called a subclass. (B) is called a superclass.

• A subclass:• Inherits accessible data fields and methods from its superclass.

• May also add new data fields and methods.

Page 9: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Example

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com 9

GeometricObject

-color: String

-filled: boolean

-dateCreated: java.util.Date

+GeometricObject()

+GeometricObject(color: String,

filled: boolean)

+getColor(): String

+setColor(color: String): void

+isFilled(): boolean

+setFilled(filled: boolean): void

+getDateCreated(): java.util.Date

+toString(): String

The color of the object (default: white).

Indicates whether the object is filled with a color (default: false).

The date when the object was created.

Creates a GeometricObject.

Creates a GeometricObject with the specified color and filled

values.

Returns the color.

Sets a new color.

Returns the filled property.

Sets a new filled property.

Returns the dateCreated.

Returns a string representation of this object.

Circle

-radius: double

+Circle()

+Circle(radius: double)

+Circle(radius: double, color: String,

filled: boolean)

+getRadius(): double

+setRadius(radius: double): void

+getArea(): double

+getPerimeter(): double

+getDiameter(): double

+printCircle(): void

Rectangle

-width: double

-height: double

+Rectangle()

+Rectangle(width: double, height: double)

+Rectangle(width: double, height: double

color: String, filled: boolean)

+getWidth(): double

+setWidth(width: double): void

+getHeight(): double

+setHeight(height: double): void

+getArea(): double

+getPerimeter(): double

Page 10: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Example

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com 10

public class GeometricObject {

private String color = "White";

private boolean filled;

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public boolean isFilled() {

return filled;

}

public void setFilled(boolean filled) {

this.filled = filled;

}

public void printObjectDetails() {

System.out.println("Color is " + color + "Is filled? " + filled);

}

}

Page 11: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Example

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com 11

public class Circle extends GeometricObject {

private double radius = 1;

public double getRadius() {

return radius;

}

public void setRadius(double radius) {

this.radius = radius;

}

public double getArea() {

return radius * radius * Math.PI;

}

public double getPerimeter() {

return 2 * radius * Math.PI;

}

public void printCircleDetails() {

System.out.println("Circle color is " + getColor() + " , circle

filled ? "+ isFilled()+ ", circle radius is"+ radius);

}

}

Page 12: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Example

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com 12

public class Rectangle extends GeometricObject {

private double width = 1;

private double height = 1;

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

public double getArea() {

return width * height;

}

public double getPerimeter() {

return 2 * (width + height);

}

public void printRectangleDetails() {

System.out.println("Rectangle color is " + getColor() + " , rectangle filled?

"+ isFilled()+ ", rectangle width is"+ width + ", rectangle height is" +

height);

}

}

Page 13: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Example

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com 13

public class TestCircleRectangle {

public static void main(String[] args) {

Circle c = new Circle();

c.printObjectDetails();

c.setColor("Black");

c.setFilled(true);

c.setRadius(5);

c.printCircleDetails();

Rectangle r = new Rectangle();

r.setColor("Red");

r.setFilled(true);

r.setWidth(3);

r.setHeight(5);

r.printRectangleDetails();

}

}

These methods are inherited from the GeometricObject class.

Page 14: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Important Notes

• Contrary to conventional interpretation, a subclass is not a subset of its superclass.• In fact, a subclass usually contains more information and methods

than its superclass.

• Private data fields in a superclass are not accessible outside the class.• They cannot be used directly in a subclass.

• They can only be accessed/mutated through public accessors/mutators if defined in the superclass.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

14

Page 15: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Important Notes

• Inheritance is used to model the is-a relationship.• Do not blindly extend a class just for the sake of reusing methods.

• Some programming languages allow you to derive a subclass from several classes. This capability is called multiple inheritance.• Java does not allow multiple inheritance.

• A Java class may inherit directly from only one class.

• (Multiple inheritance can be achieved through interfaces in Java?)

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

15

Page 16: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

The super Keyword

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

16

Page 17: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

The super Keyword

• The keyword super refers to the superclass and can be used to:• Call a superclass constructor.

• Call a superclass method.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

17

Page 18: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

The super Keyword

• Remember that a constructor is used to construct an instance of a class.

• Unlike properties and methods, the constructors of a superclass are not inherited by a subclass.• They can only be invoked from the constructors of the subclasses

using the keyword super.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

18

Page 19: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

The super Keyword

• The syntax to call a superclass’s constructor is:

super(); // to invoke the no-arg constructor

super(parameters); //to invoke a constructor with parameters

• The statement super() or super(parameters) must appear in the first line of the subclass’s constructor.

• If the keyword super is not explicitly used, the superclass's no-arg constructor is automatically invoked.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

19

Page 20: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

The super Keyword

• The statement super() or super(parameters) must appear in the first line of the subclass’s constructor.

• If the keyword super() is not explicitly used, the superclass's no-arg constructor is automatically invoked.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

20

public A(double d) {

// some statements

}

is equivalent to

public A(double d) {

super();

// some statements

}

public A() {

}

is equivalent to

public A() {

super();

}

Page 21: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

The super KeywordExample

• The following constructor can be added to the Circle class of the previous example:

public Circle (double radius){

super();

this.radius = radius;

}

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

21

Page 22: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Constructor Chaining

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

22

Page 23: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Constructor Chaining

• A constructor may invoke an overloaded constructor (using this) or its superclass constructor (using super).

• If neither is invoked explicitly, the compiler automatically puts super() as the first statement in the constructor.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

23

Page 24: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Constructor Chaining

• In any case, constructing an instance of a class invokes the constructors of all the superclasses along the inheritance hierarchy.• When constructing an object of a subclass, the subclass constructor first

invokes its superclass constructor before performing its own tasks.

• If the superclass is derived from another class, the superclass constructor invokes its parent-class contsructor before performing its tasks.

• This process continues until the last constructor along the inheritance hierarchy is called.

• The process above is known as ‘Constructor Chaining’.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

24

Page 25: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Example

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com 25

public class Faculty extends Employee {

public static void main(String[] args) {

new Faculty();

}

public Faculty() {

System.out.println("(4) Faculty's no-arg constructor is invoked");

}

}

class Employee extends Person {

public Employee() {

this("(2) Invoke Employee’s overloaded constructor");

System.out.println("(3) Employee's no-arg constructor is invoked");

}

public Employee(String s) {

System.out.println(s);

}

}

class Person {

public Person() {

System.out.println("(1) Person's no-arg constructor is invoked");

}

}

Page 26: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Example

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com 26

Output:(1) Person's no-arg constructor is invoked(2) Invoke Employee’s overloaded constructor(3) Employee's no-arg constructor is invoked(4) Faculty's no-arg constructor is invoked

Page 27: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Caution

• If a class is designed to be extended, it is better to provide a no-argconstructor to avoid programming errors.

• Example: this code cannot be compiled:public class Apple extends Fruit {

}

class Fruit {public Fruit(String name) {

System.out.println("Fruit's constructor is invoked");}

}

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

27

The default no-arg constructor of Apple will try to invoke a no-arg constructor of Fruit, which does not exist!

Page 28: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Using the super Keyword to Call a Superclass Method

• The keyword super can be used to reference a method other than the constructor in the superclass.

• The syntax is:super.method(parameters);

• The printCircleDetails method in the Circle class could be rewritten as follows:

public void printCircleDetails(){System.out.println("Circle color is "+ super.getColor() + " , circle filled? "+ super.isFilled() + " , circle radius is "+ radius);}

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

28

Page 29: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Using the super Keyword to Call a Superclass Method

• It is not necessary to put the super keyword before the methods getColor and isFilled in the previous example.• These methods are inherited by the Circle class.• Cases were the super keyword is needed to invoke the superclass

methods will be showed when methods overriding is introduced.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

29

Page 30: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Overriding Methods

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

30

Page 31: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Overriding Methods

• A subclass inherits methods from a superclass.• Sometimes, it is necessary for the subclass to modify the

implementation of a method defined in the superclass.• In the previous example, the method printObjectDetails of the

GeometricObject class can be overridden in the Circle class as follows:

public void printObjectDetails(){super.printObjectDetails();System.out.println("Circle radius = "+radius);}

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

31

Page 32: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Overriding Methods

• An instance method can be overridden only if it is accessible.• Thus, a private method cannot be overridden, because it is not

accessible outside its own class.• If a method defined in a subclass is private in its superclass, the two

methods are completely unrelated.

• Like an instance method, a static method can be inherited. However a static method cannot be overridden.• If a static method defined in the superclass is redefined in a subclass,

the method defined in the superclass is hidden.• The hidden static methods can be invoked using the syntax

SuperClassName.staticMethodName(parameters);

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

32

Page 33: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Overriding vs. Overloading

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

33

Page 34: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Overriding vs. Overloading

• Overloading means to define multiple methods with the same name but different signatures.

• Overriding means to provide a new implementation for a method in the subclass.• The method should be defined in the subclass using the same

signature and the same return type.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

34

Page 35: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Overriding vs. Overloading

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

35

public class Test {

public static void main(String[] args) {

A a = new A();

a.p(10);

a.p(10.0);

}

}

class B {

public void p(double i) {

System.out.println(i * 2);

}

}

class A extends B {

// This method overrides the method in B

public void p(double i) {

System.out.println(i);

}

}

public class Test {

public static void main(String[] args) {

A a = new A();

a.p(10);

a.p(10.0);

}

}

class B {

public void p(double i) {

System.out.println(i * 2);

}

}

class A extends B {

// This method overloads the method in B

public void p(int i) {

System.out.println(i);

}

}

Page 36: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Overriding vs. Overloading

• Overridden methods are in different classes related by inheritance.

• Overloaded methods can be either in the same class or different classes related by inheritance.

• Overridden methods have the same signature and return type.

• Overloaded methods have the same name but a different parameter list.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

36

Page 37: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

@override Annotation

• To avoid mistakes, you can use a special Java syntax, called override annotation:• Place @override before the method in the subclass.

• This annotation denotes that the annotated method is required to override a method in the superclass.• If a method with this annotation does not override its superclass’s method,

the compiler will report an error.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

37

Page 38: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

@override Annotation

• For example, in order to denote that the printObjectDetails is overridden in the Circle class:

@Override

public void printObjectDetails(){

super.printObjectDetails();

System.out.println("Circle radius = "+radius);

}

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

38

Page 39: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

The Object Class and Its Methods

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

39

Page 40: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

The Object Class and Its Methods

• Every class in Java is descended from the java.lang.Objectclass.

• If no inheritance is specified when a class is defined, the superclass of the class is Object.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

40

public class Circle { ...

}

Equivalent public class Circle extends Object {

...

}

Page 41: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

The toString() method in Object

• One of the most important methods provided by the Object class is the method toString().

• The signature of the toString() method is:

public String toString()

• Invoking toString() on an object returns a string that describes the object.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

41

Page 42: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

The toString() method in Object

• By default, it returns a string consisting of a class name of which the object is an instance, and at sign (@), and the object’s memory address in hexadecimal.• For example, the output of the following codeCircle c = new Circle();

System.out.println(c.toString());

is something like:

Circle@780324ff

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

42

Page 43: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

The toString() method in Object

• Usually, we override the toString() method so that it returns a descriptive string representation of the object.

• For example, the toString() method in the Object class can be overridden for the Circle class as follows:

public String toString(){

return "Color is " + getColor() + ". Is filled? " + isFilled() + ". Radius is " + radius + ".";

}

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

43

Page 44: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

The toString() method in Object

• You can also pass an object to invoke the toString() method.

• Example:

System.out.println(object);

System.out.print(object);

• These are equivalent to invoking:

System.out.println(object.toString());

System.out.print(object.toString());

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

44

Page 45: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Let’s Code

Design a class named Triangle that extends GeometricObject. The class contains:

• Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle.

• A no-arg constructor that creates a default triangle.

• A constructor that creates a triangle with the specified side1, side2, and side3.

• The accessor methods for all three data fields.

• A method named getArea() that returns the area of this triangle.

• A method named getPerimeter() that returns the perimeter of this triangle.

• A method named toString() that returns a string description for the triangle.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

45

Page 46: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Let’s Code, cont.

• The formula for computing the area of a triangle is:

𝑠 = (𝑠𝑖𝑑𝑒1 + 𝑠𝑖𝑑𝑒2 + 𝑠𝑖𝑑𝑒3)/2;

𝑎𝑟𝑒𝑎 = 𝑠(𝑠 − 𝑠𝑖𝑑𝑒1)(𝑠 − 𝑠𝑖𝑑𝑒2)(𝑠 − 𝑠𝑖𝑑𝑒3)

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

46

Page 47: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Let’s Code, cont.

• Draw the UML diagrams for the classes Triangle and GeometricObject and implement the classes.

• Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled.• The program should create a Triangle object with

these sides and set the color and filled properties using the input.

• The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

47

Page 48: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Homework

Page 49: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Homework

Part 1- Implement the following classes using Java:

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

49

Page 50: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Homework

Part 2- In your main method:

• Define one object of type Employee, one object of type Faculty, and one object of type Staff.• Object-1 (Employee):

office: 404name: Ahmedsalary: 500

• Object-2 (Faculty):office: 310name: Samersalary: 750 rank: Professor

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

50

Page 51: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Homework

Part 2- In your main method:• Object-3 (Staff):

office: 205name: Salmasalary: 450rank: Secretary

• Print the details of each object by invoking the toString() method.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

51

Page 52: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Homework Submission

• Submit only the .java files.

• Upload your file to the Facebook group.

• Submission due: Thursday, Oct 5 - 08:00 PM

• Late submission will not be reviewed by the instructor.

• Public solutions upload goal is to share knowledge, you can see other’s solutions, but, please, don’t cheat yourself!

• Don’t forget, your solution should be well-documented, well-designed, and well-styled.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

52

Page 53: Introduction to OOP with Java - Abu Khleif · 2017-09-30 · Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

- Eng. Asma Abdel Karim Computer Engineering Department, JU Slides and Sheets.- Liang, Introduction to Java Programming 10/e

Instructor: AbuKhleif, Mohammad Noor

Sep 2017

www.abukhleif.com

References:

End of Lecture =D