TA office hours are over after this week, but Dan and Maja will still be around for the next month

Similar documents
Warm-up: what does this print?

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

What will this print?

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

APCS Semester #1 Final Exam Practice Problems

McGill University School of Computer Science COMP-202A Introduction to Computing 1

CS 113 MIDTERM EXAM 2 SPRING 2013

Creating and Using Objects

Arrays. 1 Index mapping

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

ESC101 : Fundamental of Computing

Final Exam May 21, 2003

COS 126 Midterm 1 Written Exam Fall 2011

Selected Questions from by Nageshwara Rao

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

Session 04 - Object-Oriented Programming 1 Self-Assessment

Inf1-OP. Inf1-OP Exam Review. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 16, School of Informatics

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

CS 211: Methods, Memory, Equality

Options for User Input

CS S-08 Arrays and Midterm Review 1

COS 126 Midterm 1 Written Exam Fall 2012

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Bjarne Stroustrup. creator of C++

AP Computer Science A Summer Assignment 2017

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

Practice exam for CMSC131-04, Fall 2017

Project Compiler. CS031 TA Help Session November 28, 2011

CMPSCI 187 Midterm 1 (Feb 17, 2016)

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

( &% class MyClass { }

COS 126 Exam Review. Exams overview Example programming exam Example written exam questions (part 1)

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Page 2 Page 3 Page 4 Page 5 Page 7 Total

Announcements. Written Assignment 2 Due Monday at 5:00PM. Midterm next Wednesday in class, 11:00 1:00. Midterm review session next Monday in class.

Mandatory Assignment 1, INF 4130, 2017

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

First Name: AITI 2004: Exam 2 July 19, 2004

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Array. Prepared By - Rifat Shahriyar

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017

The University of Western Ontario Department of Computer Science Computer Science 1026a Midterm Exam 2 hours

Examination Questions Midterm 1

Object Oriented Programming 2015/16. Final Exam June 28, 2016

CS 116 Week 8 Page 1

This exam is worth 25% of your final mark for the course and is made up of two parts:

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Instructions. This exam has 7 questions, worth 10 points each. You have 50 minutes.

CIS Fall 2012 Midterm, 7 June 2012, Answer Key. Miscellaneous

CS 177 Recitation. Week 8 Methods

COMP-202: Foundations of Programming. Lecture 26: Image Manipulation; Wrap-Up Jackie Cheung, Winter 2015

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

AP Computer Science Summer Work Mrs. Kaelin

Last Name: Circle One: OCW Non-OCW

Program Fundamentals

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

Introduction to Software Development (ISD) Week 3

No Aids Allowed. Do not turn this page until you have received the signal to start. Read this entire page or you ll miss the bonus question.

CSE373 Fall 2013, Midterm Examination October 18, 2013

2018/2/5 话费券企业客户接入文档 语雀

COE318 Lecture Notes Week 13 (Nov 28, 2011)

CIS 110 Fall 2016 Introduction to Computer Programming 13 Oct 2016 Midterm Exam Answer Key

************ THIS PROGRAM IS NOT ELIGIBLE FOR LATE SUBMISSION. ALL SUBMISSIONS MUST BE RECEIVED BY THE DUE DATE/TIME INDICATED ABOVE HERE

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Introduction to Computer Science I Spring 2010 Sample mid-term exam Answer key

CS159 Midterm #1 Review

Programming Languages and Techniques (CIS120)

Learning Goals LG1: Implement the peer instruction process by A) clicking in on an answer B) discussing with your team C) coming to consensus and

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Main Topics. Study Tactics. Exam Structure, Roughly. Java Fundamentals. CISC 3120 Midterm Review Guide

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Midterm 1 Review Document

Question: Total Points: Score:

Instructor: Yu Wang 11/16/2012

Introduction to: Computers & Programming: Review prior to 1 st Midterm

CSE 11 Midterm Fall 2008

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

COMP-202 More Complex OOP

COMP 202 Java in one week

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

About this exam review

University of Palestine. Mid Exam Total Grade: 100

Midterm Exam 2 CS 455, Spring 2013

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Announcements. Written Assignment 2 due today at 5:00PM. Programming Project 2 due Friday at 11:59PM. Please contact us with questions!

CIS 110 Fall 2015 Introduction to Computer Programming 6 Oct 2015 Midterm Exam Answer Key

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Tutorial 8 Date: 15/04/2014

Ryerson University Department of Electrical & Computer Engineering COE618 Midterm Examination February 26, 2013

Object Oriented Programming. Java-Lecture 6 - Arrays

Transcription:

TA office hours are over after this week, but Dan and Maja will still be around for the next month Feel free to e-mail me to request an appointment. My schedule is flexible. We will have more office hours before the exam, too. These will be announced on WebCT. 1

Recursion: solve a problem of size n by solving smaller instances of that same problem using them to build the solution for n

Example Write a recursive method m() that computes the partial sum of the following series: 1/3 + 2/5 + 3/7 + 4/9 +... + i / (2i + 1) In other words, m(2) should return result of 1/3 + 2/5 m(3) should return result of 1/3 + 2/5 + 3/7 m(0), m(-1) are undefined 3

Example Write a recursive method that computes the partial sum of the following series: 1/3 + 2/5 + 3/7 + 4/9 +... + i / (2i + 1) Idea: to compute m(n), assume I know how to compute m(n-1): m(n) = n/(2n + 1) + m(n-1) 4

Example Write a recursive method that computes the partial sum of the following series: 1/3 + 2/5 + 3/7 + 4/9 +... + i / (2i + 1) Idea: to compute m(n), assume I know how to compute m(n-1): m(n) = n/(2n + 1) + m(n-1) Base case (simplest case, no recursive call) m(1) = 1.0/3 5

Example Write a recursive method that computes the partial sum of the following series: 1/3 + 2/5 + 3/7 + 4/9 +... + i / (2i + 1) How it works for m(4): m(4) = 4/(2*4 + 1) + m(3) m(3) = 3/(2*3 + 1) + m(2) m(2) = 2/(2*2 + 1) + m(1) m(1) = base case = 1/3 6

Recursive program + try-catch blocks + command-line parameters Write a class with two methods: catalan() and main() catalan() is a recursive method that computes the n th Catalan number: C(0) = 1 C(n) = (4*n -2)/(n+1)*C(n-1) The main() method reads a String from the command-line, converts it to an integer with Integer.parseInt(), calls the catalan() method on it and prints the result. If the number of arguments is not 1, if the given argument is < 1 or if parseint() throws an exception print something like Usage: java RecursiveCatalan <positive-integer> Solution: See RecursiveCatalan.java 7

Another recursive program + arrays Write a recursive program that, given an array of integers (the integers can be negative, 0, positive), returns the largest number in the array. For solution, see Exercise 20_13.java 8

2D arrays 9

Common midterm error: jagged arrays int[][] a = new int[2][1]; int[] a0 = {1,2,3; int[] a1 = {4; a[0] = a0; a[1] = a1; The above example is legal: it compiles and runs fine. (But on the midterm, lots of students stated that this kind of operation is not allowed.) a[0] contains an address pointing to an array of size 1. We can't change the size of the array a[0] points to, but we can change the content of a[0] so that it points to another array (of any size). This is what's happening on the second-to-last line of the example above. 10

exercise: 2D arrays Write a method that accepts an int[][] called img as parameter and returns a new int[][] that is the transpose of img {{ 1, 2, 3, { 4, 5, 6 produces { { 1, 4, {2, 5, {3, 6 11

Solution public static int[][] transpose(int[][] img){ int width = img.length; int height = img[0].length; int[][] newimg = new int[height][width]; for(int y = 0; y < height; y++) for(int x = 0; x < width; x++) newimg[y][x] = img[x][y]; return newimg;

exercise: more 2D arrays [no solution provided (compile and test your code to see whether it's doing the right thing. having trouble? drop by during office hours)] Write a method that (given a 2D int array called img, (x,y) coordinates, a vertical distance and a horizontal distance), returns a new array that is a cropped version of img You may assume that all the parameters are within the bounds of the array e.g. given coordinates 2,2 and v distance 1 and h distance 0 { { 0, 0, 0, 0, produces { 0, 0, 0, 0, { 0, 0, 0, 0, { 0, 0, 0, 0 { {0, {0, {0 13

Extra exercises 14

a longer exercise for ArrayLists in picture-collection.pdf This exercise is adapted from an old assignment, and the solutions to that assignment are provided in w13-thursday.zip. You will not be able to compile or run this code, unfortunately. (Since some files are missing.) The solutions do not match the instructions in picture-collection.pdf exactly, but they're close. For good practice with file i/o, try to write code to match the specification found in picture-collection/docs/simplecollectionloader.html and picturecollection/docs/simplecollectionwriter.html 15

What will this print? public class MyInteger { private int x; public MyInteger(int myx) { x = myx; public double add(int y, int z) { System.out.println("add(i,i): " + x + " + " + y + " + " + z); return x + y + z; public double add(double y, double z) { System.out.println("add(d,d): " + x + " + " + y + " + " + z); return x + y + z; public static void main(string[] args) { MyInteger n1 = new MyInteger(1); MyInteger n2 = new MyInteger(2); double r1, r3; r1 = n1.add(3, 4); r3 = n1.add(5, 6.0); System.out.println("n1.add(3, 4) == " + r1); System.out.println("n2.add(3, 4) == " + n2.add(3, 4)); System.out.println("n3.add(5, 6.0) == " + r3);

Solution add(i,i): 1 + 3 + 4 add(d,d): 1 + 5.0 + 6.0 n1.add(3, 4) == 8.0 add(i,i): 2 + 3 + 4 n2.add(3, 4) == 9.0 n3.add(5, 6.0) == 12.0

Command-line parameters What is displayed by the following program if we run it like this: java Cat java Cat 4 5 java Cat \ 4\ java Cat 1 2 3 public class Cat{ public static void main(string[] elephants){ String result = ""; for(int i = 0; i < elephants.length; i++){ result = result + elephants[i]; System.out.println(result);

This is the output at my terminal mfrydr@jack:~$ java Cat 1 2 3 123 mfrydr@jack:~$ java Cat mfrydr@jack:~$ java Cat \"4\" "4" mfrydr@jack:~$ java Cat "4" 5 45

class UselessObject{ int evennumber; int oddnumber; public UselessObject(){ evennumber = 6; oddnumber = 1; public int getsum(){ return evennumber + oddnumber; public void incrementall(int step){ if(step%2 == 0){ evennumber = evennumber + step; oddnumber = oddnumber + step;

What is printed when we run ObjectExercise? public class ObjectExercise{ public static void main(string[] args){ UselessObject a = new UselessObject(); UselessObject b = new UselessObject(); a.evennumber = -4; b.incrementall(4); System.out.println(a.getSum() - b.getsum());