Point Value: 10 points Due: Thursday August 31, 2017, 4:30pm Description Write a
ID: 2247377 • Letter: P
Question
Point Value: 10 points Due: Thursday August 31, 2017, 4:30pm Description Write a lava program to create and test a Pentagon class The class should contain: A double data member named sideLength that specifies the side length of the pentagon. A default no-arg constructor that creates a pentagon based on default values. Accessor and mutator methods for the side length data member. o The default value for side length should be 1.0. .A constructor that creates a pentagon with a specified side length, passed as a parameter. The mutator method should check for any values less than zero. If a client attempts to set side length to a negative number, the code should set it to 0.0 instead. o A method named getArea() that returns the area of this pentagon. The implementation for this method MUST use the accessor method to access side length; it should not use direct data member access. o A method named getPerimeter) that returns the perimeter of this pentagon. The implementation for this method MUST use the accessor method to access side length; it should not use direct data member access. o Ensure that all data members and methods have appropriate visibility modifiers (public or private). Draw the UML diagram for the class and then implement the class. Write a test program that: Creates an array of 5 Pentagon objects-with side lengths of 4.0, 40.0, 3.5, 35.9, and 2.1. Has a static void method called printPentagon() that accepts a single Pentagon array as a . parameter and prints out all Pentagon information (side length, area, and perimeter) as shown in the expected output. This method should work for any length Pentagon a o NOTE: Study System.out.printf) and format designators to limit decimal places. o For example: If value contains a decimal value of 3.2934949484848309 System.out.printf("Value: %.2nn", value); will print Value: 3.29 Vn provides a new line character for System.out.printf(), starting new output on a line by itself · Side length should be limited to one decimal place, area and perimeter to 2 places. oExplanation / Answer
import java.io.*;
import java.lang.*;
public class PAssign2{
public static void main(String[] args){
Pentagon[] p = new Pentagon[5];
p[0] = new Pentagon(4.0);
p[1] = new Pentagon(40.0);
p[2] = new Pentagon(3.5);
p[3] = new Pentagon(35.9);
p[4] = new Pentagon(2.1);
PAssign2.printPentagon(p);
}
public static void printPentagon(Pentagon[] a){
for(int i = 0; i<a.length; i++){
System.out.printf("Pentagon %d ",i+1);
System.out.printf("Side Length : %.1f ", a[i].getSideLength());
System.out.printf("Area : %.2f ", a[i].getArea());
System.out.printf("Perimeter : %.2f ", a[i].getPerimeter());
System.out.printf(" ");
}
}
}
class Pentagon{
private double sideLength;
public Pentagon(){
sideLength = 1.0;
}
public Pentagon(double a){
sideLength = a;
}
public void setSideLength(double a){
if (a >= 0)
sideLength = a;
else
sideLength = 0.0;
}
public double getSideLength(){
return sideLength;
}
public double getArea(){
double s = getSideLength();
return 1.0/4.0 * (Math.sqrt(5.0 * (5.0 + 2.0*Math.sqrt(5.0)))* (s * s));
}
public double getPerimeter(){
double s = getSideLength();
return 5 * s;
}
}
Pentagon
sideLength : double
getSideLength():double
setSideLength(double):void
getArea():double
getPerimeter():double
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.