prestige

Lab4, 6.1 - Main

Dec 25th, 2013
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. MyVector<Yacht> vec;
  2. vec = new MyVector<Yacht>();
  3. try {
  4. vec.add(new MotorYacht("Luisa", 100.00, "DS-100"));
  5. vec.add(new MotorYacht("Luisa", -100.00, "DS-100"));
  6. } catch (MyPriceException e) {
  7. System.out.println(e);
  8. }
  9.  
  10. try {
  11. System.out.println(vec.getItemAt(vec.getCount()));
  12. } catch (ArrayIndexOutOfBoundsException e) {
  13. System.out.println("Invalid index");
  14. }
  15.  
  16. try {
  17. vec.add(new MotorYacht("Adel", 25.00, "DS-100"));
  18. vec.add(new SallingYacht("Mariya", 1.00, "Wave"));
  19. vec.add(new MotorYacht("Tanya", 13.00, "PG-13"));
  20. } catch (MyPriceException e1) {
  21. // TODO Auto-generated catch block
  22. e1.printStackTrace();
  23. }
  24.  
  25. vec.sort(new SortByPrice(), 0, vec.getCount() - 1);
  26.  
  27. MyVector<Yacht> newVec = (MyVector<Yacht>)vec.clone();
  28. System.out.println("Simple iterated print cloned container");
  29. System.out.println(newVec.toString());
  30. try {
  31. vec.getItemAt(0).setPriceOfYacht(2);
  32. } catch (MyPriceException e1) {
  33. // TODO Auto-generated catch block
  34. e1.printStackTrace();
  35. }
  36. System.out.println("Partly printed container");
  37. for (Enumeration e = vec.elements(0, 15); e.hasMoreElements();) {
  38. System.out.println(e.nextElement());
  39. }
  40. System.out.println("Reverse printed container");
  41. for (Enumeration e = vec.elements(); e.hasMoreElements();) {
  42. System.out.println(e.nextElement());
  43. }
  44. ObjectOutputStream os = null;
  45. try {
  46. os = new ObjectOutputStream(new FileOutputStream("D:\\University\\5_course\\Java\\Container.dat"));
  47. os.writeObject(vec);
  48. } catch (FileNotFoundException e) {
  49. System.out.println(e);
  50. } finally {
  51. os.flush();
  52. os.close();
  53. }
  54. ObjectInputStream ins = null;
  55. MyVector<Yacht> serVec = null;
  56. try {
  57. ins = new ObjectInputStream(new FileInputStream("D:\\University\\5_course\\Java\\Container.dat"));
  58. serVec = (MyVector<Yacht>)ins.readObject();
  59. } catch (FileNotFoundException e) {
  60. System.out.println(e);
  61. } finally {
  62. ins.close();
  63. }
  64. System.out.println("Simple iterated print container");
  65. System.out.println(vec.toString());
  66. System.out.println("Serialized iterated print container");
  67. System.out.println(serVec.toString());
  68. System.out.print("Avarage cost is: ");
  69. System.out.println(Lab.avarageCostOfYacht(vec));
Advertisement
Add Comment
Please, Sign In to add comment