CS 200 - Week 8 Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Advertisements

Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
JAVA Arrays. Objectives Be able to declare and initialize arrays Be able to conceptualize (draw) how arrays are represented in computer memory. Be able.
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.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Object-Oriented Programming Simple Stack Implementation.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
Primitive data types Lecture 03. Review of Last Lecture Write a program that prints the multiplication table of 5. class MultiplicationTable { public.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Values vs. References Lecture 13.
CS Week 10 Jim Williams, PhD.
Building Java Programs
Building Java Programs
CS Week 14 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
CS 200 Branches Jim Williams, PhD.
CS Week 4 Jim Williams, PhD.
CS Week 11 Jim Williams, PhD.
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
CSC 253 Lecture 8.
Arrays Declare the Array of 100 elements Notes
CS 200 Using Objects Jim Williams, PhD.
CS Week 6 Jim Williams, PhD.
CS Week 7 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
יסודות מדעי המחשב – תרגול 4
CSC 253 Lecture 8.
An Introduction to Java – Part I, language basics
Building Java Programs
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 302 Week 8 Jim Williams, PhD.
CS Week 9 Jim Williams, PhD.
Week 6 CS 302 Jim Williams, PhD.
CS 200 Objects and ArrayList
Review for Final Exam.
Building Java Programs
Building Java Programs
CS 302 Week 9 Jim Williams.
CS 200 More Primitives, Objects, Branches and Methods
CS 200 Loops Jim Williams, PhD.
CS 180 Assignment 6 Arrays.
CS 200 Primitives and Expressions
Compiler Design Second Lecture.
Lecture 9: Arrays Building Java Programs: A Back to Basics Approach
class PrintOnetoTen { public static void main(String args[]) {
CS 200 Arrays Jim Williams, PhD.
CS Week 4 Jim Williams, PhD.
CS 200 Primitives and Expressions
CS 200 Additional Topics and Review
CS2011 Introduction to Programming I Multidimensional Arrays
Review for Final Exam.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 200 Objects and ArrayList
Arrays in Java.
Building Java Programs
CS Week 2 Jim Williams, PhD.
Question 1a) What is printed by the following Java program? int s;
Building Java Programs
Two-Dimensional Arrays
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
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
CS302 Week 6 Jim Williams.
Week 7 - Monday CS 121.
Presentation transcript:

CS 200 - Week 8 Jim Williams, PhD

This Week Team Lab: Arrays and Hangman Lecture: drawing diagrams (bring paper and pencil) Lecture: More Arrays and Methods Unit Testing

How To Study?

Question 37 / Lab 2, Exercise B, L. String exprStr1 = 4 + 1 + " = 4 + 1"; String exprStr2 = "4 + 1 = " + 4 + 1; String exprStr3 = "4 + 1 = " + (4 + 1);

Question 6 / Lab 3, Exercise B, K: Random numGenerator = new Random(); int num1 = numGenerator.nextInt(10); int num2 = numGenerator.nextInt(10); int num3 = numGenerator.nextInt(10); //Does the value returned from nextInt method call change each time? //What are the possible values returned?

Question 9 / Lab 4, Exercise B, G: boolean flag = false; if ( flag = true) { System.out.println("flag is true"); } else { System.out.println("flag is false"); }

Question 11 / Lab 5, Exercise B, N: String str2 = null; if ( str2.length() >= 1 && str2 != null) { System.out.println( str2); } else { System.out.println("null or zero length string"); }

Draw a picture int [] items = new int []{5,6,7};

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

Is it possible for methodC to change the contents of the array? public static void main(String []args) { char [] items = new char[]{'c','b','a'}; methodC( items); }

Where in memory is variable z? stack heap it is local Error public static void main(String []args) { int r = 3; int c = 4; 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? yes no reference lost Error public static int [] createList(int size) { return new int[size]; } public static void main(String[]args) { int [] list = createList( 10); list[3] = 10; try it.

Can main see the changed value within list? yes no reference lost Error public static void changeList(int [] list) { if ( list.length > 0) { list[0] = 10; } public static void main(String []args) { int [] z = new int[5]; changeList( z); System.out.println( z[0]); try it.

Can you call this method to get the new array? yes no reference lost Error public static void createList(int [] list) { list = new int[10]; } 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.

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.

Can main see the changed value within list? yes no reference lost Error public static void changeList(int [][] list) { list[0] = new int[3]; list[0][1] = 9; } public static void main(String []args) { int [][] z = new int[5][5]; changeList( z); System.out.println( z[0][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[][] 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.

Unit Testing (method) Developer Writes method to meet specification Tester Verifies that method meets specification

Specification /** * This method returns the number of 9s in the table. * @param table a 2 dimensional array of int * @return the number of 9s in table */ public static int num9s(int [][]table) { return -1; }