Create aJava GUI program that calculates the area, and perimeter of arectangle o
ID: 3609855 • Letter: C
Question
Create aJava GUI program that calculates the area, and perimeter of arectangle or triangle, or the area and circumference acircle. Your program will read a file that contains 4 spaceseparated fields. The input file name should be passed as acommand line argument.The first field is a string that specifiesthe shape. The remaining three fields are integer fields andare described below.
Shape -String
IntegerField #1
IntegerField #2
IntegerField #3
Circle
Radius
0
0
Rectangle
Length
Width
0
Triangle
Side A
Side B
Side C
Yourprogram should read the first line of the input file and displaythe name of the shape, the three integer fields, the area, and theperimeter or circumference of the shape based on the values of thethree integer fields. There should be a Nextbutton that when pressed will read the next line of the input fileand display the next shape, area, etc. in the input file.
The area of a triangle can be calculated given the three sidesof a triangle (a, b, and c) using the following formula:
Area of Triangle = SQRT(s(s-a)(s-b)(s-c))
where s = perimeter/2.
SampleInput File
Circle 5 0 0
Rectangle 5 6 0
Triangle 4 4 5
End 0 0 0
Program Requirements:
Yourprogram must have a super class named Shape, andthree subclasses (Circle, Triangle, andRectangle) that extend Shape.
Each ofyour three subclasses must have the following value returningmethods: calcArea, equals andtoString. Each of your subclasses shouldhave private data members that store the dimensions of the shape(dimensions are read from the input file). Your Rectangle and Trianglesubclasses must also include a value returning methodcalcPerimeter. The Circleclass must include a calcCircumference method. Your programmust use these subclass methods in order to calculate the area,perimeter and circumference. Your program should NOTinstantiate the Shape class.
Theequals method should take one parameter (object ofit’s class type) and return true if the two objects datamembers are identical, otherwise it should return false.
ThetoString method should return the type of objectand it’s dimensions.
Example: The Circle toString should return a stringcontaining “Circle with a radius equal to 2”.
Shape -String
IntegerField #1
IntegerField #2
IntegerField #3
Circle
Radius
0
0
Rectangle
Length
Width
0
Triangle
Side A
Side B
Side C
Explanation / Answer
public class Shape {
public double getArea() { // subclassto provide actual implementation
System.out.println("Shapeunknown!");
return0.0; // cannot compile without a return statement
}
}
public class Rectangle extends Shape {
private double length; // privatevariables
private double width;
public Rectangle(double length, double width){ // constructor
this.length = length;
this.width = width;
}
public double getPeri() { // override to provideactual implementation
return 2(length + width);
}
public double getArea() { // overrideto provide actual implementation
return length * width;
}
}
public class Triangle extends Shape {
private double a,b,c; // privatevariables
private double peri,s;
public Traingle(double a, double b, double c){ // constructor
this.a = a;
this.b = b;
this.c = c;
}
public double getPeri() { // override to provideactual implementation
peri = a+b+c;
return peri;
}
public double getArea() { // overrideto provide actual implementation
peri = a+b+c;
s = peri / 2 ;
return sqrt(s(s-a)(s-b)(s-c));
}
}
public class Circle extends Shape {
private double radius; // privatevariables
public Circle(double radius) { //constructor
this.radius = radius;
}
public double getCircum() { // override to provideactual implementation
return 2*1.33 * radius ;// 2 * pi * r
}
public double getArea() { // override toprovide actual implementation
return 1.33 * radius * radius; //pi * r * r
}
}
public class TestShape {
public static void main(String[] args) {
Shape s1 = new Rectangle(5.5,6.6);
System.out.println(s1.getPeri()); // run the overriddenversion
System.out.println(s1.getArea()); // run the overriddenversion
Shape s2 = new Triangle(3.2, 4.5,6.7);
System.out.println(s2.getPeri()); // run the overriddenversion
System.out.println(s2.getArea()); // run the overriddenversion
Shape s3 = new Circle(3.2);
System.out.println(s3.getCircum()); // run theoverridden version
System.out.println(s3.getArea()); // run the overriddenversion
}
}
please try for toString and equals method, if anythingrequires, CRAMSTER is there to helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.