0% found this document useful (0 votes)
21 views3 pages

TP 4 Java

The document defines an abstract Shape class with methods to get/set height and width. Rectangle and Triangle classes extend Shape and override methods to calculate and display their specific areas. A Test class uses a Scanner to input shape details, stores Shape objects in an array, and calls shape methods to output details.

Uploaded by

imen gueddess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

TP 4 Java

The document defines an abstract Shape class with methods to get/set height and width. Rectangle and Triangle classes extend Shape and override methods to calculate and display their specific areas. A Test class uses a Scanner to input shape details, stores Shape objects in an array, and calls shape methods to output details.

Uploaded by

imen gueddess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package heritage;

public abstract class shape {


protected double height;
protected double width;

public double getHeight() {


return height;
}
public void setHeight(double height) {
[Link] = height;
}
public double getwidth() {
return width;
}
public void setwidth(double width) {
[Link] = width;
}

public shape(double h , double w){}

public void showshape(){


[Link]("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 ([Link] * [Link] ) ;
}
public void display(){
[Link]("the area of the rectangle is : "+[Link]());
}
}
-----------------------------------------------------------------------------------
--------
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) {
[Link] = base;
}
public double getbase() {
return base;
}
public void showshape(){
[Link]("width " +getbase() + " height "+ getHeight());
}

public double getArea(){


return( ([Link] * [Link] )/2) ;
}
public void display(){
[Link]("the area of the triangle is : "+[Link]());
}
}
-----------------------------------------------------------------------------------
--------
package heritage;

import [Link];

public class test {


public static void main(String []args){
Scanner scanner;
scanner=new Scanner([Link]);
/*
[Link]("------------------
RECTANGLE------------------------");

[Link]("hight :");
double h=[Link]() ;
[Link]("width :");
double w= [Link] () ;
rectangle objr = new rectangle(h,w);
[Link](h);
[Link](w);
[Link]();
[Link]();

[Link]("---------------TRIANGLE------------------------");

[Link]("hight :");
double h1=[Link]() ;
[Link]("base :");
double b= [Link] () ;
triangle objt = new triangle(h1,b);
[Link](h1, b);
[Link]();
[Link]();*/

[Link]("-----------------TABLEAU------------------------");

[Link]("nombre de shape :");


int n=[Link]() ;
shape[] sh = new shape[n] ;
for (int i =0; i<n;i++){
[Link]("que voulez vous entrer triangle(1) ou bien
rectangle (2) ");
int ch=[Link]() ;
if (ch==1){
[Link]("hight :");
double h1=[Link]() ;
[Link]("base :");
double b= [Link] () ;
triangle objt = new triangle(h1,b);
[Link](h1, b);
sh[i]=objt;
sh[i].showshape();
}
else{
[Link]("hight :");
double h=[Link]() ;
[Link]("width :");
double w= [Link] () ;
rectangle objr = new rectangle(h,w);
[Link](h);
[Link](w);
sh[i]=objr;
sh[i].showshape();
}
}
for (shape s : sh){
[Link]([Link]().getName());
if (s instanceof rectangle ){
[Link]();
[Link]();
}
}

You might also like