Week 6 CS 302. Jim Williams, PhD

Similar documents
CS 200 Arrays, Loops and Methods Jim Williams, PhD

CS Week 14. Jim Williams, PhD

CS Week 5. Jim Williams, PhD

CS Week 2. Jim Williams, PhD

CS 200 Using Objects. Jim Williams, PhD

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

CS111: PROGRAMMING LANGUAGE II

CS Week 13. Jim Williams, PhD

CS 206 Introduction to Computer Science II

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

Chapter 6 Recursion. The Concept of Recursion

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Lecture 5: Methods CS2301

CS 302 Week 9. Jim Williams

CS Week 11. Jim Williams, PhD

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

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

CP122 CS I. Iteration

CS 211: Recursion. Chris Kauffman. Week 13-1

Bjarne Stroustrup. creator of C++

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

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

CS-201 Introduction to Programming with Java

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

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

CS 200 Command-Line Arguments & Exceptions Jim Williams, PhD

CS 307 Midterm 2 Spring 2008

Points off Total off Net Score. CS 314 Final Exam Spring 2016

Recursion. Recursion [Bono] 1

CS 200 File Input and Output Jim Williams, PhD

CS111: PROGRAMMING LANGUAGE II

Decision-Making and Repetition

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

1. An operation in which an overall value is computed incrementally, often using a loop.

University of Palestine. Mid Exam Total Grade: 100

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

CT 229 Methods Continued

Midterm Examination (MTA)

CS159. Nathan Sprague

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS1083 Week 2: Arrays, ArrayList

CS 455 Midterm Exam 1 Fall 2015 [Bono] Thursday, Oct. 1, 2015

CPSC 219 Extra review and solutions

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Question Points Score

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

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

Dynamic Programming. See p of the text

CS 200 Objects and ArrayList Jim Williams, PhD

AP Computer Science A Unit 7. Notes on Arrays

Spring 2010 Java Programming

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

CS 113 MIDTERM EXAM 2 SPRING 2013

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Advanced Computer Programming

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

AP CS Unit 3: Control Structures Notes

Conditional Execution

CS 312 Exam 2 Fall KG Kris Megan Roman Sonika

AP Computer Science Unit 1. Programs

CS 211: Methods, Memory, Equality

Unit 10: Sorting/Searching/Recursion

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

CS 455 Midterm Exam 1 Spring 2015 [Bono] Thursday, Feb. 19, 2015

Java Coding 3. Over & over again!

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Department of Computer Science Purdue University, West Lafayette

CS170 Introduction to Computer Science Midterm 2

Computer Programming: C++

More on methods and variables. Fundamentals of Computer Science Keith Vertanen

CIS 110: Introduction to Computer Programming

CS1 Recitation. Week 2

Selected Questions from by Nageshwara Rao

CONDITIONAL EXECUTION

Introduction to Software Development (ISD) Week 3

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Recursion. Lecture 13. Recursion

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Examination Questions Midterm 1

Chapter 17 Recursion

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

1 Short Answer (15 Points Each)

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Variable Scope. The variable scope is the range of the program where the variable can be referenced.

Assignment 1 is due tonight at 11:59pm. Assignment 2 is due next Thursday at 11:59pm

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

There are some situations in which recursion can be massively inefficient. For example, the standard Fibonacci recursion Fib(n) = Fib(n-1) + Fib(n-2)

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Recursion: Recursion: repeated curses

Lecture 6: Recursion RECURSION

Lecture 10: Recursion vs Iteration

Procedures and Stacks

Transcription:

Week 6 CS 302 Jim Williams, PhD

This Week Lab: Multi-dimensional Arrays Exam 1: Thursday Lecture: Methods Review

Midterm Exam 1 What is the location of the exam? 3650 Humanities 125 Ag Hall 272 Bascom Other (due to conflicts)

Midterm Exam - Thursday Bring ID and #2 pencil Exam seating directly in front/behind, 1 empty seat to each side Multiple choice - focus on reading Java Write on your exam any assumptions Review Questions posted on Piazza

P2 - Game of Life Key Concepts arrays static methods parameter passing

Defining and Calling Methods

mprint - which is Call, Definition? static void mprint() { System.out.println("my print"); public static void main(string []args) { mprint(); mprint call then mprint definition mprint definition then mprint call

Is count: Argument or Parameter? public static void main(string []args) { int num = 10; printcount( 23); printcount( num+3); static void printcount(int count) { System.out.println( count); argument parameter actual parameter formal parameter

Compile or Not Compile? static int mprint(string str) { System.out.println( "my:" + str); compiles as part of a class will not compile as part of a class missing a statement invalid data type

What prints out? static void calc(int num) { num = 3; public static void main(string []args) { int n = 5; calc( n); System.out.println( n); 3 5 35 error

What prints out? public static void main(string []args) { int n = 5; calc( n); System.out.println( n); static int calc(int num) { return 3; 3 5 35 error

What prints out? static int calc(int num) { return 3; public static void main(string []args) { int n = 5; n = calc( n); System.out.println( n); 3 5 35 error

Which is called first: calc or println? static int calc(int num) { num -= 33; return num; public static void main(string []args) { int n = 55; System.out.println( calc( n)); calc println error

Multiple parameters and overloading

What prints out? static void print( double value) { System.out.println( "print(double)"); static int print( int value) { System.out.print( "print(int)"); return 1; public static void main( String [] args) { print( 10); print(double) print(int) invalid overloading error

What prints out? static void print( double val1, double val2) { System.out.println( "print(double)"); public static void main( String [] args) { double value = 10.0; print( value, 20); static void print( int val1, int val2) { System.out.println( "print(int)"); print(double) print(int) invalid overloading error

What prints out? static void print( double val1, double val2) { System.out.println( "void print"); static int print( double num1, double num2) { System.out.print( "int print"); return 1; public static void main( String [] args) { print( 10.0, 20.0); void print int print invalid overloading other error

What prints out? static void print( double val1, double val2) { System.out.println( "print(double)"); static int something( int val1, int val2) { System.out.print( "something(int)"); return 1; public static void main( String [] args) { print( 10, 20); print(double) something(int) invalid overloading error

Memory Areas Stack Frame Stack - local, parameters Heap - instances (object), arrays static Code

What prints out? static void change( int num, int [] list) { num = 11; list[2] = num; public static void main( String [] args) { int [] list = {1,2,3; int num = 10; change( num, list); System.out.println("num:" + num + "\nlist:" + Arrays.toString( list)); num:10 list:[1, 2, 11] num:11 list:[1, 2, 11] num:10 list:[1, 2, 3] num:10 list:[1, 2, 10]

What prints out? static void change( int num, int [] list) { list = new int[]{4,5,6; list[2] = num; public static void main( String [] args) { int [] list = new int[] {1,2,3; int num = 10; change( num, list); System.out.println("num:" + num + "\nlist:" + Arrays.toString( list)); num:10 list:[1, 2, 11] num:10 list:[4,5,10] num:10 list:[1, 2, 3] num:10 list:[1, 2, 10]

What is the value of num? class Stuff { final static int MAX_VALUE = 10; //allowed static int num = 6; //NOT allowed in P2 static void change( int n) { num = n + 1; public static void main( String [] args) { int num = MAX_VALUE; change( num); System.out.println("num:" + num); System.out.println("Stuff.num:" + Stuff.num); num: 10 Stuff.num: 6 num: 10 Stuff.num: 10 num: 10 Stuff.num: 11 error

Where in memory is variable z? static void main(string []args) { int [][] z = new int[r][c]; stack heap it is local Error

Can you call this method to get the new array outside the method? yes static void createboard(int [][] board) { board = new int[3][3]; no sometimes Error

Can you call this method to get the new array? static int [][] createboard(int r, int c) { return new int[r][c]; yes no reference lost Error

Will this initialize the array to all 1's? public static void main(string []args) { int [][] board = {{2,3,{4,3,2; initboard( board); static void initboard(int[][] board) { for ( int i = 0; i < 2; i++) for ( int j = 0; j < 3; j++) board[ i ][ j ] = 1; yes no don't know Error

Will this initialize the array to all 1's? public static void main(string []args) { int [][] board = {{2,3,{4,3,2; initboard( board); static void initboard(int[][] b) { for ( int i = 0; i < b[i].length; i++) for ( int j = 0; j < b[j].length; j++) b[ i ][ j ] = 1; yes no don't know Error

Will this initialize the array to all 1's? public static void main(string []args) { int [][] board = {{2,3,{4,3,2; initboard( board); static void initboard(int[][] b) { for ( int i = 0; i < b.length; i++) for ( int j = 0; j < b[ i ].length; j++) b[ i ][ j ] = 1; yes no don't know Error

What do a and b have in common? public static void main(string []args) { ma(); mb(); static void ma() { int a = 10; static void mb() { int b; System.out.println( b); may be the exact same memory location looking at b will reveal a (unintentially) nothing Error

How many times is fib method called? static int fib(int num) { if ( num <= 1) return 1; else return fib( num -1) + fib( num -2); public static void main(string[] args) { System.out.println("fibonacci 4: " + fib(4)); 1 3 9 Error

Recall Fibonacci Sequence 1 1 2 3 5 8 13 21 Each is the sum of the previous 2. https://en.wikipedia.org/wiki/fibonacci_number

Iterative solution public static void main(string[] args) { int n = 10; int [] fib = new int[n]; fib[0] = 1; fib[1] = 1; for (int i = 2; i < n; i++) { fib[i] = fib[i-1] + fib[i-2]; System.out.println("fib "+n+"= " + fib[n-1]);

Review Questions Scanner Arrays Loops

What is value of str? String str = ""; String [] words = {"a", "b", "c"; for ( int i = 0; i < words.length; i++) { str += "." + words[i]; "" "a.b.c." ".a.b.c" error

What is the value of sum? double sum = 0.0; double []nums = {3.1, 4.5, 2.3; for ( int i = 0; i > nums.length; i++) { sum += nums[i]; 0.0 9.9 compiler error logic error

What does this do? int [] list = new int[4]; for ( int i = 0; i <= list.length; i++) { list[i] = i * 2; set each element to i * 2 0, 2, 4, 8 compiler error runtime error