Final Review

  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Final Review as PDF for free.

More details

  • Words: 408
  • Pages: 2
2/27/09 2:54 PM

=================================================================== COGS 18 - DISCUSSION EXAMPLES: FINAL on 3/18/09 ------------------------------------------------------------------1) SORTING ARRAYS Q1: What is the error in the 3rd for loop? Q2: What gets printed? ------------------------public class F1 { public static void main( String args[] ) { int i, j, k, tmp, a[] = { 4, 3, 2, 1 }; int b[] = { 4, 3, 2, 1 }; for ( i = 0 ; i < a.length-1 ; ++i ) for ( j = i + 1 ; j < a.length ; ++j ) { if( a[i] > a[j] ) { tmp = a[i]; a[i] = a[j]; a[j] = tmp; System.out.println( a[j] ); } } // for ( k = 0 ; k <= b.length ; ++k ) // b[k] += 2; } } ------------------------------------------------------------------2) CLASSES/CONSTRUCTORS Q1) Which methods are the constructors? How do you know? Q2) Where are they called? Q3) What is the purpose of the constructor? Q4) What gets printed? Q5) What happens if you omit line C)? ------class Date { private int month, year; // A public Date() { year = 2000; month = 3; } // B public Date(int m, int y) { year = y; month = m; } // C public int getYr() { return year; } public int getMon(){ return month; } public void leap() { year = year + month; }

// D // E // F

} public class F2 { public static void main(String [] args) { Date myYear, yourYear; // G myYear = new Date(); // H yourYear = new Date(3, 2006); // I System.out.println(myYear.getMon() + "/" + myYear.getYr()); yourYear.leap(); System.out.println(yourYear.getMon() + "/" + yourYear.getYr()); }

http://icogsci1.ucsd.edu/~cg18wzz/frev

1 of 2

2/27/09 2:54 PM

} ------------------------------------------------------------------3) What gets printed? ------------------------------------------------------------------public class F3 { public static void main( String args[] ) { int j; String s1 = "eat#sleep#fun#study#eat!"; String s2 = "Final FINAL End final END End end finally!"; String []b = s2.split(" "); caps( s1 ); for( j = 0 ; j < b.length-1 ; ++j ) if( b[j].equalsIgnoreCase( b[j+1] ) ) System.out.println("True: " + b[j]); else System.out.println( j + " F: " + b[j]); System.exit(0); } public static void caps( String s) { char ch; int j; String tmpS = ""; for(j = 0; j < s.length() ; ++j ) { ch = s.charAt(j); if( ch == '#') { ch = s.charAt(--j); ch = Character.toUpperCase(ch); ++j; } tmpS += ch; } System.out.println( tmpS ); } } ================================================================

http://icogsci1.ucsd.edu/~cg18wzz/frev

2 of 2

Related Documents

Final Review
December 2019 26
Final Review
April 2020 17
Final Review
November 2019 26
Final Review
May 2020 17
Final Review
June 2020 17
Final Review
June 2020 8