Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays
|
|
- Clifton McKinney
- 6 years ago
- Views:
Transcription
1 Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays
2 Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22 nd Assignment 2 (LECTURE 8) GRADED! Section 1: Wednesday, January 31 st Assignment 1 Revision (LECTURE 10) GRADED! Section 1: Wednesday, February 7 th Assignment 2 Revision (LECTURE 12) GRADED! Section 1: Wednesday, February 14 th Assignment 3 (LECTURE 13) GRADED! Section 1: Wednesday, February 21 st Assignment 3 Revision (LECTURE 16) GRADED! Section 1: Monday, March 5 th Assignment 4 (LECTURE 18) NO REVISION! Section 1: Monday, March 12 th The Fickle Finger of Fate 2
3 Today s Topics Arrays (Recap) Arrays as Parameters Returning an Array (we'll cover again in next class) Assignment 4 Basic and Advanced Overview WALK-THROUGH: ICE
4 And now... The Quiz
5 RECAP: Arrays
6 Recap: Arrays What is an Array? Primitive variables are designed to hold only one value at a time. Arrays allow us to create a collection of like values that are indexed. An array can store any type of data but only one type of data at a time. An array is a list of data elements.
7 Recap: Arrays Let s pretend we have a list of five (5) grades we d like to store in memory. We could declare the value for each grade one at a time like this: int grade1 = 100; int grade2 = 89; int grade3 = 96; int grade4 = 100; int grade5 = 98; The five grades are then stored in memory wherever there happens to be room. The address in memory where each grade is store is it s declared name (e.g., grade1) and the data stored there is the integer value itself (e.g., 100). To access each grade number and do something with it (e.g., display it), we d call up the memory location by its assigned (declared) name: System.out.println("The first grade is: " + grade1);
8 Recap: Arrays We could, however, simplify our work by doing the same thing with an array. Instead of declaring each integer one at a time, we could declare the whole shebang at the same time, like so: grades = new int[5]; Initially, this creates a location in memory to hold the five elements that make up the array, automatically initializing each of those storage spaces to zero. All you have to do now is fill those elements. In memory, the array elements are mostly stored in sequential order Even though you declared an array with [5] elements, the count of the elements starts with [0] grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; Of course, you could have also declared and set them up like this all in one step: int[ ] grades = 100, 89, 96, 100, 98 ;
9 Another way of thinking about it in the abstract int [] grades = new int[5]; grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; The numbers inside the square braces [ ] are the "slot" numbers The numbers to the right of the assignment operator '=' are the data values going into the slots
10 Another way of thinking about it in the abstract The numbers inside the square braces [ ] are the "slot" numbers The numbers to the right of the assignment operator '=' are the data values going into the slots
11 int [] grades = new int[5]; grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; Length (size declarator) int [] grades = new int[5]; grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; Index int [] grades = new int[5]; grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; Subscripts
12 Recap: Arrays Comparing individual integer declarations to an array of integers: int grade1 = 100; int grade2 = 89; int grade3 = 96; int grade4 = 100; int grade5 = 98; In memory, the individual integers are stored wherever there happens to be an available space System.out.println("The first grade is: " + grade1); grades = new int[5]; grades[0] = 100; grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; In memory, the array elements are mostly stored in sequential order System.out.println("The first grade is: " + grade[0]);
13 Recap: Arrays Because array elements are numbered sequentially, it is a no-brainer to either populate the data in the array, or to call up and access the data in the array using Scanner keyboard = new Scanner(System.in); // Original code thanks to Kamran Majidimehr! int counter = 0; int arraylength = 0; System.out.println("How many grades would you like to enter?"); arraylength = keyboard.nextint(); System.out.println("You have indicated that you'd like to enter " + arraylength + " grades.\nplease enter grades now, one at a time."); int countdown = arraylength; double average = 0; double grades[] = new double [arraylength]; Loops while( counter < arraylength ) grades[counter] = keyboard.nextdouble(); System.out.println("(" + (counter + 1) + ") You entered: " + grades[counter] + " [There are " + (countdown - 1) + " grades remaining]"); counter++; countdown--; With loops you can either enter data manually when prompted or enter data automatically based on the particular requirements of the data going into the array
14 PASS Arrays as Parameters
15 Arrays are known as "Reference Types" Now that we have discussed the syntax for working with arrays, we should examine why array types are known as reference types. As we discussed earlier in the quarter, all the Java primitive types have well-defined standard sizes, so all primitive values can be stored in a fixed amount of memory (between 1 and 8 bytes, depending on the data type). But array types do not have a standard size, and they often require quite a bit more memory than eight bytes. For this reason, Java does not work with arrays directly. Instead, it works with references to arrays. Because Java handles arrays by reference, array types are known as reference types. In contrast, Java handles values of the primitive types directly, that is to say by value. A reference to an array is simply some fixed-size value that refers to the array in some way. When you assign an array to this value, you are actually setting that value to hold a reference to that array. Similarly, when you pass an array to a method, what really happens is that the method is given a reference to the array through which it can work with the array in some way. Typically, this reference is the memory address at where the array is stored. Let's have a look at what this all means in the grand scheme of things
16 ANALOGY: Storefront and Warehouse
17 ANALOGY: Storefront and Warehouse
18 ANALOGY: Storefront and Warehouse My products are stored In Warehouse # 43015
19 Arrays as Parameters shortarray created longarray created Passing shortarray by name Passing longarray by name
20 Arrays as Parameters class ArrayHelper - PrintArray method main - shortarray - longarray
21 Arrays as Parameters Passing an array as a parameter This method has been setup to expect an array to be passed to it, and will use the array space called arrayname to point to the passed array data when it is.
22 Arrays as Parameters
23 Arrays as Parameters
24 Arrays as Parameters Name Only Name Only
25 Arrays as Parameters Passing an array to a method behaves differently than passing a primitive data type. The array values aren't copied directly to the method, but rather a reference to the address where those values can be found. 1, 3, 5 But how does this really work in memory? How does the method actually know which array to point to and use?
26 Arrays as Parameters Don t worry about any of this memory, stack, or heap information as it won t show up on the Final Exam Is allocated when program loads; temporary Is allocated when program runs; persistent
27 Arrays as Parameters shortarray 1, 3, 5 longarray 1, 2, 3, 5, 7 (1) PrintArray method is called and and checks name in parameter which is a named storage space in the memory heap (2) PrintArray than copies the array by its address and then plugs it through the arrayname placeholder where it might be used by the method. So, when you pass an array as a parameter, you re actually passing a copy of the address of where the array data is located. Depending how you set up the method to work with the array, the original array data may or may not be changed. Let's have a look at what this "may or may not" means
28 AN ABSTRACTION: How Does This Look In Memory? Before either the shortarray or longarray array is passed by name, the arrayname array set up in the method doesn't have a "working" address or value per se, since it will be initialized from the referenced memory address of the name of the array that it is passed.
29 AN ABSTRACTION: How Does This Look In Memory? Although the name of the array that is being passed is a "reference" it is still "passing by value" except in this case that value is a copy of the address of the named array.
30 Arrays as Parameters import java.util.*; class Change extends Object public void AddFives(int arrayname[]) for(int j = 0; j < arrayname.length; j++) arrayname[j]+=5; // Same as: arrayname[j] = arrayname[j] + 5; public class PassArrayChangeValues extends Object public static void main(string[] args) Change DoIt = new Change(); int [] myarray = new int[5]; myarray[0] = 2; myarray[1] = 4; myarray[2] = 6; myarray[3] = 8; myarray[4] = 10; System.out.println("myArray contains:"); for(int i = 0; i < myarray.length; i++) System.out.println("Slot " + (i + 1) + " is: " + myarray[i]); DoIt.AddFives(myArray); System.out.println(); System.out.println("myArray NOW contains:"); for(int i = 0; i < myarray.length; i++) System.out.println("Slot " + (i + 1) + " is: " + myarray[i]); EXAMPLES: PassArrayChangeValues.java ArrayTester.java
31 Returning an Array I m going to go over the Lecture 18 part of this now, and repeat it again on Monday where you will do the ICES for Returning an Array at that time.
32 Passing and Returning Arrays Passing Arrays as Arguments Arrays are objects. Their references can be passed to methods like any other object reference variable. showarray(numbers); Address public static void showarray(int[] array) for (int i = 0; i < array.length; i++) System.out.print(array[i] + " "); Example: PassArray.java
33 Passing and Returning Arrays We just learned how you could pass an array into a method. Now we re going to take this to the next step to learn how you can return an array from a method. A method can return a reference to an array. To do so, the return type of the method must be declared properly. For example, look at the following method definition: double is the return type public static double[] getarray() // <-- No parameters called double[] array = 1.2, 2.3, 4.5, 6.7, 8.9 return array; return sends back a double The getarray method is a public static method that returns an array of doubles. See example: ReturnArray.java
34 Assignment 4 Basic and Advanced BASIC is Robot related and includes a few comments and hints. It will create a Histogram with Things and a print out using asterisks. It does not interact wit a User. ADVANCED is Non-Robot related and includes hardly any hints. It will create a Guess a Number game and interact with a User.
35 Assignment 4 Basic (Demo) NOTE: You will need to pass an array as a parameter to solve Assignment 4 Basic, but you DO NOT need to return an array. After today s ICE you will have learned everything you need to successfully complete Assignment 4 Basic
36 Assignment 4 Advanced A look at the Advanced Guessing Game assignment NOTE: You do NOT need to pass an array as a parameter to solve Assignment 4 Advanced, and you DO NOT need to return an array, although you could do it that way if you wanted to. You WILL need to return a Boolean true or false however in some of your methods as well as return the number guessed to be successful with Assignment 4 Advanced.
37 ICE: Arrays as Parameters I m going to walk-through the building of the PrintArray method for ICE PART 1 ICE_18_PrintArray.java
38 import java.util.*; class ArrayHelper extends Object public void PrintArray( int[] arrayname) // Whatever array is passed in main will populate arrayname here // Use a for loop here that checks the count against the length of arrayname, then iterates if less than length // HINT: You might want to check out the SOLUTIONS to previous lecture ICE exercises // for (?? ;?? ;??) // print out the array public class ICE_18_PrintArray extends Object public static void main(string[] args) ArrayHelper arraytester = new ArrayHelper(); // We're creating the two arrays in two different ways: // the shortarray has 3 locations and the longarray has 5, but in the longarray // we can shorten the way we assign numbers using the single line "array literal" method int [] shortarray; // Instantiate the shortarray here with 3 slots holding the three numbers: 1, 3, 5 // Add number to slot 0 // Add number to slot 1 // Add number to slot 2 int [] longarray; // fill the longarray as an "array literal" with five numbers: 1, 2, 3, 5, 7 // print out both the short and long arrays System.out.println("\nThe Short Array: "); arraytester.printarray(shortarray); // Passing the shortarray to the PrintArray() method System.out.println("\nThe Long Array: "); arraytester.printarray(longarray); // Passing the longarray to the PrintArray() method
39 import java.util.*; class ArrayHelper extends Object public void PrintArray( int[] arrayname) // Whatever array is passed in main will populate arrayname here // Use a for loop here that checks the count against the length of arrayname, then iterates if less than length // HINT: You might want to check out the SOLUTIONS to previous lecture ICE exercises // for (?? ;?? ;??) // print out the array public class ICE_18_PrintArray extends Object public static void main(string[] args) ArrayHelper arraytester = new ArrayHelper(); // We're creating the two arrays in two different ways: // the shortarray has 3 locations and the longarray has 5, but in the longarray // we can shorten the way we assign numbers using the single line "array literal" method int [] shortarray = new int [3]; // Instantiate the shortarray here with 3 slots holding the three numbers: 1, 3, 5 shortarray[0] = 1; shortarray[1] = 3; shortarray[3] = 5; int [] longarray; // fill the longarray as an "array literal" with five numbers: 1, 2, 3, 5, 7 // print out both the short and long arrays System.out.println("\nThe Short Array: "); arraytester.printarray(shortarray); // Passing the shortarray to the PrintArray() method System.out.println("\nThe Long Array: "); arraytester.printarray(longarray); // Passing the longarray to the PrintArray() method
40 import java.util.*; class ArrayHelper extends Object public void PrintArray( int[] arrayname) // Whatever array is passed in main will populate arrayname here // Use a for loop here that checks the count against the length of arrayname, then iterates if less than length // HINT: You might want to check out the SOLUTIONS to previous lecture ICE exercises // for (?? ;?? ;??) // print out the array public class ICE_18_PrintArray extends Object public static void main(string[] args) ArrayHelper arraytester = new ArrayHelper(); // We're creating the two arrays in two different ways: // the shortarray has 3 locations and the longarray has 5, but in the longarray // we can shorten the way we assign numbers using the single line "array literal" method int [] shortarray = new int [3]; // Instantiate the shortarray here with 3 slots holding the three numbers: 1, 3, 5 shortarray[0] = 1; shortarray[1] = 3; shortarray[3] = 5; int [] longarray = 1, 2, 3, 5, 7; // print out both the short and long arrays System.out.println("\nThe Short Array: "); arraytester.printarray(shortarray); // Passing the shortarray to the PrintArray() method System.out.println("\nThe Long Array: "); arraytester.printarray(longarray); // Passing the longarray to the PrintArray() method
41 import java.util.*; class ArrayHelper extends Object public void PrintArray( int[] arrayname) // Whatever array is passed in main will populate arrayname here // Use a for loop here that checks the count against the length of arrayname, then iterates if less than length // HINT: You might want to check out the SOLUTIONS to previous lecture ICE exercises for( int count = 0; count < arrayname.length; count++) System.out.println( count + ") Number: " + arrayname[count]); public class ICE_18_PrintArray extends Object public static void main(string[] args) ArrayHelper arraytester = new ArrayHelper(); // We're creating the two arrays in two different ways: // the shortarray has 3 locations and the longarray has 5, but in the longarray // we can shorten the way we assign numbers using the single line "array literal" method int [] shortarray = new int [3]; // Instantiate the shortarray here with 3 slots holding the three numbers: 1, 3, 5 shortarray[0] = 1; shortarray[1] = 3; shortarray[3] = 5; int [] longarray = 1, 2, 3, 5, 7; // print out both the short and long arrays System.out.println("\nThe Short Array: "); arraytester.printarray(shortarray); // Passing the shortarray to the PrintArray() method System.out.println("\nThe Long Array: "); arraytester.printarray(longarray); // Passing the longarray to the PrintArray() method
42 import java.util.*; class ArrayHelper extends Object public void PrintArray( int[] arrayname) for( int count = 0; count < arrayname.length; count++) System.out.println( count + ") Number: " + arrayname[count]); count public class ICE_18_PrintArray extends Object public static void main(string[] args) ArrayHelper arraytester = new ArrayHelper(); arrayname // We're creating the two arrays in two different ways: // the shortarray has 3 locations and the longarray has 5, but in the longarray // we can shorten the way we assign numbers using the single line "array literal" method 1, 3, 5 int [] shortarray = new int [3]; // Instantiate the shortarray here with 3 slots holding the three numbers: 1, 3, 5 shortarray[0] = 1; shortarray[1] = 3; shortarray[3] = 5; int [] longarray = 1, 2, 3, 5, 7; // print out both the short and long arrays System.out.println("\nThe Short Array: "); arraytester.printarray(shortarray); shortarray System.out.println("\nThe Long Array: "); arraytester.printarray(longarray); longarray
43 In-Class Exercises: Arrays as Parameters
Lecture 8 " INPUT " Instructor: Craig Duckett
Lecture 8 " INPUT " Instructor: Craig Duckett Assignments Assignment 2 Due TONIGHT Lecture 8 Assignment 1 Revision due Lecture 10 Assignment 2 Revision Due Lecture 12 We'll Have a closer look at Assignment
More informationLecture 10. Instructor: Craig Duckett
Lecture 10 Instructor: Craig Duckett Announcements Assignment 1 Revision DUE TONIGHT in StudentTracker by midnight If you have not yet submitted an Assignment 1, this is your last chance to do so to earn
More informationLecture 11. Instructor: Craig Duckett. Instance Variables
Lecture 11 Instructor: Craig Duckett Instance Variables Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22 nd Assignment 2 (LECTURE 8) GRADED! Section 1: Wednesday,
More informationLecture 7. Instructor: Craig Duckett OUTPUT
Lecture 7 Instructor: Craig Duckett OUTPUT Lecture 7 Announcements ASSIGNMENT 2 is due LECTURE 8 NEXT LECTURE uploaded to StudentTracker by midnight Assignment 2!!! Assignment Dates (By Due Date) Assignment
More informationLecture 6. Instructor: Craig Duckett
Lecture 6 Instructor: Craig Duckett Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment 1 submissions. If you did not submit an Assignment
More informationObject Oriented Programming. Java-Lecture 6 - Arrays
Object Oriented Programming Java-Lecture 6 - Arrays Arrays Arrays are data structures consisting of related data items of the same type In Java arrays are objects -> they are considered reference types
More informationCombo Lecture Lecture 14/15. Instructor: Craig Duckett
Combo Lecture Lecture 14/15 Instructor: Craig Duckett Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22 nd Assignment 2 (LECTURE 8) GRADED! Section 1: Wednesday,
More informationArray Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)
Array Basics: Outline Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and Constants
More informationChapter 6 SINGLE-DIMENSIONAL ARRAYS
Chapter 6 SINGLE-DIMENSIONAL ARRAYS Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk What is an Array? A single array variable can reference
More informationClaremont McKenna College Computer Science
Claremont McKenna College Computer Science CS 51 Handout 4: Problem Set 4 February 10, 2011 This problem set is due 11:50pm on Wednesday, February 16. As usual, you may hand in yours until I make my solutions
More informationCOMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette
COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.
More informationLecture 14. 'for' loops and Arrays
Lecture 14 'for' loops and Arrays For Loops for (initiating statement; conditional statement; next statement) // usually incremental body statement(s); The for statement provides a compact way to iterate
More informationChapter 7: Arrays and the ArrayList Class
Chapter 7: Arrays and the ArrayList Class Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter Topics Chapter 7 discusses the following main topics: Introduction
More informationArrays and Lists CSC 121 Fall 2016 Howard Rosenthal
Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods
More informationLast Class. While loops Infinite loops Loop counters Iterations
Last Class While loops Infinite loops Loop counters Iterations public class January31{ public static void main(string[] args) { while (true) { forloops(); if (checkclassunderstands() ) { break; } teacharrays();
More informationLecture 13. Instructor: Craig Duckett. Boolean Expressions, Logical Operators, and Return Values
Lecture 13 Instructor: Craig Duckett Boolean Expressions, Logical Operators, and Return Values Assignment 3: The Maze DUE TONIGHT! Uploaded to StudentTracker by midnight If you are submitting as part of
More informationLast Class. More on loops break continue A bit on arrays
Last Class More on loops break continue A bit on arrays public class February2{ public static void main(string[] args) { String[] allsubjects = { ReviewArray, Example + arrays, obo errors, 2darrays };
More informationECE 122. Engineering Problem Solving with Java
ECE 122 Engineering Problem Solving with Java Lecture 10 For Loops and Arrays Outline Problem: How can I perform the same operations a fixed number of times? Considering for loops Performs same operations
More informationBIT 115: Introduction To Programming LECTURE 3. Instructor: Craig Duckett
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett cduckett@cascadia.edu Lecture 3 Announcements By now everyone should be up and running with Java, jgrasp, and the Becker Robots
More informationArrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal
Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Review what an array is Review how to declare arrays Review what reference variables are Review how to pass arrays to methods Review
More informationArrays and Lists CSC 121 Fall 2014 Howard Rosenthal
Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods
More informationCS111: PROGRAMMING LANGUAGE II
1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1: Introduction Lecture Contents 2 Course info Why programming?? Why Java?? Write once, run anywhere!! Java basics Input/output Variables
More informationLecture 17. For Array Class Shenanigans
Lecture 17 For Array Class Shenanigans For or While? class WhileDemo { public static void main(string[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; Note:
More informationArrays and Lists CSC 121 Fall 2015 Howard Rosenthal
Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods
More informationArrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19
Arrays Lecture 11 CGS 3416 Spring 2017 March 6, 2017 Lecture 11CGS 3416 Spring 2017 Arrays March 6, 2017 1 / 19 Arrays Definition: An array is an indexed collection of data elements of the same type. Indexed
More informationObjectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array
Arrays Chapter 7 Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array as an instance variable Use an array not filled
More informationLecture 15. Arrays (and For Loops)
Lecture 15 Arrays (and For Loops) For Loops for (initiating statement; conditional statement; next statement) // usually incremental { body statement(s); The for statement provides a compact way to iterate
More informationCOMP 110 Introduction to Programming. What did we discuss?
COMP 110 Introduction to Programming Fall 2015 Time: TR 9:30 10:45 Room: AR 121 (Hanes Art Center) Jay Aikat FB 314, aikat@cs.unc.edu Previous Class What did we discuss? 1 Today Announcements Assignment
More information! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like
Indefinite loops while loop! indefinite loop: A loop where it is not obvious in advance how many times it will execute.! We often use language like " "Keep looping as long as or while this condition is
More informationExample: Computing prime numbers
Example: Computing prime numbers -Write a program that lists all of the prime numbers from 1 to 10,000. Remember a prime number is a # that is divisible only by 1 and itself Suggestion: It probably will
More informationArrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals
Arrays Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals Can we solve this problem? Consider the following program
More informationArrays. Lecture 11 CGS 3416 Fall October 26, 2015
Arrays Lecture 11 CGS 3416 Fall 2015 October 26, 2015 Arrays Definition: An array is an indexed collection of data elements of the same type. Indexed means that the array elements are numbered (starting
More informationPassing Array to Methods
Passing Array to Methods Lecture 13 Based on Slides of Dr. Norazah Yusof 1 Passing Array Elements to a Method When a single element of an array is passed to a method it is handled like any other variable.
More informationFundamentals of Programming Data Types & Methods
Fundamentals of Programming Data Types & Methods By Budditha Hettige Overview Summary (Previous Lesson) Java Data types Default values Variables Input data from keyboard Display results Methods Operators
More informationCOE318 Lecture Notes Week 4 (Sept 26, 2011)
COE318 Software Systems Lecture Notes: Week 4 1 of 11 COE318 Lecture Notes Week 4 (Sept 26, 2011) Topics Announcements Data types (cont.) Pass by value Arrays The + operator Strings Stack and Heap details
More informationCOMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016
COMP-202: Foundations of Programming Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016 Review What is the difference between a while loop and an if statement? What is an off-by-one
More informationWentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays
WIT COMP1000 Arrays Arrays An array is a list of variables of the same type, that represents a set of related values For example, say you need to keep track of the cost of 1000 items You could declare
More informationCOMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015
COMP-202: Foundations of Programming Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015 Announcements Check the calendar on the course webpage regularly for updates on tutorials and office hours.
More informationMr. Monroe s Guide to Mastering Java Syntax
Mr. Monroe s Guide to Mastering Java Syntax Getting Started with Java 1. Download and install the official JDK (Java Development Kit). 2. Download an IDE (Integrated Development Environment), like BlueJ.
More informationCONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays
CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types COMP-202 Unit 6: Arrays Introduction (1) Suppose you want to write a program that asks the user to enter the numeric final grades of 350 COMP-202
More informationStepwise Refinement. Lecture 12 COP 3014 Spring February 2, 2017
Stepwise Refinement Lecture 12 COP 3014 Spring 2017 February 2, 2017 Top-Down Stepwise Refinement Top down stepwise refinement is a useful problem-solving technique that is good for coming up with an algorithm.
More informationInstructor: Eng.Omar Al-Nahal
Faculty of Engineering & Information Technology Software Engineering Department Computer Science [2] Lab 6: Introduction in arrays Declaring and Creating Arrays Multidimensional Arrays Instructor: Eng.Omar
More informationLesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL
Lesson 06 Arrays MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Array An array is a group of variables (called elements or components) containing
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationIntroduction to Java Programming
Boaz Kantor Introduction to Computer Science, Fall semester 2009-2010 IDC Herzliya Welcome, geeks! Introduction to Java Programming Plan for today: 1. Before we begin.. 2. What is Java? 3. How to program?
More informationCS111: PROGRAMMING LANGUAGE II
CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A
More informationLAB 12: ARRAYS (ONE DIMINSION)
Statement Purpose: The purpose of this Lab. is to practically familiarize student with the concept of array and related operations performed on array. Activity Outcomes: Student will understand the concept
More informationVARIABLES AND TYPES CITS1001
VARIABLES AND TYPES CITS1001 Scope of this lecture Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Primitive types Every piece of data
More informationLoops. CSE 114, Computer Science 1 Stony Brook University
Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?
More informationArray. Lecture 12. Based on Slides of Dr. Norazah Yusof
Array Lecture 12 Based on Slides of Dr. Norazah Yusof 1 Introducing Arrays Array is a data structure that represents a collection of the same types of data. In Java, array is an object that can store a
More informationLab Activity Plan. John Dalbey CPE /30/2013
John Dalbey CPE 13-5 9/3/213 Lab Activity Plan Purpose The purpose of this lab is to demonstrate the performance impacts of autoboxing in Java. The textbook describes how Java will automatically convert
More informationInstructor: Craig Duckett
Instructor: Craig Duckett cduckett@cascadia.edu Announcements Assignment 1 Due Lecture 5 by MIDNIGHT I will double dog dare try to have Assignment 1 graded and back to you by Wednesday, January 24 th hopefully
More informationAnnouncements. 1. Forms to return today after class:
Announcements Handouts (3) to pick up 1. Forms to return today after class: Pretest (take during class later) Laptop information form (fill out during class later) Academic honesty form (must sign) 2.
More informationProgramming with Java
Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules
More informationExample Program. public class ComputeArea {
COMMENTS While most people think of computer programs as a tool for telling computers what to do, programs are actually much more than that. Computer programs are written in human readable language for
More informationSimple Java Reference
Simple Java Reference This document provides a reference to all the Java syntax used in the Computational Methods course. 1 Compiling and running... 2 2 The main() method... 3 3 Primitive variable types...
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques (CIS120) Lecture 24 March 18, 2016 The Java ASM What is the value of ans at the end of this program? Counter[] a = { new Counter(), new Counter() ; Counter[] b = {
More informationArray Basics: Outline
Arrays Chapter 7 Array Basics: Outline Creating and Accessing Arrays Array Details The Instance Variable length More About Array Indices Partially-filled Arrays Working with Arrays Creating and Accessing
More informationDarrell Bethea May 25, 2011
Darrell Bethea May 25, 2011 Yesterdays slides updated Midterm on tomorrow in SN014 Closed books, no notes, no computer Program 3 due Tuesday 2 3 A whirlwind tour of almost everything we have covered so
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationarray Indexed same type
ARRAYS Spring 2019 ARRAY BASICS An array is an indexed collection of data elements of the same type Indexed means that the elements are numbered (starting at 0) The restriction of the same type is important,
More informationH212 Introduction to Software Systems Honors
Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing
More informationCISC-124. Casting. // this would fail because we can t assign a double value to an int // variable
CISC-124 20180122 Today we looked at casting, conditionals and loops. Casting Casting is a simple method for converting one type of number to another, when the original type cannot be simply assigned to
More informationCOMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015
COMP-202: Foundations of Programming Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015 Announcements Midterm Exams on 4 th of June (12:35 14:35) Room allocation will be announced soon
More informationConstants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous
CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we
More informationCHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS
CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS Computers process information and usually they need to process masses of information. In previous chapters we have studied programs that contain a few variables
More informationLast Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it
Last Class Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it public class February4{ public static void main(string[] args) { String[]
More informationArrays. Chapter 7. Walter Savitch Frank M. Carrano
Walter Savitch Frank M. Carrano Arrays Chapter 7 Array Basics: Outline Creating and Accessing Arrays Array Details The Instance Variable length More About Array Indices Partially-filled Arrays Working
More informationWentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics
WIT COMP1000 Java Basics Java Origins Java was developed by James Gosling at Sun Microsystems in the early 1990s It was derived largely from the C++ programming language with several enhancements Java
More informationProgramming Exercise 7: Static Methods
Programming Exercise 7: Static Methods Due date for section 001: Monday, February 29 by 10 am Due date for section 002: Wednesday, March 2 by 10 am Purpose: Introduction to writing methods and code re-use.
More informationCS 106 Introduction to Computer Science I
CS 106 Introduction to Computer Science I 05 / 31 / 2017 Instructor: Michael Eckmann Today s Topics Questions / Comments? recap and some more details about variables, and if / else statements do lab work
More informationReview Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:
More informationCISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.
CISC-124 20180115 20180116 20180118 We continued our introductory exploration of Java and object-oriented programming by looking at a program that uses two classes. We created a Java file Dog.java and
More information13 th Windsor Regional Secondary School Computer Programming Competition
SCHOOL OF COMPUTER SCIENCE 13 th Windsor Regional Secondary School Computer Programming Competition Hosted by The School of Computer Science, University of Windsor WORKSHOP I [ Overview of the Java/Eclipse
More informationLAB 13: ARRAYS (ONE DIMINSION)
Statement Purpose: The purpose of this Lab. is to practically familiarize student with the concept of array and related operations performed on array. Activity Outcomes: As a second Lab on Chapter 7, this
More informationArrays. Eng. Mohammed Abdualal
Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Created by Eng: Mohammed Alokshiya Modified by Eng: Mohammed Abdualal Lab 9 Arrays
More informationData dependent execution order data dependent control flow
Chapter 5 Data dependent execution order data dependent control flow The method of an object processes data using statements, e.g., for assignment of values to variables and for in- and output. The execution
More informationChapter 2. Elementary Programming
Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,
More informationAgenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java
CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda
More informationLesson 7 Part 2 Flags
Lesson 7 Part 2 Flags A Flag is a boolean variable that signals when some condition exists in a program. When a flag is set to true, it means some condition exists When a flag is set to false, it means
More information4. Java language basics: Function. Minhaeng Lee
4. Java language basics: Function Minhaeng Lee Review : loop Program print from 20 to 10 (reverse order) While/for Program print from 1, 3, 5, 7.. 21 (two interval) Make a condition that make true only
More informationCOMP 110 Programming Exercise: Simulation of the Game of Craps
COMP 110 Programming Exercise: Simulation of the Game of Craps Craps is a game of chance played by rolling two dice for a series of rolls and placing bets on the outcomes. The background on probability,
More informationThe action of the program depends on the input We can create this program using an if statement
The program asks the user to enter a number If the user enters a number greater than zero, the program displays a message: You entered a number greater than zero Otherwise, the program does nothing The
More informationComputer Science is...
Computer Science is... Machine Learning Machine learning is the study of computer algorithms that improve automatically through experience. Example: develop adaptive strategies for the control of epileptic
More informationGrouping Objects. Primitive Arrays and Iteration. Produced by: Dr. Siobhán Drohan. Department of Computing and Mathematics
Grouping Objects Primitive Arrays and Iteration Produced by: Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topic List Primitive arrays Why do we need them? What are they?
More informationCSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists
CSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists 1 A programming problem Consider the following task: Double values representing grades are read until the user enters a negative
More informationSt. Edmund Preparatory High School Brooklyn, NY
AP Computer Science Mr. A. Pinnavaia Summer Assignment St. Edmund Preparatory High School Name: I know it has been about 7 months since you last thought about programming. It s ok. I wouldn t want to think
More informationCS121/IS223. Object Reference Variables. Dr Olly Gotel
CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223
More informationLinked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }
Linked Lists Since a variable referencing an object just holds the address of the object in memory, we can link multiple objects together to form dynamic lists or other structures. In our case we will
More informationCOMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015
COMP-202: Foundations of Programming Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015 Announcements Assignment 2 posted and due on 30 th of May (23:30). Extra class tomorrow
More informationCOMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz
COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz The in-class quiz is intended to give you a taste of the midterm, give you some early feedback about
More informationChapter 6. Arrays. Java Actually: A Comprehensive Primer in Programming
Lecture slides for: Chapter 6 Arrays Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 28. ISBN: 978-1-84448-933-2 http://www.ii.uib.no/~khalid/jac/
More informationFirst Java Program - Output to the Screen
First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the
More informationCS313D: ADVANCED PROGRAMMING LANGUAGE
CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Department Lecture 3: C# language basics Lecture Contents 2 C# basics Conditions Loops Methods Arrays Dr. Amal Khalifa, Spr 2015 3 Conditions and
More informationFollowing is the general form of a typical decision making structure found in most of the programming languages:
Decision Making Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined
More informationComputer programming Code exercises [1D Array]
Computer programming-140111014 Code exercises [1D Array] Ex#1: //Program to read five numbers, from user. public class ArrayInput public static void main(string[] args) Scanner input = new Scanner(System.in);
More informationOver and Over Again GEEN163
Over and Over Again GEEN163 There is no harm in repeating a good thing. Plato Homework A programming assignment has been posted on Blackboard You have to convert three flowcharts into programs Upload the
More informationCMPT 126: Lecture 6 Arrays
CMPT 126: Lecture 6 Arrays Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University September 25, 2007 1 Array Elements An array is a construct used to group and organize data.
More information1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003
1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 7, 2003 Name: Email Address: TA: Section: You have 90 minutes to complete this exam. For coding questions, you do not need to
More informationDeclaring and ini,alizing 2D arrays
Declaring and ini,alizing 2D arrays 4 2D Arrays (Savitch, Chapter 7.5) TOPICS Multidimensional Arrays 2D Array Allocation 2D Array Initialization TicTacToe Game // se2ng up a 2D array final int M=3, N=4;
More information