Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

odecahedron List Menu App Page 4 of 10 decabedron List,j ava- extended from Proj

ID: 3891772 • Letter: O

Question

odecahedron List Menu App Page 4 of 10 decabedron List,j ava- extended from Project 5 by adding the last six methods below. that you successfully created this class DodecahedronlList java to your new Prolect 6 folder and then ad the Indicated metheds otherwise, you will need to create all of DodecahedrenList.java as part of this prolesct.) Requirements: Create a DodecahedronList class that stores the name of the list and an ArrayList of Dodecahedron objects. It also includes methods that return the name of the list, number of Dodecahedron objects in the DodecahedronList, total surface area, total volume, average surface area, average volume, and average surface to volume ratio for all Dodecahedron objects in the DodecahedronList. The toString method returns a String containing the name of the list followed by each Dodecahedron in the ArrayList, and a summaryinfo method returns summary information about the list (see below). Design: The DodecahedronList class has two fields, a constructor, and methods as outined below (1) Fields (or instance variables): (1) a String representing the name of the list and (2) an ArrayList of Dodecahedron objects. These are the only fields (or instance variables) that this class should have. (2) Constructor: Your DodecahedronList class must contain a constructor that accepts a parameter of type String representing the name of the list and a parameter of type ArrayList Dodecahedron> representing the list of Dodecahedron objects. These parameters should be used to assign the fields described above (ie, the instance variables). (3) Methods: The methods for DodecahedronList are described below o getName: Returns a String representing the name of the list o numberofDodecahedrons: Returns an int representing the number of Dodecahedron objects in the DodecahedronList. If there are zero Dodecahedron objects in the list, zero should be returned. o totalSurfaceArea: Return s a double representing the total surface areas for all Dodecahedron objects in the list. If there are zero Dodecahedron objects in the list, zero should be returned. totalVolume: Returns a double representing the total volumes for all Dodecahedron objects in the list. returned. o If there are zero Dodecahedron objects in the list, zero should be averageSurfaceArea: Returns a double representing the average surface area for all Dodecahedron objects in the list. zero should be returned. o If there are zero Dodecahedron objects in the list, Returns a double representing the average volume for all averageVolume: Dodecahedron objects in the list. should be returned. o If there are zero Dodecahedron objects in the list, zero Returns a double representing the average averageSurfaceToVolumeRatio: surface to volume ratio for all Dodecahedron objects in the list. If there are zero Dodecahedron objects in the list, zero should be retuned o

Explanation / Answer


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;

/**
*
* @author pc
*/
public class DodecahedronList {
//constructor which sets both the fields
public DodecahedronList(String listName,ArrayList<Dodecahedron> arrr)
{
this.listName=listName;
this.arr=arrr;
}
//fields
private String listName;
private ArrayList<Dodecahedron> arr;
  
//getter to get the name
public String getName()
{
return listName;
}
//return the number of dodecahedron objects present in list
public int numberOfDodecahedrons()
{
int n;
n=arr.size();
return n;
}
//return total surface area of all th object and return 0 if no objects are present
public double totalSurfaceArea()
{
int n;
n=this.numberOfDodecahedrons();
if(n==0)
return 0;//return 0
else
{
//calculate the total surface area
double totalArea=0;
for(int i=0;i<arr.size();i++)
{
Dodecahedron d=arr.get(i);
totalArea+=(20.64*Math.pow(d.getEdge(),2));//calculations
}
return totalArea;
}
}
//return the total volume
public double totalVolume()
{
int n;
n=this.numberOfDodecahedrons();
if(n==0)
return 0;
else
{
//calculate the total volume of all the objects
double totalVol=0;
for(int i=0;i<arr.size();i++)
{
Dodecahedron d=arr.get(i);
totalVol+=(7.66*Math.pow(d.getEdge(),3));
}
return totalVol;
}
}
//return average surface area of dodecahedron objects
public double averageSurfaceArea()
{
int n;
n=this.numberOfDodecahedrons();
if(n==0)
return 0;
else
{
double totalArea=0;
for(int i=0;i<arr.size();i++)
{
Dodecahedron d=arr.get(i);
totalArea+=(20.64*Math.pow(d.getEdge(),2));//find total area
}
double avgArea=totalArea/arr.size();//calculate average by dividing total area by number of objects present
return avgArea;
}
}
//return average volume
public double averageVolume()
{
int n;
n=this.numberOfDodecahedrons();
if(n==0)
return 0;
else
{
double totalVol=0;
for(int i=0;i<arr.size();i++)
{
Dodecahedron d=arr.get(i);
totalVol+=(7.66*Math.pow(d.getEdge(),3));
}
return (totalVol/arr.size());
}
}
//return average surface area to volume
public double averageSurfaceToVolumeRatio()
{
int n;
n=this.numberOfDodecahedrons();
if(n==0)
return 0;
else
{
double avgSur=this.averageSurfaceArea();
double avgVol=this.averageVolume();
return avgSur/avgVol;//return ratio
}
}
//to string
public String toString()
{
String str="";
str=str+this.listName;//list name
int i=0;
//looping for all the objects and add the tostring all the objects one by one
while(i<arr.size())
{
str=str+" "+arr.get(i).toString()+" ";
}
return str;
}
//summary info
public String summaryInfo()
{
String str="";
str=str+listName;
str=" "+"Summary items: ";
DecimalFormat form=new DecimalFormat("#,,##0.0##");//format the double in this format
//add all the summary items in the string to return
str=str+form.format(this.numberOfDodecahedrons())+","+form.format(this.totalSurfaceArea())+","+form.format(this.totalVolume())+","+form.format(this.averageSurfaceArea())+","+form.format(this.averageSurfaceToVolumeRatio());
return str;
}
public ArrayList<Dodecahedron> getList()
{
return arr;//return list
}
public DodecahedronList readFile(String fileName)
{
//open file
File f=new File(fileName);
try{
BufferedReader br = new BufferedReader(new FileReader(f));
String listNamme;
ArrayList<Dodecahedron> ard=new ArrayList<Dodecahedron>();
listNamme=br.readLine();//read listname from file
String st;
//read all the dodecahedron objects present in the file and add them to the arraylist ard
while((st = br.readLine()) != null)
{
String label=st;
int edge=Integer.parseInt(br.readLine());
String color=br.readLine();
Dodecahedron dd=new Dodecahedron(label,edge,color);
ard.add(dd);//add
  
}
DodecahedronList dl=new DodecahedronList(listNamme,ard);
br.close();
return dl;
}
catch(Exception e)
{
  
}
return null;
}
//add dodecahedron object into list
public void addDodecahedron(String label,String color,int edge)   
{
Dodecahedron d=new Dodecahedron(label,edge,color);
this.arr.add(d);
  
}
//find dodecahedron object by label field if not found return null
public Dodecahedron findDodecahedron(String label)
{
for(int i=0;i<arr.size();i++)
{
Dodecahedron d=arr.get(i);
if(d.getLabel().equalsIgnoreCase(label))
{
return d;
}
  
}
return null;
}//delete found obbject
public Dodecahedron deleteDodecahedron(String label)
{
for(int i=0;i<arr.size();i++)
{
Dodecahedron d=arr.get(i);
if(d.getLabel().equalsIgnoreCase(label))
{
arr.remove(d);
return d;
}
  
}
return null;
}//edit object's field if found
public boolean editDodecahedron(String label, String color, int edge)
{
for(int i=0;i<arr.size();i++)
{
Dodecahedron d=arr.get(i);
if(d.getLabel().equalsIgnoreCase(label))
{
d.setColor(color);
d.setEdge(edge);
return true;
}
  
}
return false;
}
}


public class Dodecahedron {

//toString method which gives the description of this object
@Override
public String toString() {
return "Dodecahedron{" + "label=" + label + ", edge=" + edge + ", color=" + color + '}';
}
//getter method to get all the field one by one
public String getLabel() {
return label;
}
//setter method
public void setLabel(String label) {
this.label = label;
}

public int getEdge() {
return edge;
}

public void setEdge(int edge) {
this.edge = edge;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}
//constructor method which sets all the field
public Dodecahedron(String label, int edge, String color) {
this.label = label;
this.edge = edge;
this.color = color;
}
//fields
private String label;
private int edge;
private String color;
  
}