Java Program Please read very carefully Required Formulas Co
Java Program Please read very carefully.
Required Formulas:
Cone Volume: 1/3 r^2 h
Ellipsoid Volume: 4/3 r1 r2 r3
Sphere Volume: 4/3 r^3
-------------------------------------------------------------------
Required Input Files: Dat file
------------------------------------------------------------------
The Shape Interface:
NOTE: You may not add anything to this interface, it must be implemented exactly as shown.
Shape has only one public method, getVolume().
This method should return a double.
This method will need to be implemented with the correct volume formula for each shape type.
----------------------------------------------------------------------------------------------------------------------------------------
The Cone Class:
NOTE: You may not change this class in any way EXCEPT for one minor thing you will need to add in order to enable Object I/O.
Cone should implement the Shape interface.
Member Variables:
height: double
radius: double
Constructors:
one constructor which takes values for height and radius and initializes them.
Methods:
toString(): returns a string which includes the type of shape, the radius, and the volume.
----------------------------------------------------------------------------------------------------------------------------------------
The Ellipsoid Class:
NOTE: You may not change this class in any way EXCEPT for one minor thing you will need to add in order to enable Object I/O.
Ellipsoid should implement the Shape interface.
Member Variables:
radius1: double
radius2: double
radius3: double
Constructors:
one constructor which takes values for radius1, radius2 and radius3 and initializes them.
Methods:
toString(): returns a string which includes the type of shape, the three radii, and the volume.
----------------------------------------------------------------------------------------------------------------------------------------
The Sphere Class:
NOTE: You may not change this class in any way EXCEPT for one minor thing you will need to add in order to enable Object I/O.
Sphere should implement the Shape interface.
Member Variables:
radius: double
Constructors:
one constructor which takes a value for radius1 and initializes it.
Methods:
toString(): returns a string which includes the type of shape, the radius, and the volume.
----------------------------------------------------------------------------------------------------------------------------------------
The ObjectSaver Class:
The ObjectSaver class must be implemented as a Generic Class which can work with any type of object. The point of this class is to allow you to easily read and write objects to and from a given file.
NOTE: You may NOT overload any of the methods in this class. You are only allowed to write each method once, and the methods should work with any type of object. You also may not use Object or Shape as a concrete parameter. You must use generics to implement this class.
Member Variables:
file: File
Constructors:
one constructor which takes a File object which is the file to read/write to/from
Methods:readOneObject():
takes an integer as a parameter, this integer is the specific object in the file that you want to read. the first object in the file is 1, the second is 2 and so on.
reads / loads the requested object and returns this object.
this method should also throw an IOException if the parameter is outside the range of possible object positions in the file.
readAllObjects():
returns an ArrayList of Generic Type which holds all the objects read from the file.
writeOneObject():
takes a generic class type as a parameter.
takes a boolean value as a parameter. if the value is true append to the end of the file, if false, overwrite the old contents of the file.
writes the object to the File.
this method should also throw a NotSerializableException.
writeAllObjects():
takes an ArrayList of any type as a parameter.
takes a boolean value as a parameter. if the value is true append to the end of the file, if false, overwrite the old contents of the file.
writes all the objects in the ArrayList to the File specified in the constructor.
this method should also throw a NotSerializableException.
----------------------------------------------------------------------------------------------------------------------------------------
The ShapeUtilities Class
This class has three static utility methods in it which should all be Generic Methods.
recursiveSort():
This method takes an ArrayList of Bounded Generic type and sorts it based on the volumes of the objects using the following algorithm. The bound should only allow any Shape object and subclasses. NOTE: You must follow the algorithm exactly, do not deviate.
Algorithm: (the part below is the recursive hit and min(), max() requirement)
min():
This method takes an ArrayList of Bounded Generic Type which only allows Shape objects and its subclasses.
The method should return the object with the minimum volume from the list of objects.
max():
This method takes an ArrayList of Bounded Generic Type which only allows Shape objects and its subclasses.
The method should return the object with the maximum volume from the list of objects.
----------------------------------------------------------------------------------------------------------------------------------------
The Main Class
Test all of your classes in the Main class. You must demonstrate that everything works correctly.
Use your ShapeSaver class to read each input file and create three ArrayLists, one per file.
Display all of the objects in each ArrayList.
Display the mininum and maximum objects of each ArrayList using the methods in your ShapeUtilities class.
Sort each ArrayList using the sorting method from your ShapeUtilities class.
Write the sorted ArrayLists to three output files using your ShapeSaver class.
Use these output files as input files to create three new ArrayLists and then display these ArrayLists.
Solution
public interface Shape {
double getVolume();
}
public class Cone implements Shape {
private double height;
private double radius;
public Cone(double height, double radius) {
this.height = height;
this.radius = radius;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public String toString() {
return \"Cone [height=\" + height + \", radius=\" + radius
+ \"the volume of a cone is :\" + getVolume() + \"]\";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(height);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(radius);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Cone other = (Cone) obj;
if (Double.doubleToLongBits(height) != Double
.doubleToLongBits(other.height))
return false;
if (Double.doubleToLongBits(radius) != Double
.doubleToLongBits(other.radius))
return false;
return true;
}
@Override
public double getVolume() {
double height = getHeight();
double radius = getRadius();
double volume = ((1 / 3) * 3.14 * radius * radius * height);
return volume;
}
}
public class Ellipsoid implements Shape {
private double radius1;
private double radius2;
private double radius3;
public double getRadius1() {
return radius1;
}
public void setRadius1(double radius1) {
this.radius1 = radius1;
}
public double getRadius2() {
return radius2;
}
public void setRadius2(double radius2) {
this.radius2 = radius2;
}
public double getRadius3() {
return radius3;
}
public void setRadius3(double radius3) {
this.radius3 = radius3;
}
public Ellipsoid(double radius1, double radius2, double radius3) {
super();
this.radius1 = radius1;
this.radius2 = radius2;
this.radius3 = radius3;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(radius1);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(radius2);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(radius3);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Ellipsoid other = (Ellipsoid) obj;
if (Double.doubleToLongBits(radius1) != Double
.doubleToLongBits(other.radius1))
return false;
if (Double.doubleToLongBits(radius2) != Double
.doubleToLongBits(other.radius2))
return false;
if (Double.doubleToLongBits(radius3) != Double
.doubleToLongBits(other.radius3))
return false;
return true;
}
@Override
public String toString() {
return \"Ellipsoid [radius1=\" + radius1 + \", radius2=\" + radius2
+ \", radius3=\" + radius3 + \"the volume ofa ellipsoid is :\"
+ getVolume() + \"]\";
}
@Override
public double getVolume() {
double volume = ((4 / 3) * 3.14 * radius1 * radius2 * radius3);
return volume;
}
}
public class Sphere implements Shape {
private double radius;
public Sphere(double radius) {
this.radius = radius;
}
@Override
public String toString() {
return \"Sphere [radius=\" + radius + \"the volume of sphere is:\"
+ getVolume() + \"]\";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(radius);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Sphere other = (Sphere) obj;
if (Double.doubleToLongBits(radius) != Double
.doubleToLongBits(other.radius))
return false;
return true;
}
@Override
public double getVolume() {
double volume = ((4 / 3) * 3.14 * radius * radius * radius);
return volume;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class ObjectSaver {
protected File file;
List<Object> list;
public ObjectSaver(File file) {
this.file = file;
}
public void readOneObject(int param) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
int count = 0;
String line = \"\";
while ((line = reader.readLine()) != null) {
count++;
if (param == count) {
list = new ArrayList<Object>();
list.add(line);
}
}
if (param > count) {
throw new IOException(
\"you passed position objet exceed the file size\");
}
}
public void readAllObjects() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
int count = 0;
String line = \"\";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
public boolean writeObject(Shape obj, boolean flag)
throws NotSerializableException, FileNotFoundException {
PrintWriter writer = new PrintWriter(file);
writer.write(obj.toString());
writer.flush();
return true;
}
public boolean writeAllObjects(List<Object> list, boolean flag)
throws FileNotFoundException
{
PrintWriter writer = new PrintWriter(file);
for (Object obj : list) {
writer.write(obj.toString());
writer.flush();
}
return true;
}
}







