Java please help, I am very confused on how to complete this assignment since ou
ID: 3799228 • Letter: J
Question
Java please help, I am very confused on how to complete this assignment since our professor barely covered it and I lack prior coding experience.
Requirements: Create a Dodecahedron class that stores the label, color, and edge (i.e., length of
an edge, which must be greater than zero). The Dodecahedron class also includes methods to set
and get each of these fields, as well as methods to calculate the surface area, volume, and surface
to volume ratio of a Dodecahedron object, and a method to provide a String value of a Dodecahedron object (i.e., a class instance).
Explanation / Answer
Solution: See the code below:
1. Dodecahedron class: Dodecahedron.java
-----------------------------------
package dodecahedron;
import java.text.DecimalFormat;
import java.text.NumberFormat;
/**
* Dodecahedron class
*
*/
public class Dodecahedron {
//class members
private String label; //label of dodecahedron
private String color; //color of dodecahedron
private double edge; //length of edge
/**
* Constructor
*/
public Dodecahedron() {
label="";
color="";
edge=0.0;
}
/**
* @param label
* @param color
* @param edge
*/
public Dodecahedron(String label, String color, double edge) {
setLabel(label);
setColor(color);
setEdge(edge);;
}
/**
* @return the label
*/
public String getLabel() {
return label;
}
/**
* @param label the label to set
*/
public void setLabel(String label) {
this.label = label;
}
/**
* @return the color
*/
public String getColor() {
return color;
}
/**
* @param color the color to set
*/
public void setColor(String color) {
this.color = color;
}
/**
* @return the edge
*/
public double getEdge() {
return edge;
}
/**
* @param edge the edge to set
*/
public void setEdge(double edge) {
this.edge = edge;
}
/**
* @return surface area
*/
public double surfaceArea()
{
return (Math.cbrt(25+(10*Math.sqrt(5)))*(edge*edge));
}
/**
* @return volume
*/
public double volume()
{
return (((15+7*Math.sqrt(5))/4)*(edge*edge*edge));
}
/**
* @return surface area to volume ratio
*/
public double surfaceVolumeRatio()
{
return (surfaceArea()/volume());
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
NumberFormat formatter = new DecimalFormat("#,##0.0##");
String val = "Dodecahedron "+'"' + getLabel() +'"' + " is" + '"' + getColor() + '"' + " with 30 edges of length" + getEdge() + "units." +
" surface area = " + formatter.format(surfaceArea()) + " square units" +
" volume = " + formatter.format(volume()) + " cubic units" +
" surface/volume ratio = " + formatter.format(surfaceVolumeRatio());
return val;
}
}
--------------------------------------------------
2. DodecahedronList class: DodecahedronList.java
----------------------------------------------------
package dodecahedron;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
/**
* DodecahederonList class
*
*/
public class DodecahederonList {
//class members
private String dodecahedronListName; //name of list
private ArrayList<Dodecahedron> dodecahedrons; //array list containing dodecahedron objects
/**
* @param dodecahedronListName
* @param dodecahedrons
*/
public DodecahederonList(String dodecahedronListName, ArrayList<Dodecahedron> dodecahedrons) {
this.setDodecahedronListName(dodecahedronListName);
this.dodecahedrons = dodecahedrons;
}
/**
* @return the dodecahedronListName
*/
public String getDodecahedronListName() {
return dodecahedronListName;
}
/**
* @param dodecahedronListName the dodecahedronListName to set
*/
public void setDodecahedronListName(String dodecahedronListName) {
this.dodecahedronListName = dodecahedronListName;
}
/**
* @return number of dodecahedrons
*/
public int numberOfDodecahedrons()
{
return dodecahedrons.size();
}
/**
* @return total surface area of all dodecahedrons in list
*/
public double totalSurfaceArea()
{
double area=0.0;
for (Dodecahedron dodecahedron : dodecahedrons) {
area+=dodecahedron.surfaceArea();
}
return area;
}
/**
* @return total volume of all dodecahedrons in list
*/
public double totalVolume()
{
double volume=0.0;
for (Dodecahedron dodecahedron : dodecahedrons) {
volume+=dodecahedron.volume();
}
return volume;
}
/**
* @return average surface area of all dodecahedrons in list
*/
public double averageSurfaceArea()
{
double averageArea=0.0;
if(dodecahedrons.size()!=0)
averageArea=totalSurfaceArea()/dodecahedrons.size();
return averageArea;
}
/**
* @return average volume of all dodecahedrons in list
*/
public double averageVolume()
{
double averageVolume=0.0;
if(dodecahedrons.size()!=0)
averageVolume=totalVolume()/dodecahedrons.size();
return averageVolume;
}
/**
* @return average surface to volume ration for all dodecahedrons in list.
*/
public double averageSurfaceToVolumeRatio()
{
return ((dodecahedrons.size()!=0)?(averageSurfaceArea()/averageVolume()):0.0);
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "DodecahederonList [dodecahedronListName=" + dodecahedronListName + ", dodecahedrons=" + dodecahedrons
+ "]";
}
public String summaryInfo()
{
NumberFormat formatter = new DecimalFormat("#,##0.0##");
return "DodecahederonList [Name="+dodecahedronListName+
" Number of Dodecahedrons="+numberOfDodecahedrons()+
" Total Surface Area="+formatter.format(totalSurfaceArea())+
" Total Volume="+formatter.format(totalVolume())+
" Average Surface Area="+formatter.format(averageSurfaceArea())+
" Average Volume="+formatter.format(averageVolume())+
" Average Surface To Volume Ratio="+formatter.format(averageSurfaceToVolumeRatio());
}
}
------------------------------------------------
3. DodecahedronListApp class: DodecahedronListApp.java
---------------------------------------------
package dodecahedron;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* DedecahedronListApp class
*
*/
public class DodecahedronListApp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter name of file:");
String filename="";
Scanner in=new Scanner(System.in);
filename=in.next();
try {
BufferedReader reader= new BufferedReader(new FileReader(new File(filename)));
int linesRead=0;
String listName="";
String line="";
ArrayList<Dodecahedron> dodecahedrons=new ArrayList<Dodecahedron>();
String name="", color="";
double edge=0.0;
int counter=0;
while((line=reader.readLine())!=null)
{
linesRead++;
if(linesRead==1)
{
listName=line;
}
else
{
counter++;
switch(counter)
{
case 1:
name=line;
break;
case 2:
color=line;
break;
case 3:
edge=Double.parseDouble(line);
Dodecahedron dodecahedron=new Dodecahedron(name, color, edge);
dodecahedrons.add(dodecahedron);
name="";
color="";
edge=0.0;
counter=0;
break;
}
}
}
DodecahederonList dodecahederonList=new DodecahederonList(listName, dodecahedrons);
System.out.println("Dodecahedrons'list:");
System.out.println(dodecahederonList.toString());
dodecahederonList.summaryInfo();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
in.close();
}
}
--------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.