340 likes | 1.02k Views
Midterm Review Session. Programming Problems For more practice: http://webster.cs.washington.edu:8080/practiceit/. Midterm Logistics. Bring your UW Student ID card !! Will check your ID while you’re leaving Bring your stuff with you while you exit (don’t go back to seat) 60 minute exam
E N D
Midterm Review Session Programming Problems For more practice: http://webster.cs.washington.edu:8080/practiceit/
Midterm Logistics • Bring your UW Student ID card!! • Will check your ID while you’re leaving • Bring your stuff with you while you exit (don’t go back to seat) • 60 minute exam • -10 if you start early or try to write after time • Any paper notes allowed, but NO ELECTRONICS • Book OK, section handouts OK, past exams + solutions OK, etc. • NO CALCULATORS! NO LAPTOPS! • Aiming for a median of 80 • Will be a curve if the exam is too hard, will not have a hurtful curve if exam is too easy
About the Exam • 100 pts, 20% of course grade • Non-programming – 61 pts • Expressions (10), parameter mystery (12), if/else simulation (12), while loop simulation (12), assertions (15) • Easy Programming – 15 pts • Medium Programming – 15 pts • Really Hard Programming – 9 pts • Topics covered: Chapters 1-5 • Topics not covered: see website! • Includes Graphics, printf, do/while, break
More Exam Info • Non-programming questions • You do not need to show your work • You are not allowed to use calculators (e.g. on expressions) • Programming questions • Are not graded on style, only external correctness • Must write valid Java code that would compile • Will not grade comments or pseudo-code • Do NOT use abbreviations • Do not have to write import statements • Substantial partial credit is given for partially working solutions • Write as much as you can: even a method header and the beginnings of a while loop could earn you partial points! • More info on course website!
General Tips • BE AFRAID. Success on HW is not an indication of being prepared for the exam. • Watch the clock! • The hardest part about this exam will be the time limit. • PRACTICE Q1-Q5 so that you can go through them fast and accurately! You want as much time as possible for the programming questions. • Write as much as you can! • A partial answer is better than none at all. • Writing the correct method header will earn at least 1 point. • Writing a partial loop, if statements, returns, etc. will points too
General Tips • Read the instructions now and learn the format so you won't be surprised tomorrow • The format of the midterm will be the same as the samples • For more practice, use Marty Stepp's Practice-it tool: http://webster.cs.washington.edu:8080/practiceit/
Concepts Review Some essential topics from Ch1-5
For-loops • Use to repeat stuff when you know exactly* how much you want to repeat • * Could also have a variable or expression that tells you exactly how much you want to repeat • Nested for-loops: loops within a loop • When you want to do task multiple times, and that task has repetition to it • E.g. multiplication table • Cumulative sum: variable that keeps a sum in progress and is updated repeatedly until summing is finished • In general: think about bounds, possible fencepost issues
Conditionals and Tests • If/else statement: Decide between several logical choices • While loop: Repeat unknown number of times • Sentinel loops: repeat until a certain signal is seen (e.g. prompt until you see a -1) • boolean: primitive type with a true or false value • When to return?? • Boolean flag is helpful
Strings • length() to get length of a String • indexOf/containsto search for substring within String • equals/equalsIgnoreCase to test equality • startsWith/endsWith to test beginning/end of word • substring(index1, index2)to get a piece of a String • index1 inclusive, index2 exclusive • Index 0 for first letter, index length() -1 for last letter • + to concatenate Strings together
Other miscellaneous stuff • Scanner to prompt for input • hasNext, hasNextInt, hasNextDouble, hasNextLine • Random to get a random value • To generate random number between [min, max]:r.nextInt(range) + min Where range: max – min + 1 • r.nextInt(51) + 50; // produces random value 50 - 100 • Expressions stuff: • % 10 and / 10 to get digits • % 2 == 0 to test for even (!= for odd)
Programming Practice Practice problems from previous exams
Programming Tips • Read the problems carefully: • Does it want you to print a result, or return it? • What values does the method use for computation? Are these values parameters, are they read from a Scanner, etc.? • What type of value (if any) does the method return? • Have you handled all special cases? • What if the integer is 0, or negative? • What if the string has no letters? • What if there is only one word in the string? Many words?
Practice Problem 1: Print Multiples • Write a static method named printMultiples • Takes two integers n and m as parameters and prints the first m multiples of n • Assume m >= 1 • Multiples are separated by commas printMultiples(3, 5); The first 5 multiples of 3 are 3, 6, 9, 12, 15 printMultiples(7, 3); The first 3 multiples of 7 are 7, 14, 21
Print Multiples Solution public static void printMultiples(int n, int times) { System.out.print("The first " + times + " multiples of " + n + " are " + n); for (int i = 2; i <= times; i++) { System.out.print(", " + i * n); } System.out.println(); }
Practice Problem 2: Count Even Digits • Write a static method named countEvenDigits • Accepts an integer as its parameter and returns the number of even-valued digits in that number. • Even digits: 0, 2, 4, 6, or 8 • Assume value passed to your method is non-negative // 4 even digits: two 8s, the 4, and 6 int x = countEvenDigits(8346387) ; System.out.println("x is " + x); // x is 4
Count Even Digits Solution public static int countEvenDigits(int n) { int count = 0; while (n != 0) { int digit = n % 10; n = n / 10; if (digit % 2 == 0) { count++; } } return count; }
Practice Problem 3: Cheerleader • Write a static method named cheerleader • Takes two params: • number of lines of output • number of "cheers" per line • Cheer structure: • 1 cheer: Go • 2 cheers: Go Team Go • 3 cheers: Go Team Go Team Go • Each line indented by 3 spaces; first line at 0 spaces cheerleader(2, 1); Go Go cheerleader(4, 3); Go Team Go Team Go Go Team Go Team Go Go Team Go Team Go Go Team Go Team Go cheerleader(2, 4); Go Team Go Team Go Team Go Go Team Go Team Go Team Go
Cheerleader Solution public static void cheerleader(int lines, int cheers) { for (int line = 1; line <= lines; line++) { for (int space = 1; space <= line - 1; space++) { System.out.print(" "); } for (int cheer = 1; cheer <= cheers - 1; cheer++) { System.out.print("Go Team "); } System.out.println("Go"); } }
Practice Problem 4: Favorite Letter • Write a static method named faveLetter • Accepts two parameters: • Scanner for the console • Favorite letter represented as a one-letter String • Repeatedly prompt the user until two consecutive words are entered that start with that letter. • Case sensitive • Print a message showing the last word typed. (assume a Scanner named console was made earlier in code) faveLetter(console, "y"); Looking for two "y" words. Type a word: hi Type a word: bye Type a word: yes Type a word: what? Type a word: yellow Type a word: yippee "y" is for "yippee"
Favorite Letter Solution public static void faveLetter(Scanner console, String letter) { System.out.println("Looking for two \"" + letter + "\" words."); int count = 0; String word = ""; while (count < 2) { System.out.print("Type a word: "); word = console.next(); if (word.startsWith(letter)) { count++; } else { count = 0; } } System.out.println("\"" + letter + "\" is for \"" + word + "\""); }
Practice Problem 5: Random Rects • Write a static method named randomRects • Calculates and displays the area of randomly-generated rectangles. • Each rectangle has random width and height btwn 1 and 10 inclusive • Keep generating rectangles until an increasing sequence of four areas is printed. • Stop when the last four rectangles generated have areas of a1, a2, a3 and a4 such that a1 < a2 < a3 < a4 randomRects(); w: 5, h: 6, area: 30 w: 10, h: 5, area: 50 w: 2, h: 8, area: 16 w: 4, h: 4, area: 16 w: 2, h: 9, area: 18 w: 8, h: 3, area: 24 w: 7, h: 2, area: 14 w: 3, h: 10, area: 30 w: 7, h: 9, area: 63 w: 9, h: 8, area: 72 End random rectangles.
Random Rectangles Solution public static void randomRects() { Random r = new Random(); int last = 0; int count = 0; while (count != 4) { int w = r.nextInt(10) + 1; int h = r.nextInt(10) + 1; int area = w * h; if (area <= last) { count = 1; // need to count first rect in sequence } else { count++; } System.out.println("w: " + w + ", h: " + h + ", area: " + area); last = area; } System.out.println("End random rectangles."); }
Non-Programming Practice Tips on Questions 1-4 (For assertions help, see lecture 14)
Expressions • integer division and mod: quotient and remainder 14 / 3 is 4, 14 % 3 is 2 7 / 10 is 0, 7 % 10 is 7 • precedence: ( ) before * / % before + - 5 + 2 * 6 - (1 + 7) % 3 5 + 2 * 6 - 8 % 3 5 + 12 - 8 % 3 5 + 12 - 2 17 - 2 15
Expressions 2 • String concatenation: same precedence as integer + -, evaluated left-to-right with other + - operations 1 + 2 + "3" + 4 + 5 3 + "3" + 4 + 5 "33" + 4 + 5 "334" + 5 "3345" • type promotion: done as needed when int and double are mixed 50 / 6 / 5.0 8 / 5.0 1.6
Expression questions • Evaluate the following expressions: 16 / 3 + 3.2 * 2 8 / 7 + "5 / 4" + 8 / 3 88 % 10 % 3 * 16 / 10 29 / 3 / 2 / 4.0 + 3.6 * 2 1.4 + (3 + 2 * 6) / (8 - 14 / 3) * 2.2
Expression answers • Correct answers: 16 / 3 + 3.2 * 2 11.4 8 / 7 + "5 / 4" + 8 / 3 "15 / 42" 88 % 10 % 3 * 16 / 10 3 29 / 3 / 2 / 4.0 + 3.6 * 2 8.2 1.4 + (3 + 2 * 6) / (8 - 14 / 3) * 2.2 8.0
Parameter mystery question • What is the output of the following program? public class Mystery { public static void main(String[] args) { String john = "skip"; String mary = "george"; String george = "mary"; String fun = "drive"; String work = "john"; speak(mary, john, fun); speak(george, work, john); speak(fun, "george", "work"); speak(george, mary, john); speak(george, "john", "dance"); } public static void speak(String mary, String john, String fun) { System.out.println(john + " likes to " + fun + " with " + mary); } }
Parameter mystery tips • Try making a table of the parameters passed to each call: public class Mystery { public static void main(String[] args) { String john = "skip"; String mary = "george"; String george = "mary"; String fun = "drive"; String work = "john"; speak(mary, john, fun); speak(george, work, john); speak(fun, "george", "work"); speak(george, mary, john); speak(george, "john", "dance"); } public static void speak(String mary, String john, String fun) { System.out.println(john + " likes to " + fun + " with " + mary); } }
Parameter mystery answer • What is the output of the following program? public class Mystery { public static void main(String[] args) { String john = "skip"; String mary = "george"; String george = "mary"; String fun = "drive"; String work = "john"; speak(mary, john, fun); skip likes to drive with george speak(george, work, john); john likes to skip with mary speak(fun, "george", "work"); george likes to work with drive speak(george, mary, john); george likes to skip with mary speak(george, "john", "dance"); john likes to dance with mary } public static void speak(String mary, String john, String fun) { System.out.println(john + " likes to " + fun + " with " + mary); } }
While loop mystery question • Given the following program, public static void mystery(int y) { int x = 0; int z = 0; while (y > 0) { System.out.print(x + " " + z + " "); x++; z = z + y % 10; y = y / 10; } System.out.println(x + " " + z); } • What is the output of the following sequence of calls? mystery(0); mystery(8); mystery(32); mystery(72); mystery(184); mystery(8239);
While loop mystery tips • Keep track of each variable's value as it changes: public static void mystery(int y) { int x = 0; int z = 0; while (y > 0) { System.out.print(x + " " + z + " "); x++; z = z + y % 10; y = y / 10; } System.out.println(x + " " + z); } • What is the output of the following call? mystery(184); • Sometimes, these problems are performingreal computations in disguise. • What is this problem really doing?
While loop mystery answers • Given the following program, public static void mystery(int y) { int x = 0; int z = 0; while (y > 0) { System.out.print(x + " " + z + " "); x++; z = z + y % 10; y = y / 10; } System.out.println(x + " " + z); } • What is the output of the following sequence of calls? mystery(0); 0 0 mystery(8); 0 0 1 8 mystery(32); 0 0 1 2 2 5 mystery(72); 0 0 1 2 2 9 mystery(184); 0 0 1 4 2 12 3 13 mystery(8239); 0 0 1 9 2 12 3 14 4 22