Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this is my code.... when I try to run it... the error message plz help me fix the problem... ----------------------------------------------------------------------------- java.io.InvalidClassException: Sphere; local class incompatible:

this is my code.... when I try to run it... the error message

plz help me fix the problem...

-----------------------------------------------------------------------------

java.io.InvalidClassException: Sphere; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 7497003029646850194 at java.io.ObjectStreamClass.initNonProxy(Unknown Source) at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source) at java.io.ObjectInputStream.readClassDesc(Unknown Source) at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source) at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.readObject(Unknown Source) at ObjectSaver.readAllObjects(ObjectSaver.java:46) at Main.main(Main.java:77) Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:78)

-----------------------------------------------------------------------------------

Main.java

public class Main {

public static void main(String arg[]){ Shape cone = new Cone(20,10); Shape ellipsoid = new Ellipsoid(5,10,15); Shape sphere = new Sphere(30); ObjectSaver coneFile = new ObjectSaver<>( new File("cones.dat")); // should accpet any data file? .txt ObjectSaver ellipsoidFile = new ObjectSaver<>( new File("ellipsoids.dat")); ObjectSaver sphereFile = new ObjectSaver<>( new File("spheres.dat")); System.out.println( " write file test.............. "); try{ // cone System.out.println("Cone ->" + cone); if(coneFile.writeOneObject(cone, false)) { System.out.println("written to file."); } // ellipsoid System.out.println("Ellipsoid ->" + ellipsoid); if(ellipsoidFile.writeOneObject(ellipsoid, false)){ System.out.println("written to file."); } // sphere System.out.println("Sphere ->" + sphere); if(ellipsoidFile.writeOneObject(sphere, false)){ System.out.println("written to file."); } }catch(NotSerializableException e){ e.printStackTrace(); } // some bug... not solved yet System.out.println( " read file test.............. "); //cone ArrayList listFromFile = coneFile.readAllObjects(); Iterator iterator = listFromFile.iterator(); System.out.println(" cone file: "); while (iterator.hasNext()){ System.out.println(iterator.next()); } //ellipsoid // problem listFromFile = ellipsoidFile.readAllObjects(); iterator = listFromFile.iterator(); System.out.println(" elliposid file: "); while (iterator.hasNext()){ System.out.println(iterator.next()); } //sphere // problem listFromFile = sphereFile.readAllObjects(); iterator = listFromFile.iterator(); System.out.println(" sphere file: "); while (iterator.hasNext()){ System.out.println(iterator.next()); } ArrayList listOfShapes = new ArrayList<>(); // add listOfShapes.add(cone); listOfShapes.add(ellipsoid); listOfShapes.add(sphere); System.out.println(" Min V is: " + ShapeUtilities.min(listOfShapes)); System.out.println(" Max V is: " + ShapeUtilities.max(listOfShapes)); iterator = listOfShapes.iterator(); System.out.println("Sorted list is: "); while(iterator.hasNext()){ System.out.println(iterator.next()); } } }

--------------------------------------------------------------

ObjectSaver.java

public class ObjectSaver {

private File file; public ObjectSaver(File file){ this.file = file; } // readOneObject // this method should also throw an IOException if the parameter is outside the range of possible object positions in the file. public T readOneObject(int index) throws IOException{ ArrayList result = readAllObjects(); // find object index and return back to function if((result == null) || (result.size() >= index)){ // if cannot read one, then return outside range throw new IOException("parameter is outside the range"); } else return result.get(index); } // readAllObjects // returns an ArrayList of Generic Type which holds all the objects read from the file. @SuppressWarnings("unchecked") public ArrayList readAllObjects(){ ArrayList result; FileInputStream fileInputStream = null; // declare to null ObjectInputStream objectInputStream = null; try{ fileInputStream = new FileInputStream(file); objectInputStream = new ObjectInputStream(fileInputStream); result = (ArrayList) objectInputStream.readObject(); return result; // // catch (FileNotFoundException e){e.printStackTrace();} // catch (IOException e){} // catch (Exception e){} }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); }catch(ClassNotFoundException e){ e.printStackTrace(); }finally{ // finally block use to exit try block

if(fileInputStream != null){ // inputsteam -> close try{ fileInputStream.close(); }catch(IOException e){ e.printStackTrace(); } } else if(objectInputStream != null){ try{ objectInputStream.close(); }catch(IOException e){ e.printStackTrace(); } } } return null; } // 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. public boolean writeOneObject(T object, boolean append) throws NotSerializableException{ ArrayList fileContentWritten; if(append){ fileContentWritten = readAllObjects(); } else{ fileContentWritten = new ArrayList(); } fileContentWritten.add(object); writeAllObjects(fileContentWritten, append); return true; } // 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. public boolean writeAllObjects(ArrayList objectWritten, boolean append) throws NotSerializableException{ FileOutputStream fileOutputStream = null; //// declare to null ObjectOutputStream objectOutputStream = null; try{ fileOutputStream = new FileOutputStream(file); objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(objectWritten); }catch (FileNotFoundException e){ e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); }finally{ // finally block use to exit try block // https://docs.oracle.com/javase/ial/essential/exceptions/finally.html if(fileOutputStream != null){ // inputsteam -> close try{ fileOutputStream.close(); }catch(IOException e){ e.printStackTrace(); } } else if(objectOutputStream != null){ try{ objectOutputStream.close(); }catch(IOException e){ e.printStackTrace(); } } } // readallObjects & writeAllObjects // input & output return true; } }

------------------------------------------------------------------------

ShapeUtilities.java

public class ShapeUtilities { // 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. public static ArrayList recursiveSort(ArrayList list){ if(list != null && list.size() <= 1){ return list; // if the list size <= 1 //return list } else{ // else select a middle element from the list and remove it int mid = list.size()/2; double midVolume = list.get(mid).getVolume(); // creat 2 lists leftlist and rightlist ArrayList leftList = new ArrayList<>(); ArrayList rightList = new ArrayList<>(); Iterator iterator = list.iterator(); int i = 0; // if element is less than the middle element then add element to the right // else add to left while(iterator.hasNext()){ // object next() return next element T current = iterator.next(); if (i != mid) { if (current.getVolume() < midVolume){ rightList.add(current); } else{ leftList.add(current); } } i++; // till string end; } leftList = recursiveSort(leftList); rightList = recursiveSort(rightList); ArrayList result = new ArrayList<>(); result.add(list.get(mid)); if(leftList.size()>0){ // add result to leftlist leftList.addAll(result); result = leftList; } else if(rightList.size()>0){ // else do add on rightlist result.addAll(rightList); } return result; // return the combination of recursiveSort } } // 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 maximum volume from the list of objects. public static T min(ArrayList list){ if(list != null && list.size() != 0){ Iterator iterator = list.iterator(); T minVolumeShape = list.get(0); while(iterator.hasNext()){ T currentShape = iterator.next(); if(currentShape.getVolume() < minVolumeShape.getVolume()){ minVolumeShape = currentShape; } } // return the minimum volume from the list of obj return minVolumeShape; } else return null; }// // 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 minimum volume from the list of objects. public static T max(ArrayList list){ if (list != null && list.size() != 0) { Iterator iterator = list.iterator(); T maxVolumeShape = list.get(0); while (iterator.hasNext()) { T currentShape = iterator.next(); if (currentShape.getVolume() > maxVolumeShape.getVolume()) { maxVolumeShape = currentShape; } } // return the maximum volume from the list of obj return maxVolumeShape; } else return null; }// same as min }

-----------------------------------------------------------------------------

Shape.java

public interface Shape {

public double getVolume(); }

--------------------------------------------

Sphere.java

public class Sphere implements Shape, Serializable{ private double radius; public Sphere(double radius){ this.radius =radius; } @Override //getVolume public double getVolume(){ return 3.14 * radius * radius * radius * 4 / 3 ; } public String toString(){ return "Type of shape: Sphere, Radius is: " + radius + ", Volume is: " + getVolume(); } // returns a string which includes the type of shape, the radius, and the volume.

}

-----------------------------------------------------------------------------

Ellipsoid.java

public class Ellipsoid implements Shape, Serializable{ private double radius1; private double radius2; private double radius3; public Ellipsoid(double radius1, double radius2, double radius3){ this.radius1 = radius1; this.radius2 = radius2; this.radius3 = radius3; }

@Override //getVolume public double getVolume(){ return 3.14 * radius1 * radius2 * radius3 * 4 / 3; } public String toString(){ return "Type of shape: Ellipsoid, Radius 1 is: " + radius1 + " Radius 2 is: " + radius2 + " Radius 3 is: " + radius3 + ", Volume is: " + getVolume(); } // returns a string which includes the type of shape, the radius, and the volume. }

---------------------------------------------------------

Cone.java

public class Cone implements Shape, Serializable{ private double height; private double radius; public Cone(double height, double radius){ this.height = height; this.radius = radius; } @Override //getVolume public double getVolume(){ return 3.14 * radius * radius * height / 3 ; // this one works // 1/3 * 3.14 * r * r *h doesnt work ??? } public String toString(){ return "Type of shape: Cone, Radius is: " + radius + ", Volume is: " + getVolume(); } // returns a string which includes the type of shape, the radius, and the volume.

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory And Application Bio Science And Bio Technology International Conferences DTA And BSBT 2011 Held As Part Of The Future Generation In Computer And Information Science 258

Authors: Tai-hoon Kim ,Hojjat Adeli ,Alfredo Cuzzocrea ,Tughrul Arslan ,Yanchun Zhang ,Jianhua Ma ,Kyo-il Chung ,Siti Mariyam ,Xiaofeng Song

2011th Edition

ISBN: 3642271561, 978-3642271564

More Books

Students also viewed these Databases questions