Please submit the following java programs in a single zipped folder on Canvas: O
ID: 3821875 • Letter: P
Question
Please submit the following java programs in a single zipped folder on Canvas:
Octagonal Prism (60 pts) - OctPrism.java
Driver Program (40 pts) – OctDriver.java
Extra Credit: Square Table (10pts) – SquareTable.java
Surface Area and Volume of a Right Regular Octagonal Prism
A right regular octagonal prism is a geometric figure with octagons at its top and base (shown below)
The surface area and volume of this type of prism are:
where a represents the base edge and h represents the height.
Write a class called OctPrism that contains the necessary information for storing and calculating the surface area and volume of a octagonal prism. In particular, the class should have:
members to store the base and height data
a constructor that accepts no parameters that initializes base and height to 0.0
mutators for the base and height
accessors for the base, height, area, and volume
Write a program that demonstrates the OctPrism class by:
creating an OctPrism object
asking the user for the base and height of the prism
reporting the base, height, area and volume
the base and height should be referenced in the output by using the appropriate accessors
The area and volume results should be set to 4 decimal places
Samples of the output are shown below:
Sample Output 1:
Enter the base edge of an Octagonal Prism: 5
Enter the height of an Octagonal Prism: 7
Given an octagonal prism with a base edge of 5.0 units, and height of 7.0 units:
The area is: 521.4214 units squared.
The volume is: 844.9747 units cubed.
Sample Output 2:
Enter the base edge of an Octagonal Prism: 10
Enter the height of an Octagonal Prism: 3
Given an octagonal prism with a base edge of 10.0 units, and height of 3.0 units:
The area is: 1205.6854 units squared.
The volume is: 1448.5281 units cubed.
Explanation / Answer
OctPrism.java
public class OctPrism {
private double base;
private double height;
public double getArea()
{
double area = 8*base*height + 4*(1+Math.sqrt(2))*base*base;
return area;
}
public double getVolume()
{
double volume = 2*(1+Math.sqrt(2))*base*base*height;
return volume;
}
public OctPrism(double a, double h)
{
this.base = a;
this.height = h;
}
public OctPrism()
{
this(0.0, 0.0);
}
public double getBase() {
return base;
}
public void setBase(double base) {
this.base = base;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
DriverOctPrism.java
import java.util.Scanner;
public class DriverOctPrism {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the base edge of an Octagonal Prism: ");
double base = sc.nextDouble();
System.out.print("Enter the height of an Octagonal Prism: ");
double height = sc.nextDouble();
OctPrism octprism = new OctPrism();
octprism.setBase(base);
octprism.setHeight(height);
System.out.printf("Given an octagonal prism with a base edge of %.1f units, and height of %.1f units: ", octprism.getBase(), octprism.getHeight());
System.out.printf("The area is: %.4f units squared. ", octprism.getArea());
System.out.printf("The volume is: %.4f units cubed. ", octprism.getVolume());
sc.close();
}
}
Sample output
Enter the base edge of an Octagonal Prism: 5
Enter the height of an Octagonal Prism: 7
Given an octagonal prism with a base edge of 5.0 units, and height of 7.0 units:
The area is: 521.4214 units squared.
The volume is: 844.9747 units cubed.
Enter the base edge of an Octagonal Prism: 10
Enter the height of an Octagonal Prism: 3
Given an octagonal prism with a base edge of 10.0 units, and height of 3.0 units:
The area is: 1205.6854 units squared.
The volume is: 1448.5281 units cubed.