1. Create an Inteface called Shapes with one method: public int getArea() 2. Cre
ID: 3714300 • Letter: 1
Question
1. Create an Inteface called Shapes with one method:
public int getArea()
2. Create a Rectangle class that implements the Shapes interface.
It will have a data members length and width and area as integers
It will have an argument constructor to pass length and width
It will have getLength()//returns length
It will have getWidth()//returns width
It will have getArea()//will assign area to be length * width then returns area
3. Create a Square class that implements the Shapes Interface
It will have as data members side and area as integers
It will have an argument constructor to pass side
It will have getSide()//returns side
It will have getArea()// assigns area to be side*side and then returns area
4. Create a shapesDemo class
In the main method create 2 objects one for Rectangle r1 and one for Square s1.
For the rectangle pass 10 and 15, and for the square pass 4
Using the getLength() and getWidth() method calls display the dimensions for the Rectangle
Using the showArea() display rectangle's area
Using the getSide() method call display the side of the square
Using the showArea() dispaly the square's area
**Create the showArea definition as a static method that passes the Shapes Inteface
Output is in form
Area: calcualtedArea
AFter finishing the above create similar classes for
Triangle which has base height and area and methods getBase(), getHeight()
and getArea()
int area=(base*height)/2;
return area;
Circle which has radius and area and getRadius() and
getArea()
int area=(int)( 3.14*radius*radius);
return area;
In the main create t1 from Triangle to pass through 10 and 5 and c1 from Circle to pass through 10
Using the getBase() and getHeight() display triangle's base and height
Use show showArea() to display triangle's area
Using the getRadius() display circle's radius
Using the showArea() display circle's area
Explanation / Answer
public class ShapesDemo {
public static void main(String args[]) {
Rectangle R1 = new Rectangle(15,10);
Square S1 = new Square(4);
System.out.println("The dimention of the rectange is Length: " + R1.getLenght() + " WIDHT: " + R1.getWidth());
showArea( R1.getArea());
System.out.println("The side of the Square is " + S1.getSide());
showArea(S1.getArea());
Triangle T1 = new Triangle(10,5);
System.out.println("The Height and Base of the triange is " + T1.getHeight() + " and " + T1.getBase());
showArea(T1.getArea());
Circle C1 = new Circle(10);
System.out.println("The radius of the circle is " + C1.getRadius());
showArea(C1.getArea());
}
public static void showArea(int area){
System.out.println("Area: " + area);
}
}
public class Circle implements Shapes{
int radius;
int area;
public Circle(int i) {
// TODO Auto-generated constructor stub
radius = i;
}
@Override
public int getArea() {
// TODO Auto-generated method stub
area = (int) (3.14 * radius * radius);
return area;
}
/**
* @return the radius
*/
public int getRadius() {
return radius;
}
}
public class Triangle implements Shapes{
int base;
int height;
int area;
/**
* @param base
* @param height
*/
public Triangle(int base, int height) {
super();
this.base = base;
this.height = height;
}
@Override
public int getArea() {
// TODO Auto-generated method stub
area = (base * height) / 2;
return area;
}
/**
* @return the base
*/
public int getBase() {
return base;
}
/**
* @return the height
*/
public int getHeight() {
return height;
}
}
public class Square implements Shapes{
int side;
int area;
@Override
public int getArea() {
// TODO Auto-generated method stub
area = side * side;
return area;
}
/**
* @param side
*/
public Square(int side) {
super();
this.side = side;
}
/**
* @return the side
*/
public int getSide() {
return side;
}
}
public class Rectangle implements Shapes{
int length;
int width;
int area;
@Override
public int getArea() {
// TODO Auto-generated method stub
area = length * width;
return area;
}
/**
* @param lenght
* @param width
* @param area
*/
public Rectangle(int lenght, int width) {
super();
this.length = lenght;
this.width = width;
}
/**
* @return the lenght
*/
public int getLenght() {
return length;
}
/**
* @return the width
*/
public int getWidth() {
return width;
}
}
public interface Shapes {
public int getArea();
}
Output
The dimention of the rectange is Length: 15 WIDHT: 10
Area: 150
The side of the Square is 4
Area: 16
The Height and Base of the triange is 5 and 10
Area: 25
The radius of the circle is 10
Area: 314
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.