Midterm Review Session

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-2: Advanced if/else ; Cumulative sum reading: 4.1, 4.3, 4.5; "Procedural.
Building Java Programs
Computer Science 1620 Loops.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Copyright 2008 by Pearson Education 1 Midterm announcements Next week on Friday May 8 Must bring an ID Open book, open notes, closed electronics Must attend.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops; Procedural Design reading: 5.1 – 5.2; 4.5.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Building Java Programs
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Building java programs, chapter 5 Program logic and indefinite loops.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
1 CSE 142 Midterm Review Problems These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or modified.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
1 CSE 142 Final Exam Review Problems. 2 Question Types expressions array mystery inheritance mystery file processing array programming Critters classes.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Copyright 2009 by Pearson Education Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic reading: 5.2 self-check: # exercises: #12 videos:
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Midterm Review Problems
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Midterm Review Session Programming Problems For more practice: http://webster.cs.washington.edu:8080/practiceit/

Midterm Logistics Bring your UW Student ID card!! 60 minute exam 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 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/

Some essential topics from Ch1-5 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/contains to 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)

Practice problems from previous exams 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 http://webster.cs.washington.edu:8080/practiceit/problem.jsp?category=142+Midterm+Exams&=section2_1_expressions&problem=practicemidterm2_6_printMultiples 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 http://webster.cs.washington.edu:8080/practiceit/problem.jsp?category=142+Midterm+Exams&=section2_1_expressions&problem=practicemidterm3_8_countEvenDigits

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 cheerleader(4, 3); Go Team Go Team Go cheerleader(2, 4); Go Team Go Team Go Team Go http://webster.cs.washington.edu:8080/practiceit/problem.jsp?category=142+Midterm+Exams&=section2_1_expressions&problem=practicemidterm4_7_cheerleader

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" http://webster.cs.washington.edu:8080/practiceit/problem.jsp?category=142+Midterm+Exams&=section2_1_expressions&problem=practicemidterm1_8_favoriteLetter

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. http://webster.cs.washington.edu:8080/practiceit/problem.jsp?category=142+Midterm+Exams&=section2_1_expressions&problem=practicemidterm4_8_randomRects

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 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); george skip drive mary john work dance

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 performing real computations in disguise. What is this problem really doing? x y z 184 1 18 4 2 12 3 13

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