Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Finding the largest object
- Write a method that returns the largest objects in an array of objects. The method
- Signature is:
- public static Object max( Comparable[] a)
- All objects are instance of the Comparable interface. The order of objects in array is determind using compareTo method.
- 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.
- */
- import java.util.Random;
- import java.util.Arrays;
- public class Driver
- {
- public Driver()
- {
- }
- public static void displayArray( Object [] ob )
- {
- for( int i = 0; i < ob.length; i++ )
- System.out.println( i + ": " + ob[i] );
- System.out.println( "displayArray done." );
- }
- public static int randNum( int lowrange, int highrange )
- {
- return lowrange + ( int )( Math.random()* highrange ); // return a value from 1 to 9
- }
- public static void main( String [] Args )
- {
- // declare and create new arrays
- MyMRects [] rectangles = new MyMRects[10];
- MyStrings [] words = new MyStrings[10];
- MyDates [] dates = new MyDates[10];
- // assign values to arrays
- MyMRects.fillRects( rectangles );
- MyStrings.fillStrings( words );
- MyDates.fillDates( dates );
- Object [] things;
- things = new Object[3];
- things[0] = rectangles;
- things[1] = words;
- things[2] = dates;
- // pre-sort printing
- System.out.println( "\nBEFORE SORT:" );
- displayArray( words );
- displayArray( rectangles );
- displayArray( dates );
- // sort the arrays
- Arrays.sort( words );
- Arrays.sort( rectangles );
- Arrays.sort( dates );
- // post sort printing
- System.out.println( "\nAFTER SORT:" );
- displayArray( words );
- displayArray( rectangles );
- displayArray( dates );
- }
- }
- class MyStrings implements Comparable<MyStrings>
- {
- private String thestrings;
- public MyStrings()
- {
- thestrings = new String( "NONE_SET" );
- }
- public MyStrings( String a )
- {
- thestrings = new String( a );
- }
- public void setStr( String a )
- {
- this.thestrings = a;
- }
- public static void fillStrings( MyStrings [] a )
- {
- for( int i = 0; i < a.length; i++ ) // scan through the words array
- a[i] = new MyStrings( genString() );
- }
- // tested! this method returns a string of random letters/spaces between 10-35 length
- public static String genString()
- {
- String tempstring = new String( "" );
- // pick random characters from alphabet as string values
- final String alphabet = "abcdefghijklmnopqrstuvwxyz";
- final int N = alphabet.length();
- Random r = new Random();
- // randomize how long the string will be
- int howManyChars = Driver.randNum( 10, 35 );
- // whatever the string length is going to be, add that many characters
- // with "random" spacing in between
- for (int i = 0; i < howManyChars; i++)
- if( i%( ( Driver.randNum( 4, 7 ) ) ) == 0 ) // the random spacing part
- tempstring += " ";
- else
- tempstring += ( alphabet.charAt(r.nextInt(N)) );
- return new String( tempstring );
- }
- public String getStr()
- {
- return thestrings;
- }
- public String toString()
- {
- return getStr();
- }
- @Override
- public int compareTo( MyStrings ob )
- {
- if( this.thestrings.length() > ob.thestrings.length() )
- return 1;
- else if( this.thestrings.length() < ob.thestrings.length() )
- return -1;
- else
- return 0;
- }
- }
- class MyMRects implements Comparable<MyMRects>
- {
- private int width;
- private int height;
- public MyMRects()
- {
- }
- public MyMRects( String a )
- {
- width = 1;
- height = 1;
- }
- public MyMRects( int w, int h )
- {
- width = w;
- height = h;
- }
- public int getArea()
- {
- return getHeight()*getWidth();
- }
- public int getWidth()
- {
- return this.width;
- }
- public int getHeight()
- {
- return this.height;
- }
- public void setWidth( int a )
- {
- this.width = a;
- }
- public void setHeight( int a )
- {
- this.height = a;
- }
- // creates a.length objects filled with random width and height from 1-80
- public static void fillRects( MyMRects [] a )
- {
- for( int i = 0; i < a.length; i++ )
- a[i] = new MyMRects( ( Driver.randNum( 1, 80 ) ), ( Driver.randNum( 1, 80 ) ) );
- }
- // returns string in this format: Width: ## Height: ## Area: ####
- public String toString()
- {
- return ( (getWidth() < 10) ? " Width: ":" Width: " ) + width + " "
- + ( (getHeight() < 10) ? " Height: ":" Height: " ) + height + " " + "Area: " + this.getArea();
- }
- // compares areas from calling object and parameter object
- @Override
- public int compareTo( MyMRects ob )
- {
- if( this.getArea() > ob.getArea() )
- return 1;
- else if( this.getArea() < ob.getArea() )
- return -1;
- else
- return 0;
- }
- }
- class MyDates implements Comparable<MyDates>
- {
- private int year;
- private int month;
- private int day;
- public MyDates()
- {
- year = 1000;
- month = 01;
- day = 01;
- }
- public MyDates( int y, int m, int d )
- {
- year = y;
- month = m;
- day = d;
- }
- public int getMonth()
- {
- return month;
- }
- public static void fillDates( MyDates [] a )
- {
- for( int i = 0; i < a.length; i++ )
- {
- int y = Driver.randNum( 1000, 3000 );
- int m = Driver.randNum( 1, 12 );
- int d = Driver.randNum( 1, 30 );
- a[i] = new MyDates( y, m, d );
- }
- }
- // returns string in this format: Year: #### Month: ## Day: ##
- public String toString()
- {
- return "Year: " + year + " "
- + ( ( getMonth() < 10 ) ? " Month: ":" Month: " ) + month + " "
- + "Day: " + day;
- }
- // compares the years first, then the months, and then the days
- @Override
- public int compareTo( MyDates ob )
- {
- if( this.year > ob.year )
- return 1;
- else if( this.year < ob.year )
- return -1;
- else // if years are the same, go to months
- if( this.month > ob.month )
- return 1;
- else if( this.month < ob.month )
- return -1;
- else // if months are the same, go to days
- if( this.day > ob.day )
- return 1;
- else if( this.day < ob.day )
- return -1;
- else
- return 0; // all is the same. EQUALLY FINALLY!! WOOO
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement