Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Size: px
Start display at page:

Download "Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal"

Transcription

1 Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

2 Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods The enhanced for statement Understand basic sorting Understand linear and binary searching Understand multi-dimensional arrays 2

3 Fundamentals (1) An array is an object that is used to store a list of values. Each array is made out of a contiguous block of memory that is divided into a number of cells. Each cell holds a value, and all the values are of the same type. Sometimes the cells of an array are called slots. In the example array pictured at right, each cell holds an int. The name of this array is data. The cells are indexed 0 through 9. Each cell can be accessed by using its index. For example, data[0] is the cell which is indexed by zero (which contains the value 23). data[5] is the cell which is indexed by 5 (which contains the value 14). While we say that data is the name of the array, it is also the name of the reference variable which refers to the beginning of the array. In other words, it contains an address 3

4 Fundamentals (2) The cells in an array are numbered sequentially starting at zero. This is called zero based indexing. If there are N cells in an array, the indexes will be 0 through N-1 Sometimes the index is called a subscript. The expression data[5] is usually pronounced "data-sub-five The value stored in a cell of an array is sometimes called an element of the array. An array has a fixed number of cells, but the values placed in those cells (the elements) may change over time. Note: There are other array like structures that have a variable number of cells Every cell of an array holds a value of the same type. So, for example, you can have an array of type int, an array of type double, and so on. You can also have an array of objects, such as of type String 4

5 Reference Variables There are two types of variables in Java primitive and reference Primitive variables are byte, short, int, long, double, float, boolean and char Reference variables hold memory addresses Declaring an array in Java creates a reference variable boolean[] truthful Instantiating the array creates an actual reference to a block of memory boolean [] truthful = new boolean [5]; truthful is a reference variable that holds an address, not a boolean value truthful[0] would hold a boolean value at the first address in the array The above example creates 5 boolean cells truthful[0] truthful[4] 5

6 Declaring Arrays An array is a named sequence of contiguous memory locations capable of holding a collection of data of the same type In order to use arrays, you must learn the basic syntax You can declare an array reference variable in two ways: type[] name or type name[] The first format is more typically used Examples: int[] myarray - an array reference variable of type int called myarray double[] numbers an array reference variable of type double called numbers 6

7 Array Instan<a<on There are several ways to instantiate an array. All require using the new operator Declare and instantiate in two statements type[] name; double[] earnings; name = new type[size] ; earnings = new double[6]; Declare and instantiate in one statement type[] name = new type[size]; double[] earnings = new double[6]; Declare, instantiate and initialize in one statement type[] name = v 0, v 1, v 2 v n-1 ; double[] earnings = 25.2, 35.9, 72.6, 12.8, 176.4, 99.0; This example creates an array called earnings of length 6 7

8 Array Instan<a<on Basic Rules The values in an array must all be of the same data type The first index in the array is 0 An array is referenced from name[0] name[length-1] The elements of an array are always initialized to 0 by type this is different from primitive variables boolean arrays are initialized to false char initializes to the null character Any reference type initializes to null Until the array is instantiated as an object, or refers to an object, it has the value null (not null char) 8

9 Instant Check Write the statement(s) necessary to instantiate: - a boolean type honesty with size 7 - a short type days with size 365 9

10 Answers boolean [ ] honesty = new boolean[7]; short [] days = new short[365]; 10

11 Array Length The length of an array is how many cells it has. An array of length N has cells indexed 0..(N-1) The length is a variable associated with the object Example: int [] testarray = new int [10]; Then testarray.length equals 10 You get the length value with every array that is instantiated. It is an instance variable associated with the object If you change the reference to refer to a different array then the length will always be based on the referenced array object Note: There is also an Arrays class which has lots of methods for manipulating an array object. You can look at this but we won t be using these methods until we learn more about objects. 11

12 Bound Checking in Arrays Indexes must be of type int and greater than or equal to 0 and less than length. The index cannot be of type long, although byte and short are okay since they cast up Therefore the maximum length of an array is (the maximum value of an int) If an expression is always illegal it will be caught at compile time However, if an index is found to be illegal during execution an exception message will appear and the program is terminated 12

13 Bound Checking in Arrays - Examples Given: int[] data = new int[10]; Example Outcome data[ -1 ] always illegal index must be >= 0 data[ 10 ] data[ 1.5 ] data[ 0 ] data[ 9 ] data[ j ] illegal out of range (given the above declaration) always illegal must be an integer always OK OK (given the above declaration) can't tell (depends on the value of j) 13

14 Using an Expression as an Index class ArrayExample01 public static void main ( String[] args ) double[] val = new double[4]; val[0] = 0.12; val[1] = 1.43; val[2] = 2.98; int j = 3; System.out.println( "cell 4: " + val[ j ] ); System.out.println( "cell 3: " + val[ j-1 ] ); j = j-2; System.out.println( "cell 2: " + val[ j ] ); 14

15 Copying Values from Cell to Cell versus Crea<ng Two References to the Same Object public class ArrayExample02 public static void main ( String[] args ) int[] vala = 12, 23, 45, 56 ; int[] valb = new int[4]; //a new array valb[ 0 ] = vala[ 0 ] ; valb[ 1 ] = vala[ 1 ] ; valb[ 2 ] = vala[ 2 ] ; valb[ 3 ] = vala[ 3 ] ; int [ ] valc ; valc = valb; //This can be done since valc and valb are the same type of reference variables for (int i=0; i<vala.length; i++) System.out.println(valA[i]); for (int i=0; i<valb.length; i++) System.out.println(valB[i]); for (int i=0; i<valc.length; i++) System.out.println(valC[i]); System.out.println((valA==valB)); //this is false System.out.println((valA==valC)); //this is false System.out.println((valB==valC)); //this is true System.out.println((valA[0] == valb[0]) && (vala[0] == valc[0]) && (valb[0] == valc[0])); //this is true 15

16 Visualizing the Previous Program vala 12 valb 12 valc

17 Arrays and Methods(1) Passing an Array to a Method An array reference can be passed as a parameter to a method This allows all the current values of the array to be obtained for use by the method Example: Method Header: public static void swap(int[] xlist, int i, int j) In the body of main: int[] list = 1,3,5,7,9; swap(list, 2, 4) sends the reference to the array list to xlist, as well as the values 2 and 4 to i and j respectively Within the method xlist can reference 5 successive integers, the same as those in list. It is referring to the same place. Therefore it can change that array. Even though the variable x only exists in the swap method, the changes made to the array are persistent and visible in the calling method, since it is referencing to the same array. 17

18 Arrays and Methods(2) Passing an Array to a Method public class ArraySwapper public static void swap(int[] xlist, int i, int j) int temp = xlist[i]; xlist[i] = xlist[j]; xlist[j] = temp; public static void main(string[] args) int [] list = 1,3,5,7,9; System.out.print("Before: "); for (int i=0; i<list.length; i++) System.out.print(list[i] + " "); swap(list,2,4); System.out.print("\nAfter:"); for (int i=0; i<list.length; i++) System.out.print(list[i] + " "); 18

19 Arrays and Methods (3) Passing an Array from a Method The value of the method can be a reference to an array created in the method If the method returns a reference then it must return the reference to an array of like type. Why??? However, you don t need to create a new array, just declare the reference variable in the calling routine that accepts the reference. There is only one array, not two. 19

20 Arrays and Methods (4) Passing an Array from a Method public class ArrayPassing public static double[] returnarray( ) double[] x; //creating a reference variable x = new double[3]; // Create an array of 3 elements x[0] = 2.3; x[1] = 3.4; x[2] = 4.5; return( x ); // Return the **reference** (location) of the array public static void main (String[] args) double [ ] a; /here a.length does not exist a = returnarray(); for (int i=0; i < a.length; i++) // now a.length does exist System.out.println(a[i]); 20

21 Why Arrays? Using arrays allows us to reference large amounts of data by index. With a single name we can store and access a large number of elements, sorting, searching and organizing them For instance, instead of listing grade0, grade1, grade2, grade3, grade4,. You can just loop through an array called grade using grade[0], grade[1], grade[2], grade[3], grade[4],... With arrays and looping we have combined two powerful weapons of programming 21

22 Enhanced for Looping With Arrays (1) The enhanced for is a mechanism for easily passing through an entire array, or other array like structure public class Test public static void main(string ] args) int [] numbers = 10, 20, 30, 40, 50; for(int x : numbers ) // replaces for (int i=0; i <= numbers.length; i++) System.out.print( x ); // replaces System.out.print(number[i]); System.out.print(","); Prints out 10,20,30.40,50 22

23 Enhanced For Looping With Arrays (2) The format is: for (parameter : arrayname) statement 1; statement n; The parameter takes on the value in the array during each iteration and therefore must be of the same data type (primitive or reference) This means that the first time through the loop parameter = arrayname[0], second time through parameter = arrayname[1] etc. 23

24 Using Arrays - Popula<ng an Array public class ArrayDemo public static void main(string[] args) int[] anarray; // DECLARE a reference for an array of integers anarray = new int[10]; // CREATE an array of integers // assign a value to each array element for (int i = 0; i < anarray.length; i++) anarray[i] = i; // print a value from each array element for (int value : anarray) //using the enhanced for System.out.print(value + " "); System.out.println(); 24

25 Using Arrays - Popula<ng an Array with a RunTime Array Length import java.util.scanner; public class ArrayDemo2 public static void main(string[] args) int size; int[] anarray; // DECLARE a reference for an array of integers Scanner keyboard = new Scanner(System.in); System.out.print("Input size of the array: "); size= keyboard.nextint(); anarray = new int[size]; // CREATE an array of integers // assign a value to each array element for (int i = 0; i < anarray.length; i++) anarray[i] = i; // print a value from each array element for (int value : anarray) //using enhanced for System.out.print(value + " "); System.out.println(); Deciding the size of the array in real-time allows the program to be more efficient and flexible it is data driven rather than hard-coded There are other types of array like objects that allow for more dynamic lists also known as queues 25

26 Using Arrays - Finding The Average of the Numbers in an Array import java.util.scanner; public class ArrayAverage public static void main(string[] args) int size, sum; long average; int[] anarray; // DECLARE an array of integers Scanner keyboard = new Scanner(System.in); System.out.print("Input size of the array: "); size= keyboard.nextint(); anarray = new int[size]; // CREATE an array of integers dynamically //read in an array for (int i=0; i < anarray.length; i++) System.out.println("Enter integer value for anarray[" +i + ]"); anarray[i] = keyboard.nextint(); sum= 0; for (int value : anarray) //using enhanced for sum = sum + value; average = Math.round((double)(sum)/anArray.length); System.out.print("The average value of the array is: " +average + "\n"); 26

27 Using Arrays - Finding The Maximum Number in an Array import java.util.scanner; public class ArrayMax public static void main(string[] args) int size, maxvalue; Scanner keyboard = new Scanner(System.in); System.out.print("Input size of the array: "); size= keyboard.nextint(); int[] anarray = new int[size]; // DECLARE and CREATE an array of integers dynamically //read in an array for (int i=0; i < anarray.length; i++) System.out.println("Enter integer value for anarray[" +i + ]"); anarray[i] = keyboard.nextint(); max_value= anarray[0]; for (int i = 1; i < anarray.length; i++) if (anarray[i] > maxvalue) maxvalue = anarray[i]; System.out.print("The maximum value of the array is: " +maxvalue + \n ); Now write a program to find the minimum value in an array. Then write a program that sums the numbers in an array 27

28 SorJng Sorting is a method for reorganizing a list from top to bottom or bottom to top There are many different algorithms for sorting In order to search a list it is often easier to first sort it A random list can only be searched directly by going through the list one element at a time unless you sort it first Alphabetizing is also a type of sorting that we will learn after we study the String class We will look at the insertion method of sorting as shown in ArraySorter.java 28

29 Using Arrays Sor<ng How Does the Inser<on Method Work Assume the list is array[0] =15 array[1] = 14 array[2] = 13 array[3] = 12 after first round we have after second round after third round c d array[d] array[d+1] swap What we are doing is getting the largest number in the last cells in the first round, and the next largest in the next to last cell, etc. 29

30 Using Arrays - Sor<ng import java.util.scanner; public class ArraySorter public static void main(string []args) int size, swap; Scanner keyboard = new Scanner(System.in); System.out.println("Input number of integers to sort"); size = keyboard.nextint(); int array[] = new int[size]; System.out.println("Enter " + size + " integers"); for (int c = 0; c < size; c++) array[c] = keyboard.nextint(); for (int c = 0; c < ( size - 1 ); c++) for (int d = 0; d < size - c - 1; d++) if (array[d] > array[d+1]) /* For descending order use < */ swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; System.out.println("Sorted list of numbers"); for (int k = 0; k < size; k++) System.out.println(array[k]); System.out.println(); 30

31 Using Arrays - Search Linear Search Linear search is a method for searching one by one through a list until you find a match It is somewhat laborious For larger lists it becomes very inefficient Review ArrayLinearSearch.java for example Binary Search Requires that the list is ordered you can order the list first Simply keep dividing the list in half until you find the number Review ArrayBinarySearch.java 31

32 Efficiency of Binary Search n Average linear = n/2 Average binary = log 2 n -1 4 (2 2 ) (2 4 ) (2 10 ) (2 20 )

33 Two-Dimensional Arrays A two dimensional array is like a table or a matrix It has rows and columns In java, both the rows and columns are indexed starting at zero A two-dimensional array is actually an array of arrays As with one-dimensional arrays all values of the array must be of the same type The reference is always first by row and then column - array [row] [column] Array [0][0] Array [0][1] Array [0][2] Array [0][3] Array [1][0] Array [1][1] Array [1][2] Array [1][3] Array [2][0] Array [2][1] Array [2][2] Array [2][3] Array [3][0] Array [3][1] Array [3][2] Array [3][3] Array [4][0] Array [4][1] Array [4][2] Array [4][3] 33

34 Declaring Two-Dimensional Arrays (1) Both the two-step and one step methods are available Two-steps: int[][] table; // declares the variable table = new int[2][3]; //instantiates the array One step: int[][] table = new int[2][3] It can be processed in for loops by row or by column Either way it looks like this, with all values initialized to 0:

35 Declaring Two-Dimensional Arrays (2) You can also explicitly declare a matrix by rows: int[][] gradetable = 99, 42, 74, 83, 100, 90, 91, 72, 88, 95, 88, 61, 74, 89, 96, 61, 89, 82, 98, 93, 93, 73, 75, 78, 99, 50, 65, 92, 87, 94, 43, 98, 78, 56, 99 row Col

36 Simple Example of a Useful Table Student Week The table has 7 rows and 5 columns the headers are not part of this table 36

37 Finding the Number of Rows and Columns of Arrays With Unequal Length Ragged Arrays It is possible that the rows in an array can have different numbers of columns The length of a two-dimensional array is its number of rows arrayx.length The number of columns in a row is arrayx[row].length In matrix below: arrayx.length = 5 arrayx[0].length = 4; arrayx[1].length = 3; arrayx[2].length = 4; arrayx[3].length = 2; arrayx[4].length = 4; arrayx [0][0] arrayx [0][1] arrayx [0][2] arrayx [0][3] arrayx [1][0] arrayx [1][1] arrayx [1][2] arrayx [2][0] arrayx [2][1] arrayx [2][2] arrayx [2][3] arrayx [3][0] arrayx [3][1] arrayx [4][0] arrayx [4][1] arrayx [4][2] arrayx [4][3] 37

38 Declaring An Array With Unequal Column Entries int [][] arrayy = new int [4][]; arrayy[0] = new int[5]; arrayy[1] = new int[1]; arrayy[2] = new int[1]; arrayy[3] = new int[4]; Remember We are declaring an Array of Arrays 38

39 Declaring Two-Dimensional Arrays With Uneven Numbers of Columns during Popula<on You can also explicitly declare a matrix by rows: int[][] gradetable = 99, 42, 74, 90, 91, 72, 88, 95, 88, 61, 89, 82, 98, 93, 73, 75, 78, 99, 50, 65, 92, 87, 94, 43, 98 Using this method you can create rows with uneven numbers of entries (advanced subject) gradetable.length = 7 gradetable[0].length = 3 gradetable[1].length = 5 gradetable[2].length = 1 gradetable[3].length = 4 gradetable[4].length = 5 gradetable[5].length = 5 gradetable[6].length = 2 row Col

40 Prin<ng a Two-Dimensional Array class ArrayPrint2D public static void main( String[] args ) // declare and construct a 2D array int[][] myarray = 1, 9, 4, 6, 0, 2,4,5, 0, 1, 2, 3 ; // print out the array for ( int row=0; row < myarray.length; row++ ) System.out.print("Row " + row + ": "); for ( int col=0; col < myarray[row].length; col++ ) System.out.print( myarray[row][col] + " "); System.out.println(); 40

41 Programming Exercises Class (1) Exercise 1 Array Data Write two methods that read data from the console and store the data in an array: a. The method int readdata(int[] x) reads a list of at most 100 integers into the array x. A sentinel, -999, terminates the list. The method returns the size of the list. b. The method int[] readdata() reads and returns a list of integers. The list is preceded by the number of the items in the list. For example, the data indicates that there are six items in the list. The leading 6 is not included in the list. Test both these methods within a single program that includes a method: void printlist(int [] x, int n) that displays x[0] through x[n-1] 41

42 Programming Exercises Class (2) Exercise 14 Max Sort Implement a method void maxsort(int[] x, int size) // that sorts the partially filled array x. The method maxsort( ) first determines the largest value in x and swaps that value with x[size-1]; then maxsort( ) finds the next largest value and swaps that value with x[size-2], and so on. Include an auxiliary method int max(int[] x, int i) that returns the index of the largest element between x[0] and x[i] inclusive. Test your methods in a program. 42

43 Programming Exercises Class (3) Exercise PrintArrays Implement a method readarr that accepts a reference to a two dimensional integer array and reads in values for all the cells of the array Implement a method writearr that accepts a reference to a two dimensional array and prints out all the cells of the array row by row. Implement a main method that creates 2 twodimensional arrays, by reading in the number of rows and columns and creating the array. For each array call the two methods defined above. 43

44 Programming Exercises Lab (1) Exercise 9 Largest and Smallest Design a method that determines the largest and smallest values stored in an integer array x. Your method should return these values in an array of length 2. Use the following algorithms: Initialize variables currentbig and currentsmall to the larger and smaller values of x[0] and x[1]. Process the rest of the list two elements at a time (optional). Compare the larger of the two elements to currentbig and replace currentbig if necessary. Compare the smaller of the two elements to currentsmall and replace currentsmall if necessary. Test your method in a program and include a method that reads a list, terminated by -999, into an array. 44

45 Programming Exercises Lab (2) Exercise 15 Minimum Sort using Selection Sort Implement a method void minsort(int[ ] x, int size) // (size <= x.length) that sorts the partially filled array x. The method selectionsort( ) first determines the smallest value in x and swaps that value with x[0]; then selectionsort( ) finds the next smallest value and swaps that with x[1] and so on. Include an auxiliary method int min(int[ ] x, int i) that returns the index of the smallest element between x[i] and x[size-1], inclusive. Test your methods in a program. 45

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Arrays 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 information

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Arrays 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 information

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

Arrays 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 information

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

Array. 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 information

Last Class. While loops Infinite loops Loop counters Iterations

Last 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 information

Object Oriented Programming. Java-Lecture 6 - Arrays

Object 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 information

Selec%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 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 information

Selec%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 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 information

Arrays. Eng. Mohammed Abdualal

Arrays. 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 information

CONTENTS: 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 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 information

COMP 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 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 information

Last Class. More on loops break continue A bit on arrays

Last 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 information

Declaring Array Variable

Declaring Array Variable 5. Arrays Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar, Gyan Khand-3, Indirapuram, Ghaziabad Website: www.sisoft.in Email:info@sisoft.in Phone: +91-9999-283-283 Array Arrays are used typically

More information

Example: Computing prime numbers

Example: 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 information

Array. Array Declaration:

Array. Array Declaration: Array Arrays are continuous memory locations having fixed size. Where we require storing multiple data elements under single name, there we can use arrays. Arrays are homogenous in nature. It means and

More information

Arrays. 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 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 information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Methods CSC 121 Fall 2016 Howard Rosenthal

Methods CSC 121 Fall 2016 Howard Rosenthal Methods CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods

More information

Fundamentals of Programming Data Types & Methods

Fundamentals 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 information

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017 Your Name: Exam 2. CSC 121 MW Class Lecturer: Howard Rosenthal April 26, 2017 The following questions (or parts of questions) in numbers 1-7 are all worth 3 points each. 1. Answer the following as true

More information

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repe$$on CSC 121 Fall 2015 Howard Rosenthal Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

1 Short Answer (10 Points Each)

1 Short Answer (10 Points Each) Name: Write all of your responses on these exam pages. 1 Short Answer (10 Points Each) 1. What is the difference between a compiler and an interpreter? Also, discuss how Java accomplishes this task. 2.

More information

Computer Science is...

Computer 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 information

Last 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 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 information

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved 1 Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a matrix or a table. For example, the following table that describes

More information

Repetition CSC 121 Fall 2014 Howard Rosenthal

Repetition CSC 121 Fall 2014 Howard Rosenthal Repetition CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Repe$$on CSC 121 Spring 2017 Howard Rosenthal Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when

More information

St. Edmund Preparatory High School Brooklyn, NY

St. 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 information

Methods CSC 121 Spring 2017 Howard Rosenthal

Methods CSC 121 Spring 2017 Howard Rosenthal Methods CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods

More information

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

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method. Name: Write all of your responses on these exam pages. 1 Short Answer (5 Points Each) 1. What is the difference between a compiler and an interpreter? Also, discuss Java s method. 2. Java is a platform-independent

More information

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22 nd Assignment 2 (LECTURE 8) GRADED! Section

More information

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Chapter 7 Multidimensional Arrays rights reserved. 1 Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent

More information

COMP-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 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 information

Chapter 8 Multi-Dimensional Arrays

Chapter 8 Multi-Dimensional Arrays Chapter 8 Multi-Dimensional Arrays 1 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections of elements. myarray: 6 4 1 9 7 3 2 8 Now think

More information

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier Lesson 4 Arrays and Lists Review CSC 123 Fall 2018 Answer Sheet Short Answers 1. In an array declaration, this indicates the number of elements that the array will have. b a. subscript b. size declarator

More information

CS111: PROGRAMMING LANGUAGE II

CS111: 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 information

New Concepts. Lab 7 Using Arrays, an Introduction

New Concepts. Lab 7 Using Arrays, an Introduction Lab 7 Using Arrays, an Introduction New Concepts Grading: This lab requires the use of the grading sheet for responses that must be checked by your instructor (marked as Question) AND the submission of

More information

Give one example where you might wish to use a three dimensional array

Give one example where you might wish to use a three dimensional array CS 110: INTRODUCTION TO COMPUTER SCIENCE SAMPLE TEST 3 TIME ALLOWED: 60 MINUTES Student s Name: MAXIMUM MARK 100 NOTE: Unless otherwise stated, the questions are with reference to the Java Programming

More information

Arrays. Chapter 6. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

Arrays. Chapter 6. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays Chapter 6 Arrays Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays Chapter 6 Java: an Introduction to Computer Science & Programming

More information

Chapter 6. Arrays. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

Chapter 6. Arrays. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays Chapter 6 Arrays Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays Chapter 6 Java: an Introduction to Computer Science & Programming

More information

Arrays. CS Feb 2008

Arrays. CS Feb 2008 Arrays CS 180 15 Feb 2008 Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb Snakes & Ladders Game Review of Q5,Q8,Q11 & Programming Questions Arrays:

More information

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA. Arrays Defining arrays, declaration and initialization of arrays Introduction Many applications require the processing of multiple data items that have common characteristics (e.g., a set of numerical

More information

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) In this lecture, you will: Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of

More information

Fall 2017 CISC124 9/16/2017

Fall 2017 CISC124 9/16/2017 CISC124 Labs start this week in JEFF 155: Meet your TA. Check out the course web site, if you have not already done so. Watch lecture videos if you need to review anything we have already done. Problems

More information

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff(); COSC 117 Exam 3 Key Fall 2012 Part 1: Definitions & Short Answer (3 Points Each) 1. A method in a class that has no return type and the same name as the class is called what? How is this type of method

More information

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 25, 2016

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 25, 2016 Your Name: Exam 2. CSC 121 MW Class Lecturer: Howard Rosenthal April 25, 2016 The following questions (or parts of questions) in numbers 1-7 are all worth 3 points each. 1. Answer the following as true

More information

Spring 2010 Java Programming

Spring 2010 Java Programming Java Programming: Guided Learning with Early Objects Chapter 7 - Objectives Learn about arrays Explore how to declare and manipulate data in arrays Learn about the instance variable length Understand the

More information

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

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1 Name SOLUTION Page Points Score 2 15 3 8 4 18 5 10 6 7 7 7 8 14 9 11 10 10 Total 100 1 P age 1. Program Traces (41 points, 50 minutes)

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using

More information

Lesson 7 Part 2 Flags

Lesson 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 information

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007 Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2007 Final Examination Question Max

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #07: September 21, 2015 1/30 We explained last time that an array is an ordered list of values. Each value is stored at a specific, numbered position in

More information

1 Short Answer (10 Points Each)

1 Short Answer (10 Points Each) 1 Short Answer (10 Points Each) 1. For the following one-dimensional array, show the final array state after each pass of the three sorting algorithms. That is, after each iteration of the outside loop

More information

Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal

Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Understand variables Understand how to declare and use variables in Java Programs Learn how to formulate assignment statements

More information

Lecture #8-10 Arrays

Lecture #8-10 Arrays Lecture #8-10 Arrays 1. Array data structure designed to store a fixed-size sequential collection of elements of the same type collection of variables of the same type 2. Array Declarations Creates a Storage

More information

13 th Windsor Regional Secondary School Computer Programming Competition

13 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 information

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops) Arrays Chapter 7 (Done right after 4 arrays and loops go together, especially for loops) Object Quick Primer A large subset of Java s features are for OOP Object- Oriented Programming We ll get to that

More information

Lesson 35..Two-Dimensional Arrays

Lesson 35..Two-Dimensional Arrays Lesson 35..Two-Dimensional Arrays 35-1 Consider the following array (3 rows, 2 columns) of numbers: 22 23 24 25 26 27 Let s declare our array as follows: int a[ ] [ ] = new int [3] [2]; Subscript convention:

More information

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CS 152: Data Structures with Java Hello World with the IntelliJ IDE CS 152: Data Structures with Java Hello World with the IntelliJ IDE Instructor: Joel Castellanos e-mail: joel.unm.edu Web: http://cs.unm.edu/~joel/ Office: Electrical and Computer Engineering building

More information

System.out.print("Enter sales for salesperson " + i + ": "); sales[i] = scan.nextint();

System.out.print(Enter sales for salesperson  + i + : ); sales[i] = scan.nextint(); Tracking Sales File Sales.java contains a Java program that prompts for and reads in the sales for each of 5 salespeople in a company. It then prints out the id and amount of sales for each salesperson

More information

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers; Arrays What are they? An array is a data structure that holds a number of related variables. Thus, an array has a size which is the number of variables it can store. All of these variables must be of the

More information

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages Topic 1: Basic Java Plan: this topic through next Friday (5 lectures) Required reading on web site I will not spell out all the details in lecture! BlueJ Demo BlueJ = Java IDE (Interactive Development

More information

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Arrays A data structure for a collection of data that is all of the same data type. The data type can be

More information

Programming with Java

Programming 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 information

STUDENT LESSON A12 Iterations

STUDENT LESSON A12 Iterations STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of

More information

Arrays. Chapter 6. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

Arrays. Chapter 6. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays Chapter 6 Arrays Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays Chapter 6 Java: an Introduction to Computer Science & Programming

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

Wentworth 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 information

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2 CONTENTS: Compilation Data and Expressions COMP 202 More on Chapter 2 Programming Language Levels There are many programming language levels: machine language assembly language high-level language Java,

More information

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15 Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL

More information

DCS/100 Procedural Programming

DCS/100 Procedural Programming DCS/100 Procedural Programming Week 9: References, the Heap and the Stack Last Week: Learning Outcomes From last week you should be able to: write programs that are split into methods write programs that

More information

Variables and Assignments CSC 121 Fall 2015 Howard Rosenthal

Variables and Assignments CSC 121 Fall 2015 Howard Rosenthal Variables and Assignments CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand variables Understand how to declare and use variables in Java Programs Learn how to formulate assignment statements

More information

Chapter 7: Arrays and the ArrayList Class

Chapter 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 information

Software Practice 1 Basic Grammar

Software Practice 1 Basic Grammar Software Practice 1 Basic Grammar Basic Syntax Data Type Loop Control Making Decision Prof. Joonwon Lee T.A. Jaehyun Song Jongseok Kim (42) T.A. Sujin Oh Junseong Lee (43) 1 2 Java Program //package details

More information

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Understand Object Oriented Programming The Syntax of Class Definitions Constructors this Object Oriented "Hello

More information

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision Prof. Hwansoo Han T.A. Minseop Jeong T.A. Wonseok Choi 1 Java Program //package details public class ClassName {

More information

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency Introduction Fundamentals Declaring arrays Indexing arrays Initializing arrays Arrays and functions Multidimensional arrays Sorting and algorithm efficiency An array is a sequence of values of the same

More information

All answers will be posted on web site, and most will be reviewed in class.

All answers will be posted on web site, and most will be reviewed in class. Lesson 4 Arrays and Lists Review CSC 123 Fall 2018 Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments when required.

More information

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Chapter 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 information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Instructor: Eng.Omar Al-Nahal

Instructor: 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 information

Manipulating One-dimensional Arrays

Manipulating One-dimensional Arrays Manipulating One-dimensional Arrays Mitsu Ogihara Department of Computer Science University of Miami 1 / 30 Table of Contents 1 For each 2 Exchanging Values 3 Reversing 2 / 30 For-each iteration For enumerating

More information

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class CS0007: Introduction to Computer Programming Review General Form of a switch statement: switch (SwitchExpression) { case CaseExpression1:

More information

Getting started with Java

Getting started with Java Getting started with Java by Vlad Costel Ungureanu for Learn Stuff Programming Languages A programming language is a formal constructed language designed to communicate instructions to a machine, particularly

More information

Arrays and the TOPICS CHAPTER 8. CONCEPT: An array can hold multiple values of the same data type simultaneously.

Arrays and the TOPICS CHAPTER 8. CONCEPT: An array can hold multiple values of the same data type simultaneously. CHAPTER 8 Arrays and the ArrayList Class TOPICS 8.1 Introduction to Arrays 8.2 Processing Array Elements 8.3 Passing Arrays As Arguments to Methods 8.4 Some Useful Array Algorithms and Operations 8.5 Returning

More information

Lesson 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 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 information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

DATA STRUCTURES CHAPTER 1

DATA STRUCTURES CHAPTER 1 DATA STRUCTURES CHAPTER 1 FOUNDATIONAL OF DATA STRUCTURES This unit introduces some basic concepts that the student needs to be familiar with before attempting to develop any software. It describes data

More information

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice CONTENTS: Arrays Strings COMP-202 Unit 5: Loops in Practice Computing the mean of several numbers Suppose we want to write a program which asks the user to enter several numbers and then computes the average

More information

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to define and invoke void and return java methods JAVA

More information

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd El-Shorouk Academy Acad. Year : 2013 / 2014 High Institute of Computer Science & Information Technology Term : 1 st Year : 2 nd Computer Science Department Object Oriented Programming Section (1) Arrays

More information

Getting started with Java

Getting started with Java Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

More information

More Arrays. Last updated 2/6/19

More Arrays. Last updated 2/6/19 More Last updated 2/6/19 2 Dimensional Consider a table 1 2 3 4 5 6 5 4 3 2 12 11 13 14 15 19 17 16 3 1 4 rows x 5 columns 2 tj 2 Dimensional Consider a table 1 2 3 4 5 6 5 4 3 2 12 11 13 14 15 19 17 16

More information

Computer programming Code exercises [1D Array]

Computer 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 information

Array. Prepared By - Rifat Shahriyar

Array. Prepared By - Rifat Shahriyar Java More Details Array 2 Arrays A group of variables containing values that all have the same type Arrays are fixed length entities In Java, arrays are objects, so they are considered reference types

More information

Chapter 8 Multidimensional Arrays

Chapter 8 Multidimensional Arrays Chapter 8 Multidimensional Arrays 8.1 Introduction Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a matrix or a

More information

Computer Programming: C++

Computer Programming: C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming: C++ Experiment #7 Arrays Part II Passing Array to a Function

More information

CEN 414 Java Programming

CEN 414 Java Programming CEN 414 Java Programming Instructor: H. Esin ÜNAL SPRING 2017 Slides are modified from original slides of Y. Daniel Liang WEEK 2 ELEMENTARY PROGRAMMING 2 Computing the Area of a Circle public class ComputeArea

More information

4. Java language basics: Function. Minhaeng Lee

4. 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 information

Mr. Monroe s Guide to Mastering Java Syntax

Mr. 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 information