CS 200 Arrays, Loops and Methods

Slides:



Advertisements
Similar presentations
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Advertisements

Arrays Chapter 6 Chapter 6.
15 Sorting1June Sorting CE : Fundamental Programming Techniques.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
Java Unit 9: Arrays Declaring and Processing Arrays.
CS1101: Programming Methodology Aaron Tan.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
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.
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
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.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Arrays Chapter 7.
Chapter VII: Arrays.
Values vs. References Lecture 13.
Suppose we want to print out the word MISSISSIPPI in big letters.
Arrays (review) CSE 2011 Winter May 2018.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Alg2_1c Extra Material for Alg2_1
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
CS Week 14 Jim Williams, PhD.
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Chapter 6 Arrays.
CS 200 Branches 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 Week 6 Jim Williams, PhD.
CS Week 7 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
CNG 140 C Programming (Lecture set 8)
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.
CS 302 Week 9 Jim Williams.
CS 200 More Primitives, Objects, Branches and Methods
Building Java Programs
CS 200 Loops Jim Williams, PhD.
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Data Structures (CS212D) Week # 2: Arrays.
CS 200 Primitives and Expressions
CS 200 Arrays Jim Williams, PhD.
Arrays and Array Lists CS 21a.
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.
Building Java Programs
Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort. Complexity analysis.
Single-Dimensional Arrays chapter6
CS 200 Objects and ArrayList
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Peer Instruction 4 Control Loops.
CS Week 2 Jim Williams, PhD.
Building Java Programs
CS31 Discussion 1H Fall18: week 6
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 Arrays, Loops and Methods Jim Williams, PhD

This Week Piazza: Hackathons Team Lab: Arrays and Hangman drawing diagrams (bring paper and pencil) BP1 Milestone 1 due Thursday Lecture: More Arrays and Methods

BP1 Notes Process: 3 milestones then further testing Concepts: Use only material from Chapters 1-8 Teams: Once you submit as a team for Milestone 1 then you are a team for the entire project. If your team is not working you must dismantle your team by notifying an instructor by email by 11:59pm on Milestone 1. Then you work alone for the project.

Lab 6: Loops & Debugging Exercise TA: 2, Scanner "flushing the buffer"

Key Concepts Unit Testing Memory Access Drawing a picture of memory for an array Looping through multidimensional arrays appropriate use of .length Common Array algorithms

Unit Testing (method) Writes method to meet requirements Verifies that method meets requirements These are different perspectives and can be challenging to switch between. But the better you can develop this skill of switching between perspectives the more reliable your code will likely be.

Requirements /** * 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; }

Which are true? 1,2,3,4 String []arr = new String[4]; arr[2] = new String("hello"); arr[3] = arr[2]; arr[0] = "hello"; arr[1] = "hello"; System.out.println( arr[2] == "hello" ); //1 System.out.println( arr[2].equals( "hello")); //2 System.out.println( arr[3] == arr[2]); //3 System.out.println( arr[3].equals(arr[2])); //4 1,2,3,4 1,2,4 2,3,4 2,4 try it

Will this set all elements to 3? final int START_VALUE = 3; int [] list = new int[5]; for ( int i = 0; i < list.length -1; i++) { list[i+1] = START_VALUE; } Yes Not first Not last Error try it

What does this code do? double value = 0.0; find minimum value find maximum value compiler error logic error double value = 0.0; double [] nums= {3.0,5.0,2.3,4.1}; for ( int i = nums.length; i > 0; i--) { if ( nums[i-1] > value) { value = nums[i-1]; } try it.

Which swaps the ith and i+1th values? temp = list[i]; list[i] = list[i+1]; list[i+1] = temp; temp = list[i+1]; list[i] = temp; list[i+1] = list[i]; try it

What is print out? char [] list1 = new char[]{'a', 'b', 'c'}; true false char [] list1 = new char[]{'a', 'b', 'c'}; char [] list2 = new char[]{'b', 'b', 'c'}; System.out.println( list1.length == list2.length); System.out.println( list1[1] == list2[1]); try it.

What is print out? System.out.println( list1 == list2); true false char [] list1 = new char[]{'a', 'b', 'c'}; char [] list2 = new char[]{'a', 'b', 'c'}; System.out.println( list1 == list2); System.out.println( list1.equals( list2)); try it. Note: equals method depends on which equals method is called. This equals is probably the one inherited from Object and simply compares references See java.util.Arrays for equals methods to compare the contents of arrays. See java.util.Arrays for equals methods to compare the contents of arrays.

Arrays of Arrays Also called: Multi-dimensional arrays

Which picture of 2-D array is more accurate? int [][] board = new int[3][2]; Depends on your purpose. The first is simpler conceptually and for many purposes, fine. However, when working with code the 2nd is more technically accurate and important to understand.

How many elements will this array hold? 5 10 25 Error int [][] board = new int[5][5]; try it.

What is the data type of board[3][1]? int [][] board = new int[5][5]; int int [] reference to int can't access board[3][1] int element data type

What is the data type of board[2]? int [][] board = new int[5][5]; int int [] reference to int can't access board[2] int [] The declaration is: int [][] board. So board dereferenced once (board[2]) results in: int []

What values will elements in this array have, initially? boolean [][] board; board = new boolean[5][5]; false true none, they must be initialized first. try it.

Which is correct way to set an element value? int [][] board = new int[5][5]; How to set element 0,0 to 5? board[0][0] = 5; board[0,0] = 5; board{0,0} = 5; error try it.

How many elements in this array? int [][] board = {{1,2},{4,5,6}}; 5 6 ragged arrays are not valid error try it.

How would you access "Hi."? responses[1][1][1] Error String [ ][ ][ ] responses = { {{"hello"}, { "How do you do.", "Hi."}}, {{"always"}, { "When?", "Really, always?"}} }; try it.

Today More Arrays & Methods

Draw a Diagram int [][] board; board = new int[3][2]; How do you find the number of rows? How do you find the number of columns?

Draw a Diagram int [][] board; board = new int[][]{{1,2},{4,5,6}};

Will this initialize the array to all 1's? yes no don't know Error int [][] board; board = new int[][]{{1,2},{4,5,6}}; 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 int [][] board; board = new int[][]{{1,2},{4,5,6}}; 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 int [][] board; board = new int[][]{{1,2},{4,5,6}}; for ( int i = 0; i < b.length; i++) for ( int j = 0; j < b[ i ].length; j++) b[ i ][ j ] = 1; try it.

Is array of int at row 1 accessible? yes no don't know Error int [][] board; board = new int[][]{{1,2},{4,5,6}}; board[1] = null;

Question 1 minute to answer I want to keep the results to analyze later

What will print out? public static void methodA(int [] list) { } public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); 1 9 list[0] try it.

Memory Access Local variables and parameters (stored on stack) are only visible within the method itself. to share value, return value from method, caller captures and uses Instances/objects (stored on heap) are available anywhere using the reference. Follow the reference from inside or outside the method to shared memory area (heap).

Is it possible for methodC to change the elements in the array? public static void main(String []args) { final char [] items = new char[]{'c','b','a'}; methodC( items); } It is NOT possible for methodC to change the local variable items, regardless of whether items is final or not. It IS possible for methodC to change the array contents since the reference of the array is passed to methodC. Both main and methodC, having the reference, can access and change the array contents.

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

Can you call this method to get the new array? public static void createList(int [] list) { list = new int[10]; } yes no reference lost Error try it.

Does main use the array from createList? public static int [] createList(int size) { return new int[size]; } public static void main(String[]args) { int [] list; createList( 10); list[3] = 10; yes no reference lost Error try it.

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

What will print out? 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]); 3 5 9 try it.

3 Questions 1 minute each

What will print out? public static void methodA(int [] list) { list = new int[3]; list[0] = 9; } public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); 1 9 list[0] try it. In method, array reference changed prior to accessing array contents. Changes to array contents are only visible within the method since the new array is not returned.

What will print out? public static void methodA(int [] list) { list = new int[3]; } public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); 1 9 list[0] try it. In method, array reference followed and contents changed, prior to changing array reference. Change in array visible outside the method

What will print out? public static void methodA(int [] list) { } public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); 1 9 list[0] try it. In method, array reference followed and contents changed. Change visible anywhere reference to array is accessed since array contents are on the heap.

Common Algorithms for Arrays Searching Sorting Palindrome?

Sorting Demo 9, 6, 4, 5, 8, 2 import java.util.Arrays; public class BubbleSort { public static void swapInts(int a, int b) { int temp = a; a = b; b = temp; } public static void main(String[] args) { int x = 1; int y = 2; System.out.println("x = " + x + ", y = " + y); swapInts(x, y); int[] list = new int[]{10, 5, 3, 7, 9}; printArray(list); swapInts(list[0], list[4]); swapElements(list, 0, 4); bubbleSort(list); addElementAtEnd(list, 12); list = addElementAtEnd(list, 4); public static void printArray( int [] arr) { System.out.println( Arrays.toString( arr)); public static void swapElements(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; public static void bubbleSort(int[] arr) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length -1; j++) { if (arr[j] > arr[j+1]) { swapElements(arr, j, j+1); public static int[] addElementAtEnd( int[] arr, int elem) { int newLength = arr.length + 1; int[] newArray = new int[newLength]; newArray[i] = arr[i]; newArray[newArray.length - 1] = elem; return newArray;