Week 6 CS 302 Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
CSCI 160 Midterm Review Rasanjalee DM.
Advertisements

COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Tallying and Traversing Arrays reading: 7.1 self-checks: #1-9 videos:
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Procedural programming in Java Methods, parameters and return values.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
(Java Looping and Conditional Statements). Flow of control Sequential Executes instructions in order Method Calls Transfer control to the methods, then.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
CS Week 14 Jim Williams, PhD.
CS Week 2 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
Methods and Parameters
CS 302 Week 15 Jim Williams, PhD.
CS 200 Branches Jim Williams, PhD.
CS Week 4 Jim Williams, PhD.
CS 302 Week 11 Jim Williams, PhD.
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
CS Week 8 Jim Williams, PhD.
CS 200 Using Objects Jim Williams, PhD.
CS 177 Week 15 Recitation Slides
CS Week 6 Jim Williams, PhD.
CS Week 7 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
Building Java Programs
CS Week 3 Jim Williams, PhD.
An Introduction to Java – Part I, language basics
CS 302 Week 8 Jim Williams, PhD.
CS Week 9 Jim Williams, PhD.
Building Java Programs
CS 200 Objects and ArrayList
Review for Final Exam.
CS 302 Week 9 Jim Williams.
CS 200 More Primitives, Objects, Branches and Methods
CS 200 Loops Jim Williams, PhD.
CS 200 Primitives and Expressions
CS 200 Arrays Jim Williams, PhD.
CS Week 4 Jim Williams, PhD.
CS 200 Primitives and Expressions
CS 200 Additional Topics and Review
CS 200 Methods, Using Objects
Review for Final Exam.
CS 200 Objects and ArrayList
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Building Java Programs
Peer Instruction 4 Control Loops.
CS Week 2 Jim Williams, PhD.
Building Java Programs
CS Week 3 Jim Williams, PhD.
Week 7 CS 302 Jim Williams.
Ben Stanley for gAlpha gALPHA free, four-week venture-creation workshop designed to help entrepreneurially-minded students and technologists create high-growth.
CS 200 Objects and ArrayList
First Semester Review.
CS302 Week 6 Jim Williams.
Presentation transcript:

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) What is the location of the exam?

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? mPrint call then mPrint definition static void mPrint() { System.out.println("my print"); } public static void main(String []args) { mPrint(); B is the correct answer

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 count is a parameter (zyBooks) or formal parameter the number 23, for example is an argument (also called an actual parameter).

Compile or Not Compile? static int mPrint(String str) { compiles as part of a class will not compile as part of a class missing a statement invalid data type static int mPrint(String str) { System.out.println( "my:" + str); } C is the best answer. it is true that mPrint method must be part of a class but that is not enough to make it compile since mPrint has int as the return type, there must be a return statement that returns a value of type int. One could argue that the mistake is that changing int to void would be a better solution as it is not clear what int value should be returned.

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

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

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

Which is called first: calc or println? error static int calc(int num) { num -= 33; return num; } public static void main(String []args) { int n = 55; System.out.println( calc( n)); put a print statement within the calc method to see if it is called before the println method.

Multiple parameters and overloading

What prints out? print(double) 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 try it.

What prints out? print(double) 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 try it.

What prints out? void print int print invalid overloading other error 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 try it.

What prints out? print(double) 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 try it.

Memory Areas Stack local, parameters in Stack Frame Heap instances (object), arrays static Code review 1 passing primitive to method changing parameter in method 2 passing array to method changing contents of array in method 3 passing array to method changing parameter to another array in method 4 passing a string to method calling string method in method public class ClassNameHere { static void method1(int num) { num = 5; } static void method2(int []listA) { listA = new int[3]; listA[1] = 10; static void method3(StringBuffer name) { name //name.toUpperCase(); public static void main(String[] args) { String name = "Helen"; method3( name); System.out.println( name); // int [] listB = {4, 6, 8}; // method2( listB); // System.out.println( listB[1]); // int num = 4; // method1( num); // System.out.println("num=" + num);

Where in memory is variable z? stack heap it is local Error static void main(String []args) { int [][] z = new int[r][c]; } A and C are both correct. Local variables are those declared within methods and are stored in the stack.

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

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

What prints out? num:10 list:[1, 2, 11] num:11 list:[1, 2, 3] 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, 3] list:[1, 2, 10] try it.

What prints out? num:10 list:[1, 2, 11] list:[4,5,10] list:[1, 2, 3] 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)); try it.

Review Questions Expression Evaluation Scanner Arrays Loops

What is result? boolean flag = true; boolean result = flag = false || flag; Suggestion: draw parenthesis to show precedence and work systematically. try it.

Scanner Scanner input = new Scanner("1 \ntwo \n 2\n\n"); int a = input.nextInt(); if ( input.hasNextInt()) { int b = input.nextInt(); } else { input.nextLine(); } String line = input.nextLine(); System.out.println("#" + line + "#");

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

What is the value of sum? double sum = 0.0; 6.8 9.9 12.9 runtime error double sum = 0.0; double []nums = {3.1, 4.5, 2.3}; for ( int i = 1; i <= nums.length; i++) { sum += nums[i-1] + 1; } try it.

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

Will this initialize the array to all 1's? yes no don't know Error 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; try it.

Will this initialize the array to all 1's? yes no don't know Error 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; try it.

Will this initialize the array to all 1's? yes no don't know Error 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; try it.