0% found this document useful (0 votes)
55 views24 pages

Esc101: Fundamental of Computing: Object Oriented Programming

This document discusses object-oriented programming concepts in Java including method overloading, constructors, and building complex classes using a Point class as a basis. Key concepts covered include using method overloading to define multiple methods with the same name but different parameters, how constructors are used to initialize objects, and examples of defining Triangle and Circle classes that compose Point objects. Complex problems like finding the area of a triangle or checking if two circles intersect are demonstrated being solved more cleanly using an object-oriented approach versus a procedural approach.

Uploaded by

prabhure
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views24 pages

Esc101: Fundamental of Computing: Object Oriented Programming

This document discusses object-oriented programming concepts in Java including method overloading, constructors, and building complex classes using a Point class as a basis. Key concepts covered include using method overloading to define multiple methods with the same name but different parameters, how constructors are used to initialize objects, and examples of defining Triangle and Circle classes that compose Point objects. Complex problems like finding the area of a triangle or checking if two circles intersect are demonstrated being solved more cleanly using an object-oriented approach versus a procedural approach.

Uploaded by

prabhure
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

ESc101 : Fundamental of Computing

I Semester 2008-09

Lecture 22

Object Oriented programming

Method Overloading in Java. Constructor Building complex classes using Point class
Circle, Triangle,...

Method overloading in JAVA

Question : In a class, can there be two methods with the same name?

Yes, but they should differ in Types of Number of parameters

Method overloading in JAVA

Question : In a class, can there be two methods with the same name?

Yes, but they should differ in the their signatures

Signature of method

return type method name (type1, type2, ...) For example,

public static int absolute(int i) has signature


int absolute(int)

public static double absolute(int i, double d) has signature


double absolute (int, double) Note : The order matters in the parameter list.

Constructor

Constructor : a method with the same name as that of the class.

Each class has a default constructor without parameters. For example, Point()
constructor for class Point

It has No return type. When is it executed ? : Immediately after the attributes of the newly created
object have been initialized their (default) initial values.

There may be more than one constructor (due to availability of method


overloading)

Example

public class Point { double x; double y; Point(double x1, double y1) { x = x1; y = y1; } public double distance_from_origin() { return(Math.sqrt(x*x + y*y)); } }

Constructor

NOTE : The default constructor is no more available once we dene one or more constructors. So we may have to dene the default constructor explicitly if we need it.

Example

public class Point { double x; double y; Point(double x1, double y1) { x = x1; y = y1; } Point() { x=0; y=0; }

public double distance_from_origin() { return(Math.sqrt(x*x + y*y)); } }


8

One more method for class Point

we want to add one more method to the class to compute distance of the point from another point.

public double distance from point(???????)

The implementation is given in Point.java le

One more method for class Point

we want to add one more method to the class to compute distance of the point from another point.

public double distance from point(???????)

The implementation is given in Point.java le

10

One more method for class Point

we want to add one more method to the class to compute distance of the point from another point.

public double distance from point(Point Q)

The implementation is given in Point.java le

11

Forming complex data types using Point class


We can use Points to dene classes for 1. Triangle 2. Circle 3. Polygon 4. Line segment 5. Tetrahedron

12

Example 1 : class of Triangle

13

Class of triangle : overview

public class Triangle { Point P; Point Q; Point R; Triangle(Point A, Point B, Point C) // constructor Triangle(double x1, double y1,..., double y3) // constructor public double perimeter() //perimeter of triangle public void translate(double x_diff, y_diff) }
14

Class of triangle : actual implementation

Given in le Triangle.java

15

Example 2 : class of Circle

16

Class of Circle : overview

public class Circle { Point center; double radius; Circle(Point P, double r) //Constructor Circle() //Constructor public double perimeter() // method for computing perimeter public void translate() // method fo translating circle }
17

Class of Circle : actual implementation

Given in le Circle.java

18

Two problems to be solved


1. Given the coordinates of vertices of triangle, nd the area of the triangle. 2. Given two circles with their center and radius, determine if they intersect.

19

Solution of problems using OOP


Problem1 : Given the coordinates of vertices of triangle, nd the area of the triangle. Given in triangle program1.java le

Problem2 : Given two circles with their center and radius, determine if they Given in circle program1.java le

20

The solution based on structured programming

Adhoc Difcult to read repetition of methods


For examples, for computing distance between points

21

The solution based on structured programming

Adhoc Difcult to read repetition of methods


For examples, for computing distance between points

22

The solution based on Object Oriented Programming

Easier to code Easier to understand avoids repetitions of code

The code for computing distance between points is available from class Point itself, so no need to design it again in the two programs

23

Home work :

Include the methods Area() of triangle program1.java in the class Triangle


itself. Rewrite program for problem 1 based on the modied Triangle class

Include the method Intersect circles() of circle program1.java in the class


Circle itself. Rewrite program for problem 1 based on the modied Circle class

24

You might also like