1. (Use Java or C#) Create an interface iArea. This shape has the method double
ID: 3875721 • Letter: 1
Question
1. (Use Java or C#) Create an interface iArea. This shape has the method double getArea().
Use this interface in class Rectangle, Circle, Triangle. Implement getArea() to return area of each of those shapes. Make sure you add the required coordinates and other needed methods. Also override the toString (if using java) so it prints [Type of Object="", Name="", Area=, Coordinates=[ ]) with the proper information inside.
Create a main that test those classes
2.
(Use Java or C#)
Create a class Person. Describe basic properties of a person and methods. For example, color of eyes, name, height, age, ... person walks, runs, ..
Create a Student that inherits Person. The student has PantherID, major, GPA.
Explanation / Answer
// INTERFACE with a method declaration
interface iArea{
public double getArea();
}
// Rectangle class implementing interface iArea
class Rectangle implements iArea {
int length, width;
public double getArea()
{
return length*width;
}
public String toString()
{
return "Type of Object = Rectangle, Name = rectangle, Area = " + getArea() + ", Coordinates = [" + length + ", " + width + "]";
}
}
// Circle class implementing interface iArea
class Circle implements iArea{
int radius;
public double getArea()
{
return 3.14*radius*radius;
}
public String toString()
{
return "Type of Object = Circle, Name = circle, Area = " + getArea() + ", Coordinates = [" + radius + "]";
}
}
// Triangle class implementing interface iArea
class Triangle implements iArea{
int base, height;
public double getArea()
{
return base*height/2.0;
}
public String toString()
{
return "Type of Object = Triangle, Name = triangle, Area = " + getArea() + ", Coordinates = [" + base + ", " + height + "]";
}
}
// sample run, calling toString for each of the objects and printing output
class Main{
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.length = 2;
rectangle.width = 3;
System.out.println(rectangle.toString());
Circle circle = new Circle();
circle.radius = 4;
System.out.println(circle.toString());
Triangle triangle = new Triangle();
triangle.base = 2;
triangle.height = 3;
System.out.println(triangle.toString());
}
}
/*SAMPLE OUTPUT
Type of Object = Rectangle, Name = rectangle, Area = 6.0, Coordinates = [2, 3]
Type of Object = Circle, Name = circle, Area = 50.24, Coordinates = [4]
Type of Object = Triangle, Name = triangle, Area = 3.0, Coordinates = [2, 3]
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.