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