//// CST-112 FINAL EXAM: Planet class, Button class. // **** CHANGE THIS TO many=8 **** // int many=8; int numbuttons=5; Button[] b= new Button[numbuttons]; String bname[]= { "size", "moons", "hours", "distance", "quit" }; float buttonX=220, buttonY=400; Planet[] p = new Planet[8]; String[] namelist= { "Mercury", "Venus", "Terra", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" }; int mercury=0, venus=1, earth=2, mars=3, jupiter=4, saturn=5, uranus=6, neptune=7; float[] distlist= { 0.387, 0.723, 1, 1.52, 5.20, 9.58, 19.20, 30.05, 39.48 }; // Orbital distance (AU) float[] sizelist= { 0.25, 0.95, 1, 0.45, 10, 7, 5, 5 }; // Multiple of Earth diameter. float[] spinlist= { 58.8, -244, 1, 27.4, 1.03, 0.415, 0.445, -0.720, 0.673, 6.41 }; int[] moonlist= { 0, 0, 1, 2, 67, 62, 27, 14, 5 }; // Moons boolean[] ringlist= { false, false, false, false, true, true, true, false }; int[] hourslist= { 1411, -5856, 24, 657, 24, 9, 10, -17}; //Hours void setup() { size( 640, 480 ); ////For loop to initialize buttons//// float x=buttonX, y=buttonY; for( int n=0; n<=4; ++n) { b[n]= new Button( bname[n], x, y); x+=60; } ////For loop to initialize planets//// int b = 0; for( int n=0; n<=7; ++n) { p[n]= new Planet( n, namelist[n], distlist[n], sizelist[n], moonlist[n], hourslist[n] ); } // Force Mars red. p[mars].r= 255; p[mars].g = 60; p[mars].b = 60; } void draw() { background( 0, 0, 50 ); showall(); // Display the planets. buttons(); // Buttons // fill(255, 255, 100); text( "Sorting keys: s, m, h, d", width/3 + 10, height-25 ); text( "Tristan Szakacs", 500, 15); text( "CST 112 FINAL: Solar System Planets", 200, 15); /*REMOVE*/ fill( 255, 200, 200 ); /*REMOVE*/ text( " (Now, make the buttons work!)", width/3, height-10 ); // +++++ ADD YOUR CODE (to make buttons work), then remove the above lines. flag( 20, height-120 ); } void showall() { labels( 10, 40 ); float x=80, y=40; for( int n = 0; n <=7; ++n) { p[n].show( x,y); x+=60; } averages( x+490, 40 ); } void buttons() { float x=buttonX, y=height-60; text( "SORT BY:", x+5, height-60 ); for( int n = 0; n <= 4; ++n) { b[n].show(); } } void labels( float x, float y ) { // Name & properties. int next=15, line=0; fill(200); line++; text( "NAME:", x, y+next*line++ ); text( "DISTANCE:", x, y+next*line++ ); text( "SIZE:", x, y+next*line++ ); text( "HOURS:", x, y+next*line++ ); fill(255); text( "MOONS:", x, y+next*line++ ); } void averages( float x, float y ) { // Name & properties. int next=15, line=1; fill(200); text( "Averages:", x, y+next*line++ ); // float total=0; for (int j=0; j0) text( moons, x+20, y+next*line++ ); fill(r, g, b); // showdisc( x + 20, y+next*line++ ); } void showdisc( float xx, float yy ) { // Circle in column x, use orbit for y value. stroke( r, g, b ); float yyy= yy + + orbit*20; line( xx, yy, xx, yyy ); float diameter= (float) Math.log(10*size) * 10; ellipse( xx, yyy, diameter, diameter ); if (ring) { stroke( r, g, b ); noFill(); ellipse( xx, yyy, 1.5*diameter, 0.5*diameter ); } }// END OF showdisc() // }// END OF class Planet // //// SORT METHODS //// void sortSize( Planet[] a, int many ) { for (int m=many; m>1; --m) { // Move biggest to end, then shrink array. int w= wSize( a, m ); swap( a, m-1, w ); } } int wSize( Planet[] a, int m ) { int w=0; for (int j=1; j= a[w].size) w=j; } return w; } void swap( Planet a[], int j, int k) { Planet tmp= a[j]; a[j]= a[k]; a[k]= tmp; } void sortMoons( Planet[] a, int many ) { for (int m=many; m>1; --m) { swap( a, m-1, wMoons( a, m ) ); // Move biggest to end, then shrink array. } } int wMoons( Planet[] a, int m ) { int w=0; for (int j=1; j= a[w].moons) w=j; } // If this one is bigger, change w to point to it. return w; } void sortHours( Planet[] a, int many ) { for (int m=many; m>1; --m) { swap( a, m-1, wHours( a, m ) ); // Move biggest to end, then shrink array. } } int wHours( Planet[] a, int m ) { int w=0; for (int j=1; j= a[w].hours) w=j; // If this one is bigger, change w to point to it. return w; } // ++++ ADD A sortDistance() method HERE ++++++ void sortDistance( Planet[] a, int many ) { /*REPLACE*/ fill(255); text( "THIS CODE DOES NOTHING! REPLACE IT.", width/2, height/2 ); // ++++ ADD YOUR CODE HERE ++++ }