160 likes | 173 Views
Problem 1. Write a program for computing the perimeter of a circumference according to the following dialog: radius?__ perimeter=N Solution 1. ad-hoc import java.util .*; class Program { static public void main(String[] args ) { Scanner U = new Scanner( System. in );
E N D
Problem 1. Write a program for computing the perimeter of a circumference according to the following dialog: • radius?__ • perimeter=N • Solution 1. ad-hoc • import java.util.*; • class Program{ • static public void main(String[]args) { • Scanner U = new Scanner(System.in); • System.out.println("radius ?"); • double r = U.nextDouble(); • System.out.print("perimeter=" + 2 * Math.PI * r); } • }
Solution 2. With a class for circumference class Circumference{ private double r; public Circumference(double x){ r=x; } public double perimeter(){ return 2*Math.PI*r; } } class Program{ static public void main(String[]args){ Scanner U = new Scanner(System.in); System.out.println("radius ? "); double r=U.nextDouble(); Circumference c=new Circumference(r)); System.out.print("perimeter="+c.perimeter()); } }
Problem 2. Calculate the area and the perimeter of a circle • Solution 1. Modify class Circumference, add method area • No way ! Circumferences have no area • Solution 2. write and use a new class Circle which: • Extends class circumference with method area • Inherits other components (data and methods). • System.out.print("radius ? "); • Double r = U.nextDouble(); • Circle c=new Circle(r); • //use method from class Circle • System.out.println("area="+c.area()); • //use inherited method from Circumference • System.out.println("perimeter="+c.perimeter());
//base or superior class class Circumference{ protected double r; //visible for extensions public Circumference(double x){ r=x; } public double perimeter(){ return 2*Math.PI*r; } } //extended or derived class class Circle extends Circumference{ public double area(){ return Math.PI*r*r; } public Circle(double x){//constructor super(x);//constructor of super class } }
Problem 3. compute the area and perimeter of a circle or a square Circle(1) or Square(2)? __ radius?__ area=N perimeter=N Program: int n=U.readInt(); if(n==1){ Circle c=new Circle(U.nextDouble()); System.out.println("area="+c.area()); System.out.println("perimeter="+c.perimeter()); }else if(n==2){ Square c=new Square(U.nextDouble()); System.out.println("area="+c.area()); System.out.println("perimeter="+c.perimeter()); }else System.exit(1);
Solution 1. independent classes class Circle{ protected double r; public Circle(double x){ r=x; } public double area(){return Math.PI*r*r;} public double perimeter(){return 2*Math.PI*r;} } class Square{ protected double a; public Square(double x){ a=x; } public double area(){return a*a;} public double perimeter(){return 4*a;} }
Solution 2. With class hierarchy class Figure{ protected double x; public Figure(double x){ this.x=x; } } class Circle extends Figure{ Public Circle(double x){super(x);} public double area(){return Math.PI*x*x;} public double perimeter(){return 2*Math.PI*x;} } class Square extends Figure{ public Square(double x){super(x);} public double area(){return x*x;} public double perimeter(){return 4*x;} }
Solution 3. With dummy methods (redefined by extensions) class Figure{ protected double x; public Figure(double x){ this.x=x; } public double area(){return 0;} public double perimeter(){return 0;} } This allows a shorter program Figure f; System.out.print("Circle (1) or Square (2) ? "); switch( U.nextInt()){ case 1: System.out.print("radius? "); f=new Circle(U.nextDouble()); break; case 2: System.out.print("side? "); f=new Square(U.nextDouble()); break; } System.out.println("area="+f.area()); System.out.println("perimeter="+f.perimeter());
Solution 4: With abstract class which compels redefine methods • abstract class Figure{ • protected double x; • public Figure(double x){ • this.x=x; • } • abstract public double area(); • abstract public double perimeter(); • } • Notes • This does not allow to create objects of class Figure (new Figure()) • should define at least one abstract method: • abstract header; • Compels extended classes to redefine the abstract methods • We can define other figures
class Rectangle extends Figure{ protected double y; public Rectangle(double x,double y){ super(x); this.y=y; } public double area(){return x*y;} public double perimeter(){return 2*(x+y);} } class Triangle extends Figure{ protected double y,z; public Triangle(double x,double y,double z){ super(x); this.y=y; this.z=z; } public double perimeter(){return x+y+z;} public double area(){ double s=(x+y+z)/2; return Math.sqrt(s*(s-x)*(s-y)*(s-z)); } }
Interfaces • When classes do not share any thing but have to be “seen” as being from the same type then an interface is used • It defines implicitly only abstract methods • No data (variables) • interface Body //a geometrical 3D body • { • public double area(); • public double volume(); • }
class Sphere implements Body { protected double r; public Sphere(double x){r=x;} public double area(){ return 4*Math.PI*r*r; } public double volume(){ return Math.PI*r*r*r*4/3; } } class Cube implements Body { protected double a; public Cube(double x){a=x;} public double area(){return 6*a*a;} public double volume(){return a*a*a;} }
class Box implements Body { protected double l,a,h;//three dimensions public Box(double x,double y,double z){ l=x; a=y; h=z; } public double area(){ return 2*(l*a + l*h + a*h); } public double volume(){ return l*a*h; } } Nota. A Cube can also be implemented based on Box with three equal sides class Cube extends Box { public Cube(double x){super(x,x,x);}
Now we can write Body c; Scanner U = new Scanner(System.in); System.out.print("Cube(1),Sphere(2), Box(3) ? "); Switch (U.nextInt()) { case 1: System.out.print("side ? "); c=new Cube(U.nextDouble()); break; case 2: System.out.print("radius ? "); c=new Sphere(U.nextDouble()); break; case 3: System.out.print("three sides a b c ? "); c=new Box(U.nextDouble(), U.nextDouble(), U.nextDouble()); break; } System.out.println("volume=" + c.volume()); System.out.println("area=" + c.area());