If you were to get these questions on a test and could not use a computer to ans
ID: 3850245 • Letter: I
Question
If you were to get these questions on a test and could not use a computer to answer this question, only pen and paper.
How would you answer them? Please explain in detail and with step-by-step on how to approach questions like these :)
The interface Region defines a two-dimensional area. The classes circle (defines a circle) and Rectangle (defines a rectangle) implements this interface interface Region perimeter return S the region's perimeter e double perimeter area returres the region's area double area class Circle implements Region the radius of the circle private double radius public Circle (double radius) this radius radius code is missing here class Rectangle implements Region the lengths of the sides of the rectangle private double length; private double width public Rectangle (double length, double width) this length length this width width; code is missing here a Make the classes Circle and Rectangle complete: write the missing code. b) A static method selectRectangles accepts an array of areas of type Region, and returns an array that only contains the areas that are of type Rectangle. Create that method. c) Create an array that contains both circles (objects of type Circle) and rectangles (objects of type Rectangle). Write code that determines and shows the perimeter and area of these regions. Call the method selectRectangles with the created array as argument.Explanation / Answer
//I have modified the screen shot code.
//below code is the final code.
interface Region
{
double area();
double perimeter();
}
class Rectangle implements Region
{
private double length;
private double width;
public Rectangle(double length,double width)
{
this.length=length;
this.width=width;
}
public double area()
{
return length*width;
}
public double perimeter()
{
return 2*(length+width);
}
}
class Circle implements Region
{
private double radius;
public Circle(double radius)
{
this.radius=radius;
}
public double area()
{
return 2*3.14*radius*radius;
}
public double perimeter()
{
return 2*3.14*radius;
}
}
public class QuestionAnswer
{
public static Region[] selectRectangles(Region area[])
{
area=new Rectangle[2];
return area;
}
public static void main(String args[])
{
Circle circle[]=new Circle[2];
Rectangle rectangle[]=new Rectangle[2];
circle[0]= new Circle(2.6);
circle[1]= new Circle(4.5);
rectangle[0]=new Rectangle(2.9,6.7);
rectangle[1]=new Rectangle(12.5,14.2);
System.out.println("Circle area 1 "+circle[0].area()+" Circle perimeter 1 "+circle[0].perimeter());
System.out.println("Circle area 2 "+circle[1].area()+" Circle perimeter 2 "+circle[1].perimeter());
System.out.println("Rectangle area 1 "+rectangle[0].area()+" Rectangle perimeter 1 "+rectangle[0].perimeter());
System.out.println("Rectangle area 2 "+rectangle[1].area()+" Rectangle perimeter 2 "+rectangle[1].perimeter());
System.out.println(selectRectangles(rectangle));
}
}