Advertisement
Luninariel

Linked List - Example

Feb 11th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 39.46 KB | None | 0 0
  1. ShapesAbstractGenericLinkedList5
  2.  
  3. 1 /*This is a program demonstrating abstract classes in Java with a Generic Method and a Linked List.
  4. 2 Class Shape is an abstract class.
  5. 3 From it class Point inherits a location. From class Point, the classes Flat and ThreeD are built.
  6. 4 Class Flat impliments a Comparable for its subclasses of Square, Circle and Rectangle. ThreeD
  7. 5 also impliments a Comparable for its subclasses of Cube,Sphere and Cylindar.
  8. 6
  9. 7 Class ShapeManager stores shapes and sorts them. However it can use any type of object. As long as the
  10. 8 method supports the CompareTo function. We will demonstrate it with objects from flats, solids and
  11. 9 integers and doubles.
  12. 10
  13. 11 Class MyLinkManager also stores shapes in a linked list. Again it can store anytype of Object.
  14. 12 this will also be demonstrated with flats, solids, integers and doubles.
  15. 13
  16. 14 Finally LinkManagerwithJavaListClass will create a linked list using java.util.LinkedList<T> class
  17. 15 */
  18. 16 import java.io.*;
  19. 17 import java.io.IOException;
  20. 18 import javax.swing.*;
  21. 19 import java.util.*;
  22. 20 import java.io.File;
  23. 21 import java.io.FileNotFoundException;
  24. 22 import java.lang.*;
  25. 23 import java.util.NoSuchElementException;
  26. 24
  27. 25
  28. 26 public class ShapesAbstractGenericLinkedList5
  29. 27
  30. 28
  31. 29 {
  32. 30 public static void main(String[] args)throws Exception
  33. 31 {
  34. 32
  35. 33 PrintWriter outpt;
  36. 34 // now equate the internal name to an external file through the PrintWriter
  37. 35
  38. 36 outpt=new PrintWriter(new File("JavaAbsOut3.txt"));
  39. 37 int i, num;
  40. 38 //Create a point
  41. 39 Point Pt=new Point(5,8,outpt);
  42. 40 //now print the point
  43. 41 Pt.print(outpt);
  44. 42 //Now Create a circle and a square
  45. 43 Circle Cir=new Circle(5,8,outpt,5);
  46. 44 Square Sqr=new Square(5,8,outpt,5);
  47. 45 Rectangle Rec=new Rectangle(5,8,outpt,5,4);
  48. 46 Circle Cir2=new Circle(5,8,outpt,27);
  49. 47 Square Sqr2=new Square(6,7,outpt,4);
  50. 48 Rectangle Rec2=new Rectangle(5,10,outpt,8,3);
  51. 49 // Now print the circle and the Square
  52. 50 Sqr.CalcArea();
  53. 51 Cir.CalcArea();
  54. 52 Cir.print(outpt);
  55. 53 Sqr.print(outpt);
  56. 54 /*now create an array of Flats so we can do a comparable and then we'll sort m. Note we cannot create an array
  57. 55 of Shapes since it is an Abstract class */
  58. 56 Flat [] flatshp=new Flat[6];
  59. 57 flatshp[0]=Sqr;
  60. 58 flatshp[1]=Cir;
  61. 59 flatshp[2]=Rec;
  62. 60 flatshp[3]=Sqr2;
  63. 61 flatshp[4]=Cir2;
  64. 62 flatshp[5]=Rec2;
  65. 63 //let's be sure all areas are calculated
  66. 64
  67. 65 for(i=0;i<=5;i++)flatshp[i].CalcArea();
  68. 66 //now let's printm again
  69. 67 for(i=0;i<=5;i++)flatshp[i].print(outpt);
  70. 68
  71. 69 // Now let's make some solids
  72. 70 Sphere Ball=new Sphere(5,6,outpt,4);
  73. 71 Brick Gold=new Brick(7,9,outpt,5,6,8);
  74. 72 Cube Rubix=new Cube(4,5,outpt,9);
  75. 73 // Now print them
  76. 74 System.out.println("these are solids");
  77. 75 outpt.println("these are solids");
  78. 76 Ball.CalcArea();
  79. 77 Ball.CalcVolumn();
  80. 78 Gold.CalcArea();
  81. 79 Gold.CalcVolumn();
  82. 80 Rubix.CalcArea();
  83. 81 Rubix.CalcVolumn();
  84. 82 Ball.print(outpt);
  85. 83 Gold.print(outpt);
  86. 84 Rubix.print(outpt);
  87. 85
  88. 86 // Now lets sort them
  89. 87 Solid []soldshp=new Solid[3];
  90. 88 soldshp[0]=Ball;
  91. 89 soldshp[1]=Gold;
  92. 90 soldshp[2]=Rubix;
  93. 91
  94. 92
  95. 93 //Now let's setup a generic class
  96. 94 GenericManager<Integer> myints=new GenericManager<Integer>();
  97. 95 //now lets add 5 integer values
  98. 96 int val=25;
  99. 97 for (i=1;i<=5;i++)myints.setvalue(val+i);
  100. 98 //now lets read them back and print them
  101. 99 System.out.println("These are from myints");
  102. 100 outpt.println("These are from myints");
  103. 101 System.out.println("myints on outpt");
  104. 102
  105. 103 for(i=0;i<=4;i++)
  106. 104 {System.out.println(myints.getvalue(i));
  107. 105 outpt.println(myints.getvalue(i));
  108. 106 }
  109. 107 //now let's sort the ints
  110. 108 myints.ManageAndSort();
  111. 109 System.out.println("The manager has sorted ints");
  112. 110 // outpt.println{"The manager has sorted ints");
  113. 111
  114. 112 for(i=0;i<=4;i++)
  115. 113 {System.out.println(myints.getvalue(i));
  116. 114 // outpt.println(myints.getvalue(i));
  117. 115 }
  118. 116
  119. 117 //now let's do some solids
  120. 118 GenericManager<Solid> mysolids=new GenericManager<Solid>();
  121. 119 //now let's store the solids
  122. 120 for(i=0;i<=2;i++)mysolids.setvalue(soldshp[i]);
  123. 121 //now let's read them back and print them
  124. 122 System.out.println("These are from mysolids");
  125. 123 outpt.println("These are from mysolids");
  126. 124 for(i=0;i<=2;i++){
  127. 125 (mysolids.getvalue(i)).print(outpt);
  128. 126 }
  129. 127 System.out.println("These are from the new Generic Manager using the generic sort");
  130. 128 outpt.println("These are from the new Generic Manager using the generic sort");
  131. 129 // sort the solids
  132. 130 mysolids.ManageAndSort();
  133. 131
  134. 132 for(i=0;i<=2;i++){
  135. 133 (mysolids.getvalue(i)).print(outpt);
  136. 134 }
  137. 135 System.out.println("This is the end of the new Generic Manager Test");
  138. 136
  139. 137 System.out.println("Now let's do some linked list work with MyLinkManager");
  140. 138 outpt.println("Now let's do some linked list work with MyLinkManager");
  141. 139 // first let's try integers
  142. 140 MyLinkManager<Integer> linkint=new MyLinkManager<Integer>();//See lines 499 to 611
  143. 141 linkint.addnode(34);//See lines 522 to 547
  144. 142 linkint.addnode(17);
  145. 143 linkint.addnode(67);
  146. 144 linkint.addnode(-18);
  147. 145 linkint.addnode(25);
  148. 146 System.out.println("This is from MyLinkManager");
  149. 147 outpt.println("This is from MyLinkManager");
  150. 148 // get the entry of numbers in the linkint
  151. 149 num=linkint.getnumber();
  152. 150 for (i=1;i<=num;i++)
  153. 151 {System.out.println("from linkint"+linkint.getnode(i));
  154. 152 outpt.println("from linkint"+linkint.getnode(i));
  155. 153 }
  156. 154 // now let's try some flats we'll use flatshap array
  157. 155 MyLinkManager<Flat> linkflat=new MyLinkManager<Flat>();
  158. 156 linkflat.addnode(flatshp[3]);
  159. 157 linkflat.addnode(flatshp[0]);
  160. 158 linkflat.addnode(flatshp[1]);
  161. 159 linkflat.addnode(flatshp[2]);
  162. 160 linkflat.addnode(flatshp[4]);
  163. 161 linkflat.addnode(flatshp[5]);
  164. 162 System.out.println("This is from MyLinkManager with flats");
  165. 163 outpt.println("This is from MyLinkManager with flats");
  166. 164 num=linkint.getnumber();
  167. 165 for (i=1;i<=num;i++)
  168. 166 {System.out.println("from linkflat"+i);
  169. 167 outpt.println("from linkflat"+1);
  170. 168 (linkflat.getnode(i)).print(outpt);
  171. 169 }
  172. 170 // now lets try to add them in order
  173. 171 System.out.println("Now let's do some linked list work with MyLinkManager in order");
  174. 172 outpt.println("Now let's do some linked list work with MyLinkManager in order");
  175. 173 // first let's try integers
  176. 174 MyLinkManager<Integer> linkintorder=new MyLinkManager<Integer>();//See Lines 499-611
  177. 175 linkintorder.addinorder(34);//see lines 564-609
  178. 176 linkintorder.addinorder(17);
  179. 177 linkintorder.addinorder(67);
  180. 178 linkintorder.addinorder(-18);
  181. 179 linkintorder.addinorder(25);
  182. 180 System.out.println("This is from MyLinkManager in order");
  183. 181 outpt.println("This is from MyLinkManager in order");
  184. 182 // get the entry of numbers in the linkint
  185. 183 num=linkint.getnumber();
  186. 184 for (i=1;i<=num;i++)
  187. 185 {System.out.println("from linkinorder"+linkintorder.getnode(i));
  188. 186 outpt.println("from linkinorder"+linkintorder.getnode(i));
  189. 187 }
  190. 188 // now let's try some flats we'll use flatshap array
  191. 189 MyLinkManager<Flat> linkflatord=new MyLinkManager<Flat>();
  192. 190 linkflatord.addinorder(flatshp[3]);
  193. 191 linkflatord.addinorder(flatshp[0]);
  194. 192 linkflatord.addinorder(flatshp[1]);
  195. 193 linkflatord.addinorder(flatshp[2]);
  196. 194 linkflatord.addinorder(flatshp[4]);
  197. 195 linkflatord.addinorder(flatshp[5]);
  198. 196 System.out.println("This is from MyLinkManager with flats in order");
  199. 197 outpt.println("This is from MyLinkManager with flats in order");
  200. 198 num=linkflatord.getnumber();
  201. 199 for (i=1;i<=num;i++)
  202. 200 {System.out.println("from linkflatord"+i);
  203. 201 (linkflatord.getnode(i)).print(outpt);
  204. 202
  205. 203 outpt.println("from linkflaordt"+i);
  206. 204 }
  207. 205 LinkManagerwithJavaListClass<Integer> javalinklst=new LinkManagerwithJavaListClass<Integer>();See lines 612-701
  208. 206 javalinklst.addnode(34);
  209. 207 javalinklst.addnode(17);
  210. 208 javalinklst.addnode(67);
  211. 209 javalinklst.addnode(-18);
  212. 210 javalinklst.addnode(25);
  213. 211 System.out.println("This is from LinkManagerwithJavaListClass with ints out of order");
  214. 212 outpt.println("This is from LinkManagerwithJavaListClass with ints out of order");
  215. 213 num=javalinklst.getnumber();
  216. 214 for(i=0;i<=num-1;i++)
  217. 215 {System.out.println("from javalinklst"+i);
  218. 216 outpt.println("from javalinklst"+i);
  219. 217 System.out.println(javalinklst.getnode(i));
  220. 218 outpt.println(javalinklst.getnode(i));
  221. 219 }
  222. 220 // Now let's use the linkinorder with javalinklst
  223. 221 LinkManagerwithJavaListClass<Integer> javalinklst2=new LinkManagerwithJavaListClass<Integer>();
  224. 222 javalinklst2.addinorder(34);see lines 647-699
  225. 223 javalinklst2.addinorder(17);
  226. 224 javalinklst2.addinorder(67);
  227. 225 javalinklst2.addinorder(-18);
  228. 226 javalinklst2.addinorder(25);
  229. 227 System.out.println("This is from LinkManagerwithJavaListClass ints using addinorder");
  230. 228 outpt.println("This is from LinkManagerwithJavaListClass ints using addinorder");
  231. 229 for(i=0;i<=num-1;i++)
  232. 230 {System.out.println("from javalinklst2"+i);
  233. 231 outpt.println("from javalinklst2"+i);
  234. 232 System.out.println(javalinklst2.getnode(i));
  235. 233 outpt.println(javalinklst2.getnode(i));
  236. 234 }
  237. 235
  238. 236 // Now let's try some flats using the javelinklst
  239. 237 LinkManagerwithJavaListClass<Flat> javalinkflats=new LinkManagerwithJavaListClass<Flat>();
  240. 238 javalinkflats.addinorder(flatshp[3]);
  241. 239 javalinkflats.addinorder(flatshp[0]);
  242. 240 javalinkflats.addinorder(flatshp[1]);
  243. 241 javalinkflats.addinorder(flatshp[2]);
  244. 242 javalinkflats.addinorder(flatshp[4]);
  245. 243 javalinkflats.addinorder(flatshp[5]);
  246. 244 System.out.println("This is from LinkManagerwithJavaListClass with flats in order");
  247. 245 outpt.println("This is from LinkManagerwithJavaListClass with flats in order");
  248. 246 num=javalinkflats.getnumber();
  249. 247 for (i=0;i<=num-0;i++)
  250. 248 {System.out.println("from javalinkflats"+i);
  251. 249 (linkflatord.getnode(i)).print(outpt);
  252. 250
  253. 251 outpt.println("from javalinkflats"+i);
  254. 252 }
  255. 253
  256. 254
  257. 255
  258. 256
  259. 257
  260. 258 outpt.close();
  261. 259 }//end of main
  262. 260 }//ShapesAbstractGenericLinkedList5
  263. 261
  264. 262
  265. 263
  266. 264
  267. 265
  268. 266
  269. 267 // Define the Shape class
  270. 268 abstract class Shape{
  271. 269 //now define the abstract methods
  272. 270 //NO CONSTRUCTOR SINCE ABSTRACT CLASS CANNOT BE INSTANTIATED
  273. 271 public abstract double getArea();
  274. 272 public abstract String getName();
  275. 273
  276. 274 }// End of Shape
  277. 275 // Now create Point
  278. 276 class Point extends Shape implements Comparable {
  279. 277 /* Point is the parent of Flat and Solid. These derived classes are compared based
  280. 278 on their areas and there must be only one comparison. Hence the comparison must exist at
  281. 279 blass level above Flat and Solid so their areas can be compared. */
  282. 280 protected int x;
  283. 281 protected int y;
  284. 282 //Create the constructor for point
  285. 283 public Point(int xp, int yp, PrintWriter outf)
  286. 284 { x=xp;
  287. 285 y=yp;
  288. 286 }// end of Point constructor
  289. 287 // Now create the compareTo function
  290. 288
  291. 289 //now impliment the compareTo function
  292. 290 public int compareTo(Object o){
  293. 291 if (getArea()>((Point)o).getArea()) return 1;
  294. 292 else
  295. 293 if(getArea()<((Point)o).getArea())return -1;
  296. 294 else return 0;
  297. 295 }// end of the compareTo function for Point
  298. 296 public void setX(int x){return;}
  299. 297 public void setY(int y){return;}
  300. 298 public int getX(){return x;};
  301. 299 public int getY(){return y;};
  302. 300 public String getName(){return "Point";}
  303. 301 public double getArea(){return 0.0;};//Got to have this for the compareTo
  304. 302 public void print(PrintWriter outf)
  305. 303 {outf.println("Point X "+x+" Y "+y);
  306. 304 System.out.println("Point X "+x+" Y "+y);
  307. 305 return;
  308. 306 }
  309. 307 }// End of Point
  310. 308 class Flat extends Point
  311. 309 {/* Flat is the parent of Square, Circle and Rectangle. These derived classes are
  312. 310 compared based on their areas and can only be compared at the Flat level.*/
  313. 311 protected double area;// all flats have an area
  314. 312 // Now create the constructor for flat.
  315. 313
  316. 314 public Flat(int x, int y, PrintWriter outf){
  317. 315 super(x,y, outf);
  318. 316 }
  319. 317 public double getArea(){return area;}//we need this because we will use a getArea from the lower group
  320. 318 public void CalcArea(){return;}//we need this to calculate the area of others subs public void print (PrintWriter outf){return;}//we need to print others
  321. 319 public String getName(){return "Flat";}//again others
  322. 320 }// end of Flat class
  323. 321 // now let's create some Squares, Circles and Rectangles
  324. 322 class Square extends Flat{
  325. 323 protected int side;
  326. 324 //now the constructor for Square
  327. 325 public Square(int x, int y, PrintWriter outf,int side)
  328. 326 { super (x,y,outf);
  329. 327 this.side=side;}
  330. 328 public void CalcArea(){area=side*side;}
  331. 329 public double getArea(){return area;}
  332. 330 public String getName() {return " Square ";}
  333. 331 public void print(PrintWriter outf)
  334. 332 {outf.println("Square with center X "+x+" Y "+y+ "Side "+side);
  335. 333 System.out.println("Square with center X "+x+" Y "+y+" Side "+side);
  336. 334 outf.println(" Area "+area);
  337. 335 System.out.println("Area "+area);
  338. 336 }
  339. 337 }// end of Square
  340. 338 // now the constructor for Circle
  341. 339 class Circle extends Flat{
  342. 340 protected int radius;
  343. 341 // now the constructor for Circle
  344. 342 public Circle (int x, int y, PrintWriter outf, int radius)
  345. 343 { super(x,y,outf);
  346. 344 this.radius=radius;}
  347. 345 public void CalcArea(){area=3.1416*radius*radius;}
  348. 346 public double getArea(){return area;}
  349. 347 public String getName() {return " Circle ";}
  350. 348 public void print(PrintWriter outf)
  351. 349 {outf.println("Circle with center X "+x+" Y "+y+ "Radius "+radius);
  352. 350 System.out.println("Circle with center X "+x+" Y "+y+" Radius "+radius);
  353. 351 outf.println("\n Area "+area);
  354. 352 System.out.println("Area "+area);
  355. 353 }
  356. 354 }//end of Circle
  357. 355 class Rectangle extends Flat{
  358. 356 protected int length;
  359. 357 protected int width;
  360. 358 //now the constructor for Rectangle
  361. 359 public Rectangle(int x, int y, PrintWriter outf,int length, int width)
  362. 360 { super (x,y,outf);
  363. 361 this.length=length;
  364. 362 this. width=width;}
  365. 363 public void CalcArea(){area=length*width;}
  366. 364 public double getArea(){return area;}
  367. 365 public String getName() {return " Rectangle ";}
  368. 366 public void print(PrintWriter outf)
  369. 367 {outf.println("Rectangle with center X "+x+" Y "+y+ "Length"+length+"width"+width);
  370. 368 System.out.println("Square with center X "+x+" Y "+y+" Length "+length+"width "+width);
  371. 369 outf.println(" Area "+area);
  372. 370 System.out.println("Area "+area);
  373. 371 }
  374. 372 }// end of Rectangle
  375. 373 class Solid extends Point{
  376. 374 protected double area;
  377. 375 protected double volumn;
  378. 376 public Solid(int x, int y, PrintWriter outf){
  379. 377 super(x,y, outf);
  380. 378 }
  381. 379 public double getArea(){return area;}//we need this because we will use a getArea from the lower group
  382. 380 public void CalcArea(){return;}// we need this so we can calculate the area of others it is a place holder public void print (PrintWriter outf){return;}//we need to print others
  383. 381 public String getName(){return "Solid";}
  384. 382 public double getVolumn(){return volumn;}
  385. 383 public void CalcVolumn(){return;}//we need tis for calculation of others
  386. 384 }// end of Solid class
  387. 385
  388. 386 class Cube extends Solid{
  389. 387 protected int side;
  390. 388 //now the constructor for Cube
  391. 389 public Cube(int x, int y, PrintWriter outf,int side)
  392. 390 { super (x,y,outf);
  393. 391 this.side=side;}
  394. 392 public void CalcArea(){area=6*side*side;}
  395. 393 public void CalcVolumn(){volumn=side*side*side;}
  396. 394 public String getName() {return " Cube ";}
  397. 395 public void print(PrintWriter outf)
  398. 396 {outf.println("Cube with center X "+x+" Y "+y+ "Side "+side);
  399. 397 System.out.println("Cube with center X "+x+" Y "+y+" Side "+side);
  400. 398 outf.println(" Area "+area);
  401. 399 System.out.println("Area "+area);
  402. 400 outf.println(" Volumn "+volumn);
  403. 401 System.out.println("Volumn "+volumn);
  404. 402
  405. 403 }
  406. 404 }// end of Cube
  407. 405 class Sphere extends Solid{
  408. 406 protected int radius;
  409. 407 // now the constructor for Sphere
  410. 408 public Sphere (int x, int y, PrintWriter outf, int radius)
  411. 409 { super(x,y,outf);
  412. 410 this.radius=radius;}
  413. 411 public void CalcArea(){area=3.1416*2*radius*2*radius;}
  414. 412 public void CalcVolumn(){volumn=4.0/3.0*3.1614*radius*radius*radius;}
  415. 413 public String getName() {return " Sphere ";}
  416. 414 public void print(PrintWriter outf)
  417. 415 {outf.println("Sphere with center X "+x+" Y "+y+ "Radius "+radius);
  418. 416 System.out.println("Sphere with center X "+x+" Y "+y+" Radius "+radius);
  419. 417 outf.println("\n Area "+area);
  420. 418 System.out.println("Area "+area);
  421. 419 outf.println(" Volumn "+volumn);
  422. 420 System.out.println("Volumn "+volumn);
  423. 421
  424. 422
  425. 423 }
  426. 424 }//end of Sphere
  427. 425 class Brick extends Solid{
  428. 426 protected int length;
  429. 427 protected int width;
  430. 428 protected int height;
  431. 429 //now the constructor for Brick
  432. 430 public Brick(int x, int y, PrintWriter outf,int length, int width,int height)
  433. 431 { super (x,y,outf);
  434. 432 this.length=length;
  435. 433 this. width=width;
  436. 434 this.height=height;}
  437. 435 public void CalcArea(){area=2*length*width+2*width*height+2*height*length;}
  438. 436 public void CalcVolumn(){volumn=length*width*height;}
  439. 437 public String getName() {return " Brick ";}
  440. 438 public void print(PrintWriter outf)
  441. 439 {outf.println("Brick with center X "+x+" Y "+y+ "Length"+length+"width"+width+"height"+height);
  442. 440 System.out.println("Brick with center X "+x+" Y "+y+ "Length"+length+"width"+width+"height"+height);
  443. 441 outf.println(" Area "+area);
  444. 442 System.out.println("Area "+area);
  445. 443 outf.println(" volumn"+volumn);
  446. 444 System.out.println("volumn"+volumn);
  447. 445 }
  448. 446 }// end Brick
  449. 447
  450. 448 // Now we build a generic class type.
  451. 449 class GenericManager<T extends Comparable>{// NOTE THAT YOU MUST ADD THIS COMPARABLE TO USE
  452. 450 // COMPARETO FUNCTON THAT COMES ALONG WITH T
  453. 451 protected ArrayList<T> mylist= new ArrayList<T>();
  454. 452 protected int mcount;
  455. 453 public GenericManager()
  456. 454 {// this is the generic constructor
  457. 455 mcount=0;//mcount is the next available value in array myarray
  458. 456
  459. 457 }
  460. 458 public int setvalue(T x)//this places values in myarray
  461. 459 { mylist.add(mcount++,x);
  462. 460 return mcount;}
  463. 461 // public int compareTo(T x){return 1;}
  464. 462
  465. 463 public T getvalue(int i)//this gets values from myarray
  466. 464 { if (i<=mcount)return mylist.get(i);
  467. 465 else
  468. 466 return mylist.get(0);
  469. 467 }//end of getvalue
  470. 468 public void ManageAndSort() {/* This is a generic sort. It will sort anything that the manager manages BUT the objects
  471. 469 being sorted must support the compareTo function*/
  472. 470 //this method will sort an array of Flat objects based on their CompareTo function
  473. 471 T xsave, ysave,a,b;
  474. 472 int isw=1,xlast=mylist.size();
  475. 473 while (isw==1)
  476. 474 {isw=0;
  477. 475 for(int i=0;i<=xlast-2;i++)
  478. 476 {a=mylist.get(i);
  479. 477 b=mylist.get(i+1);
  480. 478 switch (a.compareTo(b))
  481. 479 {
  482. 480 case 1://the objects in array x are in the right order
  483. 481 break;
  484. 482 case -1:// objects out of order, they must be changed.
  485. 483 xsave=mylist.get(i);
  486. 484 ysave=mylist.get(i+1);
  487. 485 mylist.remove(i);
  488. 486 mylist.add(i,ysave);
  489. 487 mylist.remove(i+1);
  490. 488 mylist.add(i+1,xsave);
  491. 489 // mylist.add(i,mylist.get(i+1));
  492. 490 //mylist.add(i+1,xsave);
  493. 491 isw=1;
  494. 492 break;
  495. 493 default://objects are equal no chanbe
  496. 494 }//end of switch
  497. 495 }//end of for
  498. 496 }//end of while
  499. 497 }// ManageandSort
  500. 498 } //end of GenericManager class
  501. 499 class MyLinkManager<T extends Comparable>{
  502. 500 /*This is a class that will store objects with a linked list. The objects will be added to the
  503. 501 list based on their compareTo function. They will be added from larger to smaller.
  504. 502 */
  505. 503 protected MyNode<T> head, tail;//this is the head and tail of the list
  506. 504 protected int number;
  507. 505 public MyLinkManager()
  508. 506 {//this is the constructor for MyLinkManager
  509. 507 MyNode<T> head=null;// set the head and the tail of the linked list to null
  510. 508 MyNode<T> tail=null;
  511. 509 int number=0;//nothing in the list
  512. 510 }// end of the constructor
  513. 511 public int getnumber(){return number;}
  514. 512 private static class MyNode<T>
  515. 513 {// this is an internal class for constructing nodes in MyLinkManager
  516. 514 protected T nodevalue;
  517. 515 protected MyNode<T> next;
  518. 516 public MyNode(T x)
  519. 517 { nodevalue=x;
  520. 518 next=null;//create the pointer to the next node but set it to null
  521. 519 }// end of MyNode constructor
  522. 520 }//end of class MyNode
  523. 521 // Let's start by adding a node to the end of the list. We'll add this with MyLinkManager
  524. 522 public int addnode(T x)
  525. 523 {//this adds a node to the linked list. It will always add to the front of the list
  526. 524 addfront(x);
  527. 525 return number;
  528. 526 }// end of addnode
  529. 527 public void addfront(T x)
  530. 528 {// this adds a node to the front of the list.
  531. 529 // first create the node
  532. 530 MyNode<T> newnode=new MyNode<T>(x);
  533. 531 // now let's put it in the list
  534. 532 if(head==null)
  535. 533 {// this is the first node in the list.
  536. 534 head=newnode;
  537. 535 tail=newnode;
  538. 536 }
  539. 537 else
  540. 538 {// there is already someone in the list
  541. 539 // first make the newnode point to the current one at the front of the list
  542. 540 newnode.next=head;
  543. 541 //now make the front of the list point to newnode
  544. 542 head=newnode;
  545. 543 }
  546. 544 // now update the list by 1
  547. 545 number++;
  548. 546 return;
  549. 547 }// this is the end of addfront(T x)
  550. 548 public T getnode(int x)
  551. 549 {//this function returns the node at the x'th position in the list
  552. 550 // if x is out of bounds of the list a message is printed and an exception is thron
  553. 551 if ((x<0)||(x>number))System.out.println("error in getnode"+x+"while list holds"+number);
  554. 552 // set up an interator and start at first node
  555. 553 int ict=1;
  556. 554 MyNode<T> curnode;
  557. 555 //make the interator point to the first node
  558. 556 curnode=head;
  559. 557 //now chain the list till we get to the one to return
  560. 558 while(ict<x){
  561. 559 curnode=curnode.next;
  562. 560 ict++;
  563. 561 } //curnode now is at the x'th node. let's return its content
  564. 562 return curnode.nodevalue;
  565. 563 }// this is the end of getnode
  566. 564 public void addinorder(T x)//This is for our own LL Build. No Java LL Class here
  567. 565 {//this function adds the new node in compareTo order of the class T being added
  568. 566 // first create the new node with x information
  569. 567 MyNode<T> newnode=new MyNode<T>(x);
  570. 568 // now we will need two pointers to the linked list, one to the current position and
  571. 569 //another to the next position.
  572. 570 MyNode<T> cnode, nnode;
  573. 571 // check for condition 1, this is the first node in the list
  574. 572 if(number==0)
  575. 573 {// this is the first node
  576. 574 head=newnode;
  577. 575 tail=newnode;
  578. 576 number=1;
  579. 577 return;
  580. 578 }
  581. 579 //there are nodes in the list. Check to see if this goes in front, we are making them
  582. 580 //larger to smaller.
  583. 581 if(x.compareTo(head.nodevalue)==1)
  584. 582 {// this node goes in front.
  585. 583 newnode.next=head;
  586. 584 head=newnode;
  587. 585 number++;
  588. 586 return;
  589. 587 }
  590. 588 if(x.compareTo(tail.nodevalue)==-1)
  591. 589 {//this node goes in the back
  592. 590 tail.next=newnode;
  593. 591 tail=newnode;
  594. 592 number++;
  595. 593 return;
  596. 594 }
  597. 595 // this node goes somewhere after the front and before the back let's begin
  598. 596 cnode=head;
  599. 597 nnode=head.next;
  600. 598 //now check and branch
  601. 599 while(x.compareTo(nnode.nodevalue)!=1)
  602. 600 {cnode=nnode;//chain forward with curent node
  603. 601 nnode=nnode.next;//chain forward with next node
  604. 602 }
  605. 603 // now x is greater than the nnode and less than cnode
  606. 604 // make the current node point to newnode and then make newnode point to the next node
  607. 605 cnode.next=newnode;
  608. 606 newnode.next=nnode;
  609. 607 number++;
  610. 608 return;
  611. 609 }// this is the end of addinorder
  612. 610
  613. 611 }//this is the end of MyLinkManager
  614. 612 class LinkManagerwithJavaListClass<T extends Comparable>{
  615. 613 /*This is a class that will store objects with a linked list. The objects will be added to the
  616. 614 list based on their compareTo function. They will be added from larger to smaller. The
  617. 615 LinkManager uses the java.util.LinkedList class. Further the class uses an interator to find
  618. 616 and return values from the list. Note that we do not need to create a pointer to the head and
  619. 617 tail of the list. The interator will take care of this */
  620. 618 protected LinkedList<T> llist;//=new LinkedList<T>();//this is the linked list we will create.
  621. 619 protected int number;// this is the number
  622. 620 protected ListIterator<T> lptr;
  623. 621 protected T y;
  624. 622
  625. 623 public LinkManagerwithJavaListClass()
  626. 624 {System.out.println("I'm in the constructor");//this is the constructor. Note that we build the LinkedList jclass
  627. 625 llist=new LinkedList<T>();
  628. 626 lptr=llist.listIterator();
  629. 627
  630. 628 number=0;//nothing in the list
  631. 629 System.out.println("I'm leaving the constructor");
  632. 630 }// end of the constructor
  633. 631 public int getnumber(){return number;}
  634. 632 // Let's start by adding a node to the end of the list. We'll add this with MyLinkManager
  635. 633 public int addnode(T x)
  636. 634 {//this adds a node to the linked list. It will always add to the last of the list
  637. 635 // System.out.println("I'm in addnode value"+x);
  638. 636 llist.addLast(x);
  639. 637 number++;
  640. 638 //System.out.println("I'm leaving addnode value"+x+"number is"+number);
  641. 639 return number;
  642. 640 }
  643. 641 public T getnode(int x)
  644. 642 {//this function returns the node at the x'th position in the list
  645. 643 // if x is out of bounds of the list a message is printed and an exception is thron
  646. 644 if ((x<0)||(x>number))System.out.println("error in getnode"+x+"while list holds"+number);
  647. 645 return llist.get(x);
  648. 646 }// this is the end of getnode
  649. 647 public void addinorder(T x)
  650. 648 {//this function adds the new node in compareTo order of the class T being added
  651. 649 // check for condition 1, this is the first node in the list
  652. 650 if(number==0)
  653. 651 {// this is the first node
  654. 652 //System.out.println("I'm about to add the first node"+x);
  655. 653
  656. 654 llist.addFirst(x);
  657. 655 number=1;
  658. 656 // System.out.println("I've added the first node"+number);
  659. 657 return;
  660. 658 }
  661. 659 //there are nodes in the list. Check to see if this goes in front, we are making them
  662. 660 //larger to smaller.
  663. 661 if(x.compareTo(llist.getFirst())==1)
  664. 662 {// this node goes in front.
  665. 663 // System.out.println("I'm about to add to the front of the list"+x);
  666. 664 llist.addFirst(x);
  667. 665 number++;
  668. 666 //System.out.println("I've added to the front of the list"+number);
  669. 667 return;
  670. 668 }
  671. 669 if(x.compareTo(llist.getLast())==-1)
  672. 670 {//this node goes in the back
  673. 671 //System.out.println("I'm about to add to the back of the list"+x);
  674. 672 llist.addLast(x);
  675. 673 number++;
  676. 674 //System.out.println("I've added to the back of the list"+number);
  677. 675 return;
  678. 676 }
  679. 677 // this node goes somewhere after the front and before the back let's begin
  680. 678 //by setting up an interator. interators start at the front of the list
  681. 679 ListIterator lptr2=llist.listIterator();
  682. 680
  683. 681 //now check and branch
  684. 682 for (int i=0;i<=number-1;i++)
  685. 683 {//NOTE WITH THE INTERATOR WE MUST CAST THE OBJECT POINTING TO THE RETURNED VALUE
  686. 684 y=(T)lptr2.next();
  687. 685 //System.out.println("I'm comparing"+x+"and "+y);
  688. 686 if(x.compareTo(y)==1)
  689. 687 {// interate through the list
  690. 688 // now we add x just before the lptr2.next() first we must move it back one
  691. 689 lptr2.previous();
  692. 690 //Then we add it in.
  693. 691 lptr2.add(x);
  694. 692 //System.out.println("just added"+x+"before "+y);
  695. 693 number++;
  696. 694 return;
  697. 695 }
  698. 696 }//end of for
  699. 697
  700. 698
  701. 699 }// this is the end of addinorder
  702. 700
  703. 701 }//this is the end of MyLinkManager
  704. ----jGRASP wedge2: pid for process is 1628.
  705. Point X 5 Y 8
  706. Circle with center X 5 Y 8 Radius 5
  707. Area 78.54
  708. Square with center X 5 Y 8 Side 5
  709. Area 25.0
  710. Square with center X 5 Y 8 Side 5
  711. Area 25.0
  712. Circle with center X 5 Y 8 Radius 5
  713. Area 78.54
  714. Square with center X 5 Y 8 Length 5width 4
  715. Area 20.0
  716. Square with center X 6 Y 7 Side 4
  717. Area 16.0
  718. Circle with center X 5 Y 8 Radius 27
  719. Area 2290.2264
  720. Square with center X 5 Y 10 Length 8width 3
  721. Area 24.0
  722. these are solids
  723. Sphere with center X 5 Y 6 Radius 4
  724. Area 201.0624
  725. Volumn 269.77279999999996
  726. Brick with center X 7 Y 9Length5width6height8
  727. Area 236.0
  728. volumn240.0
  729. Cube with center X 4 Y 5 Side 9
  730. Area 486.0
  731. Volumn 729.0
  732. These are from myints
  733. myints on outpt
  734. 26
  735. 27
  736. 28
  737. 29
  738. 30
  739. The manager has sorted ints
  740. 30
  741. 29
  742. 28
  743. 27
  744. 26
  745. These are from mysolids
  746. Sphere with center X 5 Y 6 Radius 4
  747. Area 201.0624
  748. Volumn 269.77279999999996
  749. Brick with center X 7 Y 9Length5width6height8
  750. Area 236.0
  751. volumn240.0
  752. Cube with center X 4 Y 5 Side 9
  753. Area 486.0
  754. Volumn 729.0
  755. These are from the new Generic Manager using the generic sort
  756. Cube with center X 4 Y 5 Side 9
  757. Area 486.0
  758. Volumn 729.0
  759. Brick with center X 7 Y 9Length5width6height8
  760. Area 236.0
  761. volumn240.0
  762. Sphere with center X 5 Y 6 Radius 4
  763. Area 201.0624
  764. Volumn 269.77279999999996
  765. This is the end of the new Generic Manager Test
  766. Now let's do some linked list work with MyLinkManager
  767. This is from MyLinkManager
  768. from linkint25
  769. from linkint-18
  770. from linkint67
  771. from linkint17
  772. from linkint34
  773. This is from MyLinkManager with flats
  774. from linkflat1
  775. Square with center X 5 Y 10 Length 8width 3
  776. Area 24.0
  777. from linkflat2
  778. Circle with center X 5 Y 8 Radius 27
  779. Area 2290.2264
  780. from linkflat3
  781. Square with center X 5 Y 8 Length 5width 4
  782. Area 20.0
  783. from linkflat4
  784. Circle with center X 5 Y 8 Radius 5
  785. Area 78.54
  786. from linkflat5
  787. Square with center X 5 Y 8 Side 5
  788. Area 25.0
  789. Now let's do some linked list work with MyLinkManager in order
  790. This is from MyLinkManager in order
  791. from linkinorder67
  792. from linkinorder34
  793. from linkinorder25
  794. from linkinorder17
  795. from linkinorder-18
  796. This is from MyLinkManager with flats in order
  797. from linkflatord1
  798. Circle with center X 5 Y 8 Radius 27
  799. Area 2290.2264
  800. from linkflatord2
  801. Circle with center X 5 Y 8 Radius 5
  802. Area 78.54
  803. from linkflatord3
  804. Square with center X 5 Y 8 Side 5
  805. Area 25.0
  806. from linkflatord4
  807. Square with center X 5 Y 10 Length 8width 3
  808. Area 24.0
  809. from linkflatord5
  810. Square with center X 5 Y 8 Length 5width 4
  811. Area 20.0
  812. from linkflatord6
  813. Square with center X 6 Y 7 Side 4
  814. Area 16.0
  815. I'm in the constructor
  816. I'm leaving the constructor
  817. This is from LinkManagerwithJavaListClass with ints out of order
  818. from javalinklst0
  819. 34
  820. from javalinklst1
  821. 17
  822. from javalinklst2
  823. 67
  824. from javalinklst3
  825. -18
  826. from javalinklst4
  827. 25
  828. I'm in the constructor
  829. I'm leaving the constructor
  830. This is from LinkManagerwithJavaListClass ints using addinorder
  831. from javalinklst20
  832. 67
  833. from javalinklst21
  834. 34
  835. from javalinklst22
  836. 25
  837. from javalinklst23
  838. 17
  839. from javalinklst24
  840. -18
  841. I'm in the constructor
  842. I'm leaving the constructor
  843. This is from LinkManagerwithJavaListClass with flats in order
  844. from javalinkflats0
  845. Circle with center X 5 Y 8 Radius 27
  846. Area 2290.2264
  847. from javalinkflats1
  848. Circle with center X 5 Y 8 Radius 27
  849. Area 2290.2264
  850. from javalinkflats2
  851. Circle with center X 5 Y 8 Radius 5
  852. Area 78.54
  853. from javalinkflats3
  854. Square with center X 5 Y 8 Side 5
  855. Area 25.0
  856. from javalinkflats4
  857. Square with center X 5 Y 10 Length 8width 3
  858. Area 24.0
  859. from javalinkflats5
  860. Square with center X 5 Y 8 Length 5width 4
  861. Area 20.0
  862. from javalinkflats6
  863. Square with center X 6 Y 7 Side 4
  864. Area 16.0
  865. 
  866.  ----jGRASP wedge2: exit code for process is 0.
  867.  ----jGRASP: operation complete.
  868. 
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement