prelim3ansfall2005 - } return theMax; } public void...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
CS100J Fall 2005 Sample answers for prelim 3 1a. You didn’t need to put in the 3 round buttons, and you didn’t need to center “is”. 1b. public MyGUI() { Container cp= getContentPane(); cp.add( new JTextField("text field 1"), BorderLayout.NORTH); cp.add( new JButton("button 1"), BorderLayout.WEST); cp.add( new JButton("button 2"), BorderLayout.CENTER); cp.add( new JButton("button 3"), BorderLayout.EAST); cp.add( new JTextField("text field 2"), BorderLayout.SOUTH); pack(); setVisible( true ); } 2. if (opinions.size() == 0) return -1; int total= 0; // inv: total = sum of ratings in v[0. .i-1] for ( int i= 0; i != opinions.size(); i= i+1) { Opinion op= (Opinion) opinions.get(i); int v= op.getRating(); total= total + v; } return total / opinions.size(); 3. private int findMax( int h, int k) { int i; int theMax= h+1; double m= db[h].getOverAllRating(); // inv: movie[theMax] is max of movie[h. .i-1] // and m is its average rating. for (i= h+1; i != db.length; i= i+1) { if (db[i].getOverAllRating() > m) theMax= i;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } return theMax; } public void sortByRating() { // inv: db[0. .t-1] is sorted and db[0. .t-1] > db[t. .] for ( int t= 0; t != db.length; t= t+1) { int j= findMax(t, db.length-1); // Swap movie[t] with movie[j] Movie m= db[t]; db[t]= db[j]; db[j]= m; } } 4. (a) double b= {8, 5, 3, 2, 1}; (b) k= b[i]; while (k > 0 && b[k] <32) { b[k1]= k; k= k 3; } (c) stayAtHome= !(raining || snowing); (d) 1. Create a frame for the call. 2. Assign the value 5 to parameter x. 3. Execute the method body 4. Erase the frame for the call. Parameter x and local variables y and z are created during step 1, when the frame for the call is created. 5. (a) this .f= f; (b) super (c / 43.66); (c) return 43.66 * getDollar(); (e) a. f1= new Dollar(32); legal b. f2= new Rupee(6); legal c. String s1= f1.toString(); legal d. String s2= f2.toString(); legal e. double x= f2.getRupee(); illegal a2 Dollar Rupee Rupee( double ) getRupee() toString() f ___?___ Dollar( double ) getDollar()...
View Full Document

This note was uploaded on 06/01/2008 for the course CS 100 taught by Professor Fan/vanloan during the Fall '07 term at Cornell University (Engineering School).

Ask a homework question - tutors are online