Java. if i want to change List.java prublic String listName; prublic ArrayList d
ID: 3815830 • Letter: J
Question
Java. if i want to change List.java
prublic String listName;
prublic ArrayList dList;
to private and non - static and all the methods to non - static too, how to modifi in ListApp.java to keep same fuction?
import java.util.ArrayList;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;
public class DodecahedronList {
public static String listName;
public static ArrayList dList;
/**
* @param listNameIn - list name.
* @param dListIn the dodecahedron list.
*/
public DodecahedronList(String listNameIn, ArrayList dListIn) {
listName = listNameIn;
dList = dListIn;
}
/** @return - list .*/
public static ArrayList getList() {
return dList;
}
/** @return - list name.*/
public static String getName() {
return listName;
}
/**@return - size of the list.*/
public static int numberOfDodecahedrons() {
return dList.size();
}
/**@return - total surface area.*/
public static double totalSurfaceArea() {
double totalS = 0;
int index = 0;
while (index < dList.size()) {
totalS += dList.get(index).surfaceArea();
index++;
}
return totalS;
}
/**@return - total volume.*/
public static double totalVolume() {
double totalV = 0;
int index = 0;
while (index < dList.size()) {
totalV += dList.get(index).volume();
index++;
}
return totalV;
}
/**@return - average area.*/
public static double averageSurfaceArea() {
double avgA = 0;
if (dList.size() > 0) {
avgA = totalSurfaceArea() / dList.size();
}
return avgA;
}
/**@return - average volume.*/
public static double averageVolume() {
double avgV = 0;
if (dList.size() > 0) {
avgV = totalVolume() / dList.size();
}
return avgV;
}
/** @return - total surface area/volume ratio.*/
public static double totalRatio() {
double totalR = 0;
int index = 0;
while (index < dList.size()) {
totalR += dList.get(index).surfaceToVolumeRatio();
index++;
}
return totalR;
}
/**@return - average surface area/volume ratio.*/
public static double averageSurfaceToVolumeRatio() {
double avgR = 0;
if (dList.size() > 0) {
avgR = totalRatio() / dList.size();
}
return avgR;
}
/**@return - result as string.*/
public String toString() {
String result = listName + " ";
int index = 0;
while (index < dList.size()) {
result += " " + dList.get(index) + " ";
index++;
}
return result;
}
/**@return - summary info.*/
public static String summaryInfo() {
DecimalFormat fmt = new DecimalFormat("#,##0.0##");
String result = "";
result += "----- Summary for " + getName() + " -----";
result += " Number of Dodecahedrons: " + numberOfDodecahedrons();
result += " Total Surface Area: " + fmt.format(totalSurfaceArea());
result += " Total Volume: " + fmt.format(totalVolume());
result += " Average Surface Area: " + fmt.format(averageSurfaceArea());
result += " Average Volume: " + fmt.format(averageVolume());
result += " Average Suface/Volume Ratio: "
+ fmt.format(averageSurfaceToVolumeRatio());
return result;
}
// read file function
public static void readFile(String fileName)
{
String line;
ArrayList myList = new ArrayList();
listName = "Dodecahedron Test List";
String label = "";
String color = "";
double edge = 0;
try
{
Scanner scanFile = new Scanner(new File(fileName));
line = scanFile.nextLine();
while (scanFile.hasNext())
{
label = scanFile.nextLine();
color = scanFile.nextLine();
edge = Double.parseDouble(scanFile.nextLine());
Dodecahedron d = new Dodecahedron(label, color, edge);
myList.add(d);
}
DodecahedronList dlist = new DodecahedronList(listName, myList);
System.out.println(" File read in and DodecahedronList created ");
}
catch (FileNotFoundException ex)
{
System.out.println("FIle not found");
}
}
// add new dodecahedron object
public static void addDodecahedron(String label, String color, double edge)
{
Dodecahedron d = new Dodecahedron(label, color, edge);
dList.add(d);
System.out.println("***Dodecahedron added *** ");
}
// find dodecahedron object
public static void findDodecahedron(String label)
{
int flag = 0;
for (int i = 0; i < dList.size(); i++)
{
if (dList.get(i).getLabel().equalsIgnoreCase(label))
{
System.out.println(dList.get(i));
flag = 1;
}
}
if (flag == 0)
{
System.out.println(" " + label + " not found ");
}
}
// delete dodecahedron object
public static void deleteDodecahedron(String label)
{
int flag = 0;
for (int i = 0; i < dList.size(); i++)
{
if (dList.get(i).getLabel().equalsIgnoreCase(label))
{
dList.remove(i);
flag = 1;
System.out.println(" " + label + " deleted");
}
}
if (flag == 0)
{
System.out.println(" " + label + " not found ");
}
}
// edit dodecahedron object
public static boolean editDodecahedron(String label,
String color, double edge)
{
int flag = 0;
for (int i = 0; i < dList.size(); i++)
{
if ((dList.get(i).getLabel().equalsIgnoreCase(label)))
{
dList.get(i).setColor(color);
dList.get(i).setEdge(edge);
System.out.println(" " + label + " successfully edited ");
flag = 1;
return true;
}
}
if (flag == 0)
{
System.out.println(" " + label + " not found ");
return false;
}
else
{
return true;
}
}
}
===================================
===================================
//DodecahedronListMenuApp.java
import java.util.Scanner;
import java.io.IOException;
public class DodecahedronListMenuApp
{
// main method
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
char code;
do
{
// input choice from user
System.out.print(" DodecahedronList System Menu R -Read File and Create DodecahedronList P -Print DodecahedronList S -Print Summary A -Add Dodecahedron D -Delete Dodecahedron F -Find Dodecahedron E -Edit Dodecahedron Q -Quit Enter Code [R, P, S, A, D, F, E, or Q]:");
code = scan.next().charAt(0);
// switch case to determine which method to call
switch(code)
{
case 'R': case 'r':
System.out.print(" File name: ");
String fileName = scan.next();
DodecahedronList.readFile(fileName);
break;
case 'P': case 'p':
System.out.println(DodecahedronList.getName() + " " + DodecahedronList.getList());
break;
case 'S': case 's':
System.out.println(DodecahedronList.summaryInfo());
break;
case 'A': case 'a':
scan.nextLine();
System.out.print(" Label: ");
String label = scan.nextLine();
System.out.print(" Color: ");
String color = scan.nextLine();
System.out.print(" Edge: ");
double edge = scan.nextDouble();
DodecahedronList.addDodecahedron(label, color, edge);
break;
case 'D': case 'd':
scan.nextLine();
System.out.print(" Label: ");
label = scan.nextLine();
DodecahedronList.deleteDodecahedron(label);
break;
case 'F': case 'f':
scan.nextLine();
System.out.print(" Label: ");
label = scan.nextLine();
DodecahedronList.findDodecahedron(label);
break;
case 'E': case 'e':
scan.nextLine();
System.out.print(" Label: ");
label = scan.nextLine();
System.out.print(" Color: ");
color = scan.nextLine();
System.out.print(" Edge: ");
edge = scan.nextDouble();
DodecahedronList.editDodecahedron(label, color, edge);
break;
case 'Q': case 'q':
break;
default:
System.out.println("*** invalid code *** ");
break;
}
} while(code != 'q' && code != 'Q');
}
}
=======================
=======================
import java.text.DecimalFormat;
public class Dodecahedron {
/**
* Dodecahedron.
* @param args Commandline arguments - not used.
*/
private String label = "";
private String color = "";
private double edge = 0;
/**
* @param labelIn - input label.
* @param colorIn - input color.
* @param edgeIn - input edge.
*/
public Dodecahedron(String labelIn, String colorIn, double edgeIn) {
label = labelIn;
color = colorIn;
edge = edgeIn;
}
/**
* @param labelIn - label.
* @return true if applied, otherwise false.
*/
public boolean setLabel(String labelIn) {
if (labelIn != null) {
label = labelIn.trim();
return true;
}
else {
return false;
}
}
/**
* @param colorIn - color.
* @return true if applied, otherwise false.
*/
public boolean setColor(String colorIn) {
if (colorIn != null) {
color = colorIn.trim();
return true;
}
else {
return false;
}
}
/**
* @param edgeIn the edge.
* @return true if appled, otherwise false.
*/
public boolean setEdge(double edgeIn) {
if (edgeIn > 0) {
edge = edgeIn;
return true;
}
else {
return false;
}
}
/**@return label.*/
public String getLabel() {
return label;
}
/**@return color.*/
public String getColor() {
return color;
}
/**@return edge.*/
public double getEdge() {
return edge;
}
/**@return the value of the surface area.*/
public double surfaceArea() {
double surfaceArea = 3 * Math.sqrt(10 * Math.sqrt(5) + 25) * edge * edge;
return surfaceArea;
}
/**@return the value of the volume.*/
public double volume() {
double volume = Math.pow(edge, 3) * (15 + 7 * Math.sqrt(5)) / 4;
return volume;
}
/**@return the surface to volume ratio.*/
public double surfaceToVolumeRatio() {
double surfaceToVolumeRatio = surfaceArea() / volume();
return surfaceToVolumeRatio;
}
/**@return the string.*/
public String toString() {
DecimalFormat fmt = new DecimalFormat("#,##0.0##");
String output = " Dodecahedron " + """ + label.trim() + "" is ""
+ color.trim() + "" with 30 edges of length " + edge
+ " units.";
output += " surface area = " + fmt.format(surfaceArea())
+ " square units";
output += " volume = " + fmt.format(volume()) + " cubic units";
output += " surface/volume ratio = ";
output += fmt.format(surfaceToVolumeRatio());
return output;
}
}
Explanation / Answer
import java.util.Scanner;
import java.io.IOException;
public class DodecahedronListMenuApp
{
// main method
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
char code;
do
{
// input choice from user
System.out.print(" DodecahedronList System Menu R -Read File and Create DodecahedronList P -Print DodecahedronList S -Print Summary A -Add Dodecahedron D -Delete Dodecahedron F -Find Dodecahedron E -Edit Dodecahedron Q -Quit Enter Code [R, P, S, A, D, F, E, or Q]:");
code = scan.next().charAt(0);
// switch case to determine which method to call
switch(code)
{
case 'R': case 'r':
System.out.print(" File name: ");
String fileName = scan.next();
DodecahedronList.readFile(fileName);
break;
case 'P': case 'p':
System.out.println(DodecahedronList.getName() + " " + DodecahedronList.getList());
break;
case 'S': case 's':
System.out.println(DodecahedronList.summaryInfo());
break;
case 'A': case 'a':
scan.nextLine();
System.out.print(" Label: ");
String label = scan.nextLine();
System.out.print(" Color: ");
String color = scan.nextLine();
System.out.print(" Edge: ");
double edge = scan.nextDouble();
DodecahedronList.addDodecahedron(label, color, edge);
break;
case 'D': case 'd':
scan.nextLine();
System.out.print(" Label: ");
label = scan.nextLine();
DodecahedronList.deleteDodecahedron(label);
break;
case 'F': case 'f':
scan.nextLine();
System.out.print(" Label: ");
label = scan.nextLine();
DodecahedronList.findDodecahedron(label);
break;
case 'E': case 'e':
scan.nextLine();
System.out.print(" Label: ");
label = scan.nextLine();
System.out.print(" Color: ");
color = scan.nextLine();
System.out.print(" Edge: ");
edge = scan.nextDouble();
DodecahedronList.editDodecahedron(label, color, edge);
break;
case 'Q': case 'q':
break;
default:
System.out.println("*** invalid code *** ");
break;
}
} while(code != 'q' && code != 'Q');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.