.lT-Mobile 4:39 PM https://elearning.utdallas.edu 28 Homework 3 Problem 1: PARTI
ID: 3912853 • Letter: #
Question
.lT-Mobile 4:39 PM https://elearning.utdallas.edu 28 Homework 3 Problem 1: PARTI Create an abstract Java class named "TwoDFigure" in a package named "GEOFIGURES. This class has 3 attributes: (1) dimension: a constant, public, of type byte, and set to value of 2 (2) shape: protected, of type String (e.g.: circle, square, etc.) (3) length unit: protected, of type String (e.g.: cm, m, ft, in) Class TwoDFigure declaration provides a default constructor, get- methods and set-methods for the variable attributes (shape and length unit), an abstract method to calculate perimeter (calculatePerimeter), an abstract method to calculate area (calculate Area)), and another abstract method (displayFigureData0). PART II Create a Java class named "Circle" in the same package, i.e GEOFIGURES, that extends the above class TwoDFigure. Class Circle has 1 attribute: radius that should be defined as private Class Circle declaration provides: . a default constructor another constructor that accepts radius as a parameter, . get-method and set-method for the attribute radius, a method to get the diameter a method to calculate perimeter (calculatePerimeter)) that TwoDFigureand returns the value of perimeter . a method to calculate area (calculateArea)) that overrides the returns the value of area a method (displayFigureData)) that overrides the method of and prints out information to indicate that the figure is a circle and value of its radius PART III
Explanation / Answer
import java.util.*;
//Package GEOFIGURES;
abstract class TwoDFigure // abstract base class
{
public static final byte dimension = 2;
protected String shape;
protected String lengthUnit;
// set and get methods
public void setShape(String shape)
{
this.shape = shape;
}
public String getShape()
{
return shape;
}
public void setLengthUnit(String lengthUnit)
{
this.lengthUnit = lengthUnit;
}
public String getUnitLength()
{
return lengthUnit;
}
public TwoDFigure()// default constructor
{
shape = " ";
lengthUnit = " ";
}
//abstract methods
abstract public double calculateArea();
abstract public double calculatePerimeter();
abstract public void displayFigureData();
}
class Rectangle extends TwoDFigure //derived class
{
private double length,width;
public Rectangle(String lengthUnit,double length, double width)//constructor
{
setLengthUnit(lengthUnit);
setShape("rectangle");
this.length = length;
this.width = width;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
//override base class abstract methods
public double calculateArea()
{
return length*width;
}
public double calculatePerimeter()
{
return 2*(length+width);
}
public void displayFigureData()
{
System.out.println("Figure is a "+getShape()+" Length : "+length+ " Width : "+width);
}
}
class Circle extends TwoDFigure
{
private double radius;
public Circle(String lengthUnit,double radius)
{
setLengthUnit(lengthUnit);
setShape("circle");
setLengthUnit("cm");
this.radius = radius;
}
public double getRadius()
{
return radius;
}
public double getDiameter()
{
return 2*radius;
}
public double calculateArea()
{
return 3.14*radius*radius;
}
public double calculatePerimeter()
{
return 2*3.14*radius;
}
public void displayFigureData()
{
System.out.println("Figure is a "+getShape()+" Radius : "+radius);
}
}
class FigureCalculator
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the code for the figure <1-circle , 2- rectangle>");
int code = input.nextInt();
if(code == 1)
{
System.out.println("Enter the radius of the circle : ");
double radius = input.nextDouble();
System.out.println("Enter the measurement unit : ");
String unit = input.next();
Circle c = new Circle(unit,radius);
c.displayFigureData();
System.out.println("Circle Area : "+c.calculateArea()+" Perimeter: "+c.calculatePerimeter());
}
else if(code == 2)
{
System.out.println("Enter the length and width of rectangle : ");
double length = input.nextDouble();
double width = input.nextDouble();
System.out.println("Enter the measurement unit : ");
String unit = input.next();
Rectangle r = new Rectangle(unit,length,width);
r.displayFigureData();
System.out.println("Rectangle Area : "+r.calculateArea()+" Perimeter: "+r.calculatePerimeter());
}
else
System.out.println("Invalid code");
}
}
Output:
Enter the code for the figure <1-circle , 2- rectangle>1
Enter the radius of the circle :3.4
Enter the measurement unit :cm
Figure is a circle Radius : 3.4
Circle Area : 36.2984 Perimeter: 21.352
Do ask if any doubt. Please upvote.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.