Advertisement
Guest User

Untitled

a guest
May 11th, 2013
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.21 KB | None | 0 0
  1. /*
  2. Finding the largest object
  3. Write a method that returns the largest objects in an array of objects. The method
  4. Signature is:
  5. public static Object max( Comparable[] a)
  6. All objects are instance of the Comparable interface. The order of objects in array is determind using compareTo method.
  7. Write a test program( Deriver) that creates array of ten Strings, MRects and Dates, and find the largest String, MRect and Date in the array.
  8. */
  9.  
  10. import java.util.Random;
  11. import java.util.Arrays;
  12.  
  13.  
  14. public class Driver
  15. {
  16.     public Driver()
  17.     {
  18.     }
  19.    
  20.     public static void displayArray( Object [] ob )
  21.     {
  22.         for( int i = 0; i < ob.length; i++ )
  23.             System.out.println( i + ": " + ob[i] );
  24.         System.out.println( "displayArray done." );
  25.     }
  26.    
  27.     public static int randNum( int lowrange, int highrange )
  28.     {
  29.         return lowrange + ( int )( Math.random()* highrange ); // return a value from 1 to 9
  30.     }
  31.    
  32.     public static void main( String [] Args )
  33.     {
  34.         // declare and create new arrays
  35.         MyMRects  [] rectangles = new MyMRects[10];
  36.         MyStrings [] words      = new MyStrings[10];
  37.         MyDates   [] dates      = new MyDates[10];
  38.        
  39.         // assign values to arrays
  40.         MyMRects.fillRects(    rectangles );
  41.         MyStrings.fillStrings( words      );   
  42.         MyDates.fillDates(     dates      );   
  43.        
  44.         Object [] things;
  45.         things = new Object[3];
  46.        
  47.         things[0] = rectangles;
  48.         things[1] = words;
  49.         things[2] = dates;
  50.        
  51.         // pre-sort printing
  52.         System.out.println( "\nBEFORE SORT:" );
  53.         displayArray( words      );
  54.         displayArray( rectangles );
  55.         displayArray( dates      );
  56.        
  57.         // sort the arrays
  58.         Arrays.sort( words );
  59.         Arrays.sort( rectangles );
  60.         Arrays.sort( dates );
  61.        
  62.         // post sort printing
  63.         System.out.println( "\nAFTER SORT:" );
  64.         displayArray( words      );
  65.         displayArray( rectangles );
  66.         displayArray( dates      );
  67.     }  
  68. }
  69.  
  70. class MyStrings implements Comparable<MyStrings>
  71. {
  72.     private String thestrings;
  73.    
  74.     public MyStrings()
  75.     {
  76.         thestrings = new String( "NONE_SET" );
  77.     }
  78.    
  79.     public MyStrings( String a )
  80.     {
  81.         thestrings = new String( a );
  82.     }
  83.    
  84.     public void setStr( String a )
  85.     {
  86.         this.thestrings = a;
  87.     }
  88.    
  89.     public static void fillStrings( MyStrings [] a )
  90.     {
  91.         for( int i = 0; i < a.length; i++ ) // scan through the words array
  92.             a[i] = new MyStrings( genString() );
  93.  
  94.     }
  95.    
  96.     // tested! this method returns a string of random letters/spaces between 10-35 length
  97.     public static String genString()
  98.     {
  99.         String tempstring = new String( "" );
  100.        
  101.         // pick random characters from alphabet as string values
  102.         final String alphabet = "abcdefghijklmnopqrstuvwxyz";
  103.         final int N = alphabet.length();
  104.         Random r = new Random();
  105.        
  106.         // randomize how long the string will be
  107.         int howManyChars = Driver.randNum( 10, 35 );
  108.        
  109.         // whatever the string length is going to be, add that many characters
  110.         // with "random" spacing in between
  111.         for (int i = 0; i < howManyChars; i++)
  112.             if( i%( ( Driver.randNum( 4, 7 ) ) ) == 0 ) // the random spacing part
  113.                 tempstring += " ";
  114.             else
  115.                 tempstring += ( alphabet.charAt(r.nextInt(N)) );
  116.  
  117.         return new String( tempstring );
  118.     }
  119.     public String getStr()
  120.     {
  121.         return thestrings;
  122.     }
  123.    
  124.     public String toString()
  125.     {
  126.         return getStr();
  127.     }
  128.    
  129.     @Override
  130.     public int compareTo( MyStrings ob )
  131.     {
  132.         if( this.thestrings.length() > ob.thestrings.length() )
  133.             return 1;
  134.         else if( this.thestrings.length() < ob.thestrings.length() )
  135.             return -1;
  136.         else
  137.             return 0;
  138.     }
  139.  
  140. }
  141.  
  142. class MyMRects implements Comparable<MyMRects>
  143. {
  144.     private int width;
  145.     private int height;
  146.    
  147.     public MyMRects()
  148.     {
  149.     }
  150.    
  151.     public MyMRects( String a )
  152.     {
  153.         width = 1;
  154.         height = 1;
  155.     }
  156.    
  157.     public MyMRects( int w, int h )
  158.     {
  159.         width  = w;
  160.         height = h;
  161.     }
  162.    
  163.     public int getArea()
  164.     {
  165.         return getHeight()*getWidth();
  166.     }
  167.    
  168.     public int getWidth()
  169.     {
  170.         return this.width;
  171.     }
  172.    
  173.     public int getHeight()
  174.     {
  175.         return this.height;
  176.     }
  177.    
  178.     public void setWidth( int a )
  179.     {
  180.         this.width = a;
  181.     }
  182.  
  183.     public void setHeight( int a )
  184.     {
  185.         this.height = a;
  186.     }
  187.    
  188.     // creates a.length objects filled with random width and height from 1-80
  189.     public static void fillRects( MyMRects [] a )
  190.     {
  191.         for( int i = 0; i < a.length; i++ )
  192.             a[i] = new MyMRects( ( Driver.randNum( 1, 80 ) ), ( Driver.randNum( 1, 80 ) ) );
  193.     }
  194.    
  195.     // returns string in this format: Width: ## Height: ## Area: ####
  196.     public String toString()
  197.     {
  198.         return ( (getWidth() < 10) ? " Width:  ":" Width: "  ) + width + " "
  199.              + ( (getHeight() < 10) ? " Height:  ":" Height: "  ) + height +  " " + "Area: " + this.getArea();
  200.     }
  201.    
  202.     // compares areas from calling object and parameter object
  203.     @Override
  204.     public int compareTo( MyMRects ob )
  205.     {
  206.         if( this.getArea() > ob.getArea() )
  207.             return  1;
  208.         else if( this.getArea() < ob.getArea() )
  209.             return -1;
  210.         else
  211.             return  0;
  212.     }
  213. }
  214.  
  215. class MyDates implements Comparable<MyDates>
  216. {
  217.     private int year;
  218.     private int month;
  219.     private int day;
  220.    
  221.     public MyDates()
  222.     {
  223.         year  = 1000;
  224.         month =   01;
  225.         day   =   01;
  226.     }
  227.    
  228.     public MyDates( int y, int m, int d )
  229.     {
  230.         year  = y;
  231.         month = m;
  232.         day   = d;
  233.     }
  234.    
  235.     public int getMonth()
  236.     {
  237.         return month;
  238.     }
  239.    
  240.     public static void fillDates( MyDates [] a )
  241.     {
  242.         for( int i = 0; i < a.length; i++ )
  243.         {
  244.             int y = Driver.randNum( 1000, 3000 );
  245.             int m = Driver.randNum( 1,    12   );
  246.             int d = Driver.randNum( 1,    30   );
  247.  
  248.             a[i] = new MyDates( y, m, d );
  249.         }
  250.     }
  251.  
  252.     // returns string in this format: Year: #### Month: ## Day: ##
  253.     public String toString()
  254.     {
  255.         return "Year: " + year + " "
  256.                + ( ( getMonth() < 10 ) ? " Month:  ":" Month: " ) + month + " "
  257.                + "Day: " + day;
  258.     }
  259.    
  260.     // compares the years first, then the months, and then the days
  261.     @Override
  262.     public int compareTo( MyDates ob )
  263.     {
  264.         if( this.year > ob.year )
  265.             return 1;
  266.         else if( this.year < ob.year )
  267.             return -1;
  268.         else                            // if years are the same, go to months
  269.             if( this.month > ob.month )
  270.                 return 1;
  271.             else if( this.month < ob.month )
  272.                 return -1;
  273.             else                        // if months are the same, go to days
  274.                 if( this.day > ob.day )
  275.                     return 1;
  276.                 else if( this.day < ob.day )
  277.                     return -1;
  278.                 else
  279.                     return 0;           // all is the same. EQUALLY FINALLY!! WOOO
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement