Introduction to Programming

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Slide 1 Insert your own content. Slide 2 Insert your own content.
1 Arrays, Strings and Collections [1] Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering.
Introduction to Java 2 Programming Lecture 5 Array and Collections.
0 - 0.
Introduction to arrays Array One dimenstional array.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Introduction to Programming Java Lab 1: My First Program 11 January JavaLab1.ppt Ping Brennan
Introduction to Programming Overview of Week 2 25 January Ping Brennan
SOFTWARE AND PROGRAMMING 1 Lecture 3: Ticketing machine: Constructor, method, menu Instructor: Prof. Boris Mirkin web-site
Introduction to Programming Java Lab 7: Loops 22 February JavaLab7 lecture slides.ppt Ping Brennan
22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming Java Lab 4: Formatted Output and Strings 1 February JavaLab4 lecture slides.ppt Ping Brennan
8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan
25 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Introduction to Programming Java Lab 3: Variables and Number types 25 January JavaLab3.ppt Ping Brennan
Introduction to Programming
Chapter 7: Arrays In this chapter, you will learn about
Parallel List Ranking Advanced Algorithms & Data Structures Lecture Theme 17 Prof. Dr. Th. Ottmann Summer Semester 2006.
Two Dimensional Arrays and ArrayList
ArrayList Difference of Array and ArrayList [Sample code] TestArrayList.java.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Chapter 8: Arrays.
1 Chapter Eleven Arrays. 2 A Motivating Example main( ) { int n0, n1, n2, n3, n4; scanf(“%d”, &n0); scanf(“%d”, &n1); scanf(“%d”, &n2); scanf(“%d”, &n3);
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Computer Programming Lab(7).
Building Java Programs Chapter 13
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
CS110 Programming Language I
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Week 8 Arrays Part 2 String & Pointer
1 Various Methods of Populating Arrays Randomly generated integers.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Recursion CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Iteration.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Building java programs, chapter 3 Parameters, Methods and Objects.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Introduction to Programming Python Lab 8: Loops 26 February PythonLab8 lecture slides.ppt Ping Brennan
Java Programming Language Lecture27- An Introduction.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Introduction to Programming
Suggested self-checks: Section 7.11 #1-11
Introduction to Programming
Presentation transcript:

Introduction to Programming Java Lab 9: Arrays JavaLab9 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk) 8 March 2013

Declaration of an Array Declare an array called data which contains 10 elements, each element is of type double. double[] data = new double[10]; Alternatively declare an array called moreData and initialise using a single statement. double[] moreData = { 6, 7, 2, 1, 0, -3 }; Name of array variable Element type Type of array variable Length List of initial values indices moredata [0] [1] [2] [3] [4] [5] 6 7 2 1 -3

Initialisation of an Array The elements of an array are accessed using an index. The first element in the array has an index of 0. The elements of the array data can be initialised as shown: data[0] = 5; data[1] = 2; The fourth entry of the array data is accessed below: System.out.println("The 4th element of the array data has the value " + data[3]); Use brackets to access an element The index must be >= 0 and < the length of the array

Java Project Project Name: JavaLab9 ReverseArray ArrayMethods

Class ReverseArray Prints out an array of integers in reverse order. Objectives Understand how to pass and return an array as a parameter variable for a method. Use an array to solve problems in a program. Method public static int[] reverseArray(int[] data) Method reverseArray returns the reverse of the array data.

Class ReverseArray (2) Algorithm for the method reverseData (in pseudo code) 1. Create an array variable, reversedData , using new int[data.length]. 2. Use a for loop to copy the values from data into reversedData in reverse order. 3. Return reversedData. Hint: use data.length in the test condition to terminate the for loop such that the loop iterates from 0 up to (data.length-1).

Class ReverseArray (3) Testing the method Make the following statement as the first line of your program import java.util.Arrays; Use the method Arrays.toString to print out an array of integers. For example, int[] data = { 1, 2, 3 } ; System.out.println(Arrays.toString(data));

Class ReverseArray (4) Computation Output Input Parameter variable, data  { 1, 2, 7 } Example (in method main): int[] data = { 1, 2, 7 }; int[] dataR = reverseArray(data); System.out.println("Reversed data: " + Arrays.toString(dataR)); Computation Inside the method reverseArray: for loop to reverse the data; return the reversedData array. Output [ 7, 2, 1 ]

Anatomy of Class ReverseArray public class ReverseArray { public static void main(String[] args) int[] data = { 1, 2, 7 } ; int[] dataR = reverseArray(data); System.out.println("Original array: " + Arrays.toString(data)); /* To do: Write a similar statement to print the reversed data from the array dataR */ } /* To do: insert code for the method reverseArray here (shown on next slide) */ } // end of class ReverseArray

Anatomy of method reverseArray public static int[] reverseArray(int[] data) { /* 1. Create an array variable, reversedData, using new int[data.length]. */ /* 2. Use a for loop to copy the values from data into reversedData in reverse order. Hint: (i) use data.length in the test condition to terminate the for loop such that the loop iterates from 0 up to (data.length-1) using the index i. (ii) use reversedData[(data.length-1) – i] = data[i]; to copy the data inside the for loop. */ /* 3. return reversedData */ } // end of method reverseArray

Class ArrayMethods Objective Methods Understand how to use more than one method in a program. Methods public static void printArray(int[] data); public static int productElements(int[] data); public static int numberNegativeElements(int[] data); Note: the first method is the usual one, main. See JFE, R6.7

Method printArray Objective Algorithm (in pseudo code) Print all the elements in an array of type int[] in a single row, separated by spaces. Algorithm (in pseudo code) for loop to print each array element, separated by a space except for the last element. Hint: use an if statement to check for (i < data.length-1) in order to print out a space after each array element. Note that i is the loop index and data.length is the length of the array data.

Method productElements Objective Return the product of all the elements in an array of type int[]. Algorithm (in pseudo code) 1. Define an integer variable, product, and set it to 1. 2. Use a for loop to update the product by multiplying it with the current array element. 3. Return product.

Method numberNegativeElements Objective Return the number of elements in an array of type int[] that are strictly less than 0. Algorithm (in pseudo code) 1. Define an integer variable, count, and set it to 0. 2. Use a for loop to increment count if a current array element is strictly less than 0. Hint: use an if statement to perform the test condition, if (data[i] < 0) , where i is the array index and data is name of the array variable. 3. Return count.

Testing Class ArrayMethods Input Parameter variable, data  { 1, 2, 3, -4, -7} Example (in main): int[] data = { 1, 2, 3, -4, -7 }; printArray(data); System.out.println("Product: " + productElements(data)); System.out.println("Number of negative elements: " + numberNegativeElements(data)); Computation printArray: for loop and if statements to print all elements productElements: for loop to calculate the product of all elements; return the product numberNegativeElements: for loop to count the number of elements strictly less than 0; return the number Output 1 2 3 -4 -7 Product: 168 Number of negative elements: 2

Anatomy of Class ArrayMethods /* The program applies various methods to an array of integers to produce certain results. Author: (your name) Date: 8 March 2013 */ public class ArrayMethods { /* 1. insert the code for the method main */ /* 2. insert the code for the method printArray */ /* 3. insert the code for the method productElements */ /* 4. insert the code for the method numberNegativeElements */ } // end of class ArrayMethods

Anatomy of method main public static void main(String[] args) { int[] data = { 1, 2, 3, -4, -7 }; printArray(data); System.out.println("Product elements: " + productElements(data)); System.out.print("Number of elements strictly less than 0: "); System.out.println(numberNegativeElements(data)); } /* To do: write the code for the three methods, printArray, productElements and numberNegativeElements, after the method main. */