Specifications -Use arrays in this project; ArrayLists are not allowed! Overview
ID: 3725888 • Letter: S
Question
Specifications -Use arrays in this project; ArrayLists are not allowed! Overview: The objective is to modify your previous project to use arrays instead of Array List objects, You will write a program this week that is composed of three classes: the first class defines ConicalFrustum objects, the second class defines ConicalFrustumList2 objects, and the third, ConicalFrustumList2MenuApp, presents a menu to the user with eight options and implements these: (1) read input file (which creates a ConicalFrustumList2 object), (2) print report, (3) print summary (4) add a ConicalFrustum object to the ConicalFrustumList2 object, (5) delete a ConicalFrustum object from the ConicalFrustumList2 object, (6) find a ConicalFrustum object in the ConicalFrustumList2 object, (7) Edit a ConicalFrustum in the ConicalFrustumList2 object, and (8) quit the program. IYou should create a new project folder and copy your previous project files ConicalFrustum.java, Conical FrustumList.java, ConicalFrustumListMenuApp.java, conical frustum data1.txt, and conical frustum data 0.txt) to it, rather than work in the same folder as the previous project.] xisting.iava file. open the file in jGRASP, change the name of the class and the name of the constructor (if it has one) in the source file, and then click the Save button. ick the “Rename and Save" button. This will rename and save the o renam In the dialog that pops up, el java file and delete the old associated.class file. Requirements: Create a ConicalFrustum class that stores the label, radius of top, radius of bottom, and height where the radii and height are non-negative. The ConicalFrustum class also includes methods to set and get cach of these fields, as well as methods to calculate the volume, slant height, lateral surface area, and total surface area of a ConicalFrustum object, and a method to provide a String value that describes a ConicalFrustum object.Explanation / Answer
//CanonicalFrustum.java
import java.io.*;
import java.util.*;
class ConicalFrustum {
//defining memebers
public static final double PI = 3.14;
private String label = "";
private double radius1 = 0;
private double radius2 = 0;
private double height = 0;
//defining contructor with 4 arguments
public ConicalFrustum(String labelIn, double radius1In, double radius2In, double heightIn) {
setLabel(labelIn);
setRadius1(radius1In);
setRadius2(radius2In);
setHeight(heightIn);
}
public boolean setLabel(String labelIn) {
if (!labelIn.equals("")) {
label = labelIn;
return true;
}
return false;
}
public boolean setRadius1(double radius1In) {
if (radius1In >= 0) {
radius1 = radius1In;
return true;
}
return false;
}
public boolean setRadius2(double radius2In) {
if (radius2In >= 0) {
radius2 = radius2In;
return true;
}
return false;
}
public boolean setHeight(double heightIn) {
if (heightIn >= 0) {
height = heightIn;
return true;
}
return false;
}
public String getLabel() {
return label;
}
public double getRadius1() {
return radius1;
}
public double getRadius2() {
return radius2;
}
public double getHeight() {
return height;
}
public double volume() {
double r1 = getRadius1(), r2 = getRadius2(), h = getHeight();
return (1 / 3) * PI * h * ((r1 * r1) * (r2 * r2) + (r1 * r2));
}
public double slantHeight() {
double r1 = getRadius1(), r2 = getRadius2(), h = getHeight();
return Math.sqrt(Math.pow(r1 - r2, 2) + (h * h));
}
public double lateralSurfaceArea() {
double r1 = getRadius1(), r2 = getRadius2(), h = getHeight();
return Math.PI * (r1 + r2) * slantHeight();
}
public double totalSurfaceArea() {
double r1 = getRadius1(), r2 = getRadius2(), h = getHeight();
return Math.PI *((r1*r1)+(r2*r2)+(r1+r2)*slantHeight());
}
public String toString() {
String str = "ConicalFrustum " + getLabel() + " with radius1 = " + getRadius1() + ", radius1 = " + getRadius2()
+ ", height = " + getHeight() + " has:";
str += " volume = " + volume() + " cubic units slant height = " + slantHeight()
+ " units lateral surface area = " + lateralSurfaceArea() + " units total surface area = "
+ totalSurfaceArea() + " square units";
return str;
}
}
//CanonicalFrustumList.java
import java.io.*;
import java.util.*;
class ConicalFrustumList {
private String name;
private List<ConicalFrustum> list = new ArrayList<ConicalFrustum>();
ConicalFrustumList(String nameIn) {
name = nameIn;
}
public String getName() {
return name;
}
public int numberOfConicalFrustums() {
return list.size();
}
public double totalSurfaceArea() {
double res = 0;
for (ConicalFrustum el : list) {
res += el.totalSurfaceArea();
}
return res;
}
public double totalVolume() {
double res = 0;
for (ConicalFrustum el : list) {
res += el.volume();
}
return res;
}
public double averageVolume() {
return totalVolume() / numberOfConicalFrustums();
}
public double averageSurfaceArea() {
return totalSurfaceArea() / numberOfConicalFrustums();
}
public String toString() {
String str = getName();
System.out.println(list.size());
for (ConicalFrustum el : list) {
str += " " + el.toString() + " ";
}
return str;
}
public String summaryInfo() {
String str = "---------- Summary for " + getName() + " ------------ ";
str += "Number of ConicalFrustum Objects: " + numberOfConicalFrustums() + " ";
str += "Total Surface Area: " + totalSurfaceArea() + " ";
str += "Total Volume: " + totalVolume() + " ";
str += "Average Surface Area: " + averageSurfaceArea() + " ";
str += "Average Volume: " + averageVolume() + " ";
return str;
}
public List<ConicalFrustum> getList() {
return list;
}
public void readFile(String fileName) {
try {
File file = new File(fileName);
Scanner sc = new Scanner(new FileReader(file));
String label;
double radius1, radius2, height;
while (sc.hasNextLine()) {
label = sc.next();
radius1 = sc.nextDouble();
radius2 = sc.nextDouble();
height = sc.nextDouble();
addConicalFrustum(label, radius1, radius2, height);
}
} catch (FileNotFoundException e) {
System.out.println("File Not found");
}
}
public void addConicalFrustum(String labelIn, double radius1In, double radius2In, double heightIn) {
ConicalFrustum newItem = new ConicalFrustum(labelIn, radius1In, radius2In, heightIn);
list.add(newItem);
}
public ConicalFrustum findConicalFrustum(String label) {
for (ConicalFrustum el : list) {
if (el.getLabel() == label) {
return el;
}
}
return null;
}
public boolean deleteConicalFrustum(String label) {
for (ConicalFrustum el : list) {
if (el.getLabel() == label) {
list.remove(el);
return true;
}
}
return false;
}
public boolean editConicalFrustum(String labelIn, double radius1In, double radius2In, double heightIn) {
for (ConicalFrustum el : list) {
if (el.getLabel() == labelIn) {
el.setRadius1(radius1In);
el.setRadius2(radius2In);
el.setHeight(heightIn);
return true;
}
}
return false;
}
}
//CanonicalFrustumListMenuApp.java
import java.io.*;
import java.util.*;
class ConicalFrustumListMenuApp {
public static void main(String[] args) {
char choice;
ConicalFrustumList list = new ConicalFrustumList("Testing");
do{
System.out.println("Conical Frustum List System Menu");
System.out.println("R - Read File and Create ConicalFrustum List");
System.out.println("P - Print ConicalFrustum List");
System.out.println("S - Print Summary");
System.out.println("A - Add ConicalFrustum");
System.out.println("D - Delete ConicalFrustum");
System.out.println("F - Find ConicalFrustum");
System.out.println("E - Edit ConicalFrustum");
System.out.println("Q - Quit");
System.out.println("Enter Code [R, P, S, A, D, F, E, or Q]: ");
Scanner in = new Scanner(System.in);
choice = in.next().charAt(0);
switch (choice) {
case 'R': {
String file;
System.out.print("Enter File name: ");
file = in.next();
list.readFile(file);
System.out.println("File read in and ConicalFrustum List created");
break;
}
case 'P':
System.out.println(list.getList().size());
System.out.println(list.toString());
break;
case 'S':
System.out.println(list.summaryInfo());
break;
case 'A': {
String label;
double radius1, radius2, height;
System.out.println("Label: ");
label = in.next();
System.out.println("Radius1: ");
radius1 = in.nextDouble();
System.out.println("Radius2: ");
radius2 = in.nextDouble();
System.out.println("Height: ");
height = in.nextDouble();
list.addConicalFrustum(label, radius1, radius2, height);
System.out.println("*** ConicalFrustum added ***");
break;
}
case 'D': {
String label;
System.out.println("Label: ");
label = in.next();
if (list.deleteConicalFrustum(label)) {
System.out.println(""" + label + "" deleted");
} else {
System.out.println(""" + label + "" not found");
}
}
break;
case 'F': {
String label;
System.out.print("Label: ");
label = in.next();
ConicalFrustum r = list.findConicalFrustum(label);
if (r == null) {
System.out.println(""" + label + "" not found");
} else {
r.toString();
}
break;
}
case 'E': {
String label;
double radius1, radius2, height;
System.out.println("Label: ");
label = in.next();
System.out.println("Radius1: ");
radius1 = in.nextDouble();
System.out.println("Radius2: ");
radius2 = in.nextDouble();
System.out.println("Height: ");
height = in.nextDouble();
if(list.editConicalFrustum(label, radius1, radius2, height))
System.out.println("""+label+"" successfully edited");
else{
System.out.println("""+label+"" not found");
}
break;
}
case 'Q':
System.exit(1);
break;
default:
System.out.println("***** Invalid code *****");
}
}while(choice!='Q');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.