ICS 201 Introduction to Computer Science Problem Solving #4.

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
OOP: Inheritance By: Lamiaa Said.
Problem Solving 2 Abstract Classes, Interfaces and Exception Handling
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Huron High School AP Computer Science Instructor: Kevin Behemer S. Teacher: Guillermo Moreno.
Inheritance COMP53 Sept Inheritance Inheritance allows us to define new classes by extending existing classes. A child class inherits all members.
1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.
Compunet Corporation1 Programming with Visual Basic.NET Creating and Using Classes Lecture # 8 Tariq Ibn Aziz.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
Review 4 View classes as modules Encapsulate operations Functions are static methods 4 View classes as struct types Encapsulate data 4 View classes as.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1.4: Rewrite Formulas & Equations
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Internet Software Development Classes and Inheritance Paul J Krause.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Introduction to Computation and Problem Solving Class 9: Static Methods and Data Members Prof. Steven R. Lerman and Dr. V. Judson Harward.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
1 Classes and Objects in Java Basics of Classes in Java.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
ICS 201 Introduction to Computer Science
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
1.8: Perimeter, Circumference, and Area
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
1 Object state: fields. Fields field: A variable inside an object that represents part of its internal state.  Each object will have its own copy of.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Objects and Classes Mostafa Abdallah
CS 21b Intro to Computing II. Object-Oriented Programming: Classes in Java.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
CS 884 (Prasad)Java Names1 Names Scope and Visibility Access Control Packages.
Data Structures in Scheme Building data structures: Java class Circle { private double center_x, center_y, radius; Circle(double x, double y, double r)
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
1 / 41 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 5 Programming Fundamentals using Java 1.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
INHERITANCE -This feature allows one class to specialize the state and behavior of another class. -Significant code reuse can be achieved by this technique.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
Then/Now You wrote equations of lines using information about their graphs. Write the equation of a circle. Graph a circle on the coordinate plane.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Interface.
CS250 Introduction to Computer Science II
Class vs Abstract vs Interface
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
Building Java Programs
Chapter 14 Abstract Classes and Interfaces
Presentation transcript:

ICS 201 Introduction to Computer Science Problem Solving #4

Example 1 Write a program that illustrates how to declare interfaces and implement these in various classes. Declare an interface Shape2D that declares a getArea( ) method that calculates and returns the area of an enclosed 2D shape. Declare an interface Shape3D that declares a getVolume( ) method that calculates and returns the volume of an enclosed 3D shape.

interface Shape2D { double getArea() ; } interface Shape3D { double getVolume() ; }

Example 1 Cont. Declare a class Point3D that contains: The three coordinates of a point A constructor to initialize the field of the class Declare an abstract class Shape that declares an abstract display( ) method that prints the name of the class

class Point3D { double x,y,z ; Point3D (double ax, double ay, double az) { this.x = ax ; this.y = ay ; this.z = az ; } abstract class Shape { abstract void display() ; }

Declare a class Circle that extends Shape class and implements Shape2D interface. This class contains: Two variables of type Point3D: center and p A constructor with two arguments of type Point3D: the first represents the center of the circle and the second represents any point on the circumference of the circle. An overridden method display( ). Hint: the Area of the circle is calculated by this formula: Area =  * r 2, where with c(x 1, y 1 ) represents the center of the circle and p(x 2, y 2 ) represents any point on the circumference of the circle. Example 1 Cont.

class Circle extends Shape implements Shape2D { Point3D center, p ; // p is any point on the circomference Circle (Point3D C, Point3D P){ this.center =C ; this.p = P ; } public void display(){ System.out.println("Circle"); } public double getArea (){ double dx = center.x - p.x ; double dy = center.y -p.y ; double d = dx*dx +dy*dy ; double radius = Math.sqrt(d) ; return Math.PI *radius*radius; }

Declare a class Sphere that extends Shape class and implements Shape3D interface. This class contains: One double variable radius that represents the radius of the sphere One variable center of type Point3D that represents the center of the sphere A constructor with two arguments: the first represents the center of the sphere and the second represents the radius of the sphere. An overridden method display( ). Hint: the volume of the sphere is calculated by this formula:, where r is the radius of the sphere.

class Sphere extends Shape implements Shape3D { Point3D center ; // p is any point on the circomference double radius ; Sphere (Point3D C, double r){ this.center =C ; this.radius = r ; } public void display(){ System.out.println("Sphere"); } public double getVolume(){ return 4*Math.PI *radius*radius*radius/3; }

Instantiate the two classes Circle and sphere and invoke all their methods.

class Shapes{ public static void main(String[] ar){ Circle C= new Circle(new Point3D(1,1,0), new Point3D(1,0,0)); C.display(); System.out.println(C.getArea()) ; Sphere S= new Sphere(new Point3D(0,0,0), 1); S.display(); System.out.println(S.getVolume()) ; }