Create a Java program that will: . Solve problem using Array list . Preform file
ID: 3908289 • Letter: C
Question
Create a Java program that will:
. Solve problem using Array list
. Preform file I/O
. Apply Geometric concepts that is translated to Java
The program will allow users to read a data from a file about Spheroids
You will read the data, store it appropriately in an object of type Spheroid, and produce the volume and whether the sphere is oblate or prolate. Obviously, you must create a custom class (that must be named Spheroid), and you must provide a means to store the required data.
The required data consist of the equatorial radius, and the polar radius. Some information related to spheroids:
• A spheroid is oblate if c < a
• A spheroid is prolate if c > a
• A spheroid is a sphere if c = a The volume of a spheroid, regardless of its classification is:
?? = 4 3 ???? 2?? Again, a is the equatorial radius, and c is the polar radius. For a sphere, since a = c, this would reduce to the familiar form ?? = 4 3 ???? 3 .
The input will be obtained from a file, named input.txt. A single line, therefore takes the general form:
Equatorial_Radius Polar_Radius Specifically, a single line of example input might be:
5 2
As soon as the file is read into memory and the data is placed in the appropriate data structure(s), the following will be printed to standard output (the console), in general form: Volume Classification Where volume is the calculated volume from the formula, and the classification is oblate, prolate, or sphere.
SAMPLE INPUT FILE
5 2
10 10
4 3
6 8
SAMPLE OUTPUT
209.44 Oblate
4188.79 Sphere
201.06 Oblate
1206.37 Prolate
Hints and Requirments:
You should consider creating two classes to help you solve the problem:
• Spheroid
o Represents a single spheroid
o Maintains the equatorial and polar radii as fields
o Has a constructor allowing the user to set the equatorial and polar radii (as doubles)
o Has accessors (getters) and mutators (setters) for both the equatorial radius and polar radius
o Can return the volume using a method getVolume ? Returns a double ? Takes no parameters
o Can return the classification of a spheroid, using a method getClassification ? Returns a string ? Takes no parameters ? Calculates the information based on the current equatorial and polar
o There should be NO print statements inside this class
• SpheroidDemo
o This class will contain the main method
o You must read the file
o You must create instances of Spheroid, and store them in an ArrayList ? You can use the constructor of the Spheroid immediately with the equatorial and polar radii that are read from file
o Once all information is read from the file and all the spheroid objects are created, loop through the ArrayList of Spheroids and print out their volumes and classifications to the console
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Spheroid.java
public class Spheroid {
//attributes
private double equitorial_radius;
private double polar_radius;
/**
* constructor to initialize both equitorial and polar radii
* @param equitorial_radius (a)
* @param polar_radius (c)
*/
public Spheroid(double equitorial_radius, double polar_radius) {
this.equitorial_radius = equitorial_radius;
this.polar_radius = polar_radius;
}
//getters and setters
public double getEquitorial_radius() {
return equitorial_radius;
}
public void setEquitorial_radius(double equitorial_radius) {
this.equitorial_radius = equitorial_radius;
}
public double getPolar_radius() {
return polar_radius;
}
public void setPolar_radius(double polar_radius) {
this.polar_radius = polar_radius;
}
/**
* @return the volume of spheroid
*/
public double getVolume() {
// applying general equation 4/3*Pi*a*a*c to find the volume
return (4.0 / 3.0) * Math.PI * equitorial_radius * equitorial_radius
* polar_radius;
}
/**
* @return the spheroid classification
*/
public String getClassification() {
if (equitorial_radius < polar_radius) {
//a<c
return "Prolate";
} else if (equitorial_radius > polar_radius) {
//a>c
return "Oblate";
} else {
//a=c
return "Sphere";
}
}
}
// Test.java (contains main method)
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
/**
* creating an arraylist of spheroids
*/
ArrayList<Spheroid> spheroids = new ArrayList<Spheroid>();
/**
* Defining input file, make sure you have input.txt in your project
* directory
*/
File file = new File("input.txt");
/**
* opening the file and looping through each pair of data
*/
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
//getting radii values
double a = Double.parseDouble(scanner.next());
double c = Double.parseDouble(scanner.next());
//creating a spheroid
Spheroid spheroid = new Spheroid(a, c);
//adding to list
spheroids.add(spheroid);
}
//looping through all spheroids
for (Spheroid s : spheroids) {
//displaying volume and classification
System.out
.printf("%.2f %s ", s.getVolume(), s.getClassification());
}
}
}
/*input.txt*/
5 2
10 10
4 3
6 8
/*OUTPUT*/
209.44 Oblate
4188.79 Sphere
201.06 Oblate
1206.37 Prolate