Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

package heritage;

public abstract class shape {


protected double height;
protected double width;

public double getHeight() {


return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getwidth() {
return width;
}
public void setwidth(double width) {
this.width = width;
}

public shape(double h , double w){}

public void showshape(){


System.out.println("width " +getwidth() + " height "+ getHeight());
}
public abstract void display();

}
-----------------------------------------------------------------------------------
----------
package heritage;

public class rectangle extends shape {


public rectangle(double h , double w){
super(h,w);}

public double getArea(){


return (this.height * this.width ) ;
}
public void display(){
System.out.println("the area of the rectangle is : "+this.getArea());
}
}
-----------------------------------------------------------------------------------
--------
package heritage;

public class triangle extends shape {


double base;
public triangle(double h , double b){
super(h,b);}
public void setValue(double h , double b){
setHeight(h);
setbase(b);
}
public void setbase(double base) {
this.base = base;
}
public double getbase() {
return base;
}
public void showshape(){
System.out.println("width " +getbase() + " height "+ getHeight());
}

public double getArea(){


return( (this.height * this.base )/2) ;
}
public void display(){
System.out.println("the area of the triangle is : "+this.getArea());
}
}
-----------------------------------------------------------------------------------
--------
package heritage;

import java.util.Scanner;

public class test {


public static void main(String []args){
Scanner scanner;
scanner=new Scanner(System.in);
/*
System.out.println("------------------
RECTANGLE------------------------");

System.out.println("hight :");
double h=scanner.nextDouble() ;
System.out.println("width :");
double w= scanner.nextDouble () ;
rectangle objr = new rectangle(h,w);
objr.setHeight(h);
objr.setwidth(w);
objr.showshape();
objr.display();

System.out.println("---------------TRIANGLE------------------------");

System.out.println("hight :");
double h1=scanner.nextDouble() ;
System.out.println("base :");
double b= scanner.nextDouble () ;
triangle objt = new triangle(h1,b);
objt.setValue(h1, b);
objt.showshape();
objt.display();*/

System.out.println("-----------------TABLEAU------------------------");

System.out.println("nombre de shape :");


int n=scanner.nextInt() ;
shape[] sh = new shape[n] ;
for (int i =0; i<n;i++){
System.out.println("que voulez vous entrer triangle(1) ou bien
rectangle (2) ");
int ch=scanner.nextInt() ;
if (ch==1){
System.out.println("hight :");
double h1=scanner.nextDouble() ;
System.out.println("base :");
double b= scanner.nextDouble () ;
triangle objt = new triangle(h1,b);
objt.setValue(h1, b);
sh[i]=objt;
sh[i].showshape();
}
else{
System.out.println("hight :");
double h=scanner.nextDouble() ;
System.out.println("width :");
double w= scanner.nextDouble () ;
rectangle objr = new rectangle(h,w);
objr.setHeight(h);
objr.setwidth(w);
sh[i]=objr;
sh[i].showshape();
}
}
for (shape s : sh){
System.out.print(s.getClass().getName());
if (s instanceof rectangle ){
s.showshape();
s.display();
}
}

You might also like