Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICS 201 Introduction to Computer Science Problem Solving #4.

Similar presentations


Presentation on theme: "ICS 201 Introduction to Computer Science Problem Solving #4."— Presentation transcript:

1 ICS 201 Introduction to Computer Science Problem Solving #4

2 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.

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

4 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

5 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() ; }

6 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.

7 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; }

8 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.

9 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; }

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

11 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()) ; }


Download ppt "ICS 201 Introduction to Computer Science Problem Solving #4."

Similar presentations


Ads by Google