Objects First with Java CITS1001

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Grouping objects Iterators. Iterator and iterator() Collections have an iterator() method. This returns an Iterator object. Iterator has three methods:
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Grouping Objects 3 Iterators, collections and the while loop.
Introduction to Programming with Java, for Beginners Arrays of Objects.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
EFFICIENCY & SORTING CITS1001. Listen to the sound of sorting Various algorithms Quicksort
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Object Oriented Design: Identifying Objects
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
Introduction to programming in java Lecture 23 Two dimensional (2D) Arrays – Part 3.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
ARRAYS II CITS1001. Scope of this lecture Arrays of objects Multi-dimensional arrays The Game of Life (next lecture) 2.
Chapter VII: Arrays.
Array in C# Array in C# RIHS Arshad Khan
Lesson 8: Introduction To Arrays
Objects First with Java CITS1001 week 4
Chapter 6: Using Arrays.
Lecture 5 of Computer Science II
Passing Objects to Methods
Lecture 8: 2D Arrays and Nested Loops
Two-Dimensional Arrays
Objects First with Java CITS1001
Arrays.
Chapter 8 – Arrays and Array Lists
Topic Dimensional Arrays
Two Dimensional Arrays
Two Dimensional Array Mr. Jacobs.
Two-Dimension Arrays Computer Programming 2.
Today’s Material Arrays Definition Declaration Initialization
Objects First with Java CITS1001
Two-Dimensional Arrays
Arrays An Array is an ordered collection of variables
Arrays in Java What, why and how Copyright Curt Hill.
Defining methods and more arrays
Arrays .
Multidimensional Arrays
Chapter 7 Part 2 Edited by JJ Shepherd
int [] scores = new int [10];
Lecture 4 2d Arrays CSE /26/2018.
Collections and iterators
Multi-Dimensional Arrays
INC 161 , CPE 100 Computer Programming
EECE.2160 ECE Application Programming
CIS 110: Introduction to Computer Programming
Review for Midterm 3.
Collections and iterators
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Presentation transcript:

Objects First with Java CITS1001 Arrays II CITS1001 © David J. Barnes and Michael Kölling

Scope of this lecture Arrays of objects Multi-dimensional arrays The Game of Life (next lecture)

Objects First with Java Arrays of objects The arrays we have seen so far had elements of a primitive type int[], double[], boolean[], etc. But an array can also hold objects e.g. Term[] Arrays can also be two-dimensional e.g. int[][] Arrays can also be three-dimensional, or more But we won’t get to that in CITS1001 © David J. Barnes and Michael Kölling

Objects First with Java Arrays of objects When using an array of primitive type, there are two steps involved Declare the variable to refer to the array Create the space for the array elements using new When using an array of object type, there are three steps involved Populate the array with objects by repeatedly using new, often in a loop © David J. Barnes and Michael Kölling

A Student class public class Student { private String studentNumber; private int mark; public Student(String studentNumber, int mark) { this.studentNumber = studentNumber; this.mark = mark; } public String getStudentNumber() { return studentNumber; public int getMark() { return mark; A skeleton version of a possible Student class in a student records system

Creating a unit list Declare Create Populate Student[] unitList; unitList = new Student[numStudents]; unitList[0] = new Student(“042371X”,64); unitList[1] = new Student(“0499731”,72); unitList[2] = new Student(“0400127”,55); … unitList[numStudents-1] = new Student(“0401332”,85); Declare Create Populate

The three steps unit null unit null null null null null unit[0]

Using arrays of objects Using an array of objects is the same as using any array You just need to remember that the elements are objects! private Student top(Student[] unitlist) { Student top = unitlist[0]; for (Student si : unitlist) if (si.getMark() > top.getMark()) top = si; return top; }

Compare with max private int max(int[] a) { int max = a[0]; for (int i : a) if (i > max) max = i; return max; }

Student top(Student[] unitList) Method signatures int max(int[] a) Student top(Student[] unitList)

Initialisation int max = a[0]; Declare a variable to hold the “best so far”, and initialise it to the first element in the array Student top = unitList[0];

Processing for (int i : a) if (i > max) max = i; Check each element in turn, compare it with the best so far, and update the best if necessary for (Student si : unitList) if (si.getMark() > top.getMark()) top = si;

Return return max; Finally return the extreme element – the highest int or the Student with the best mark return top;

What if we want to find the highest mark? We could copy-and-paste top, and edit the details to create a new method private int highestMark(Student[] unitlist) { int max = unitlist[0].getMark(); for (Student si : unitlist) if (si.getMark() > max) max = si.getMark(); return max; } No!!

Much better to use the existing method! Do not write another looping method! Use the already-written functionality private int highestMark(Student[] unitlist) { return top(unitlist).getMark(); }

Objects First with Java 2D arrays We sometimes need arrays with more than one dimension int[][] a = new int[4][3]; a[0][0] a[0][1] a[0][2] This creates an array with four “rows” and three “columns” The “row” index ranges from 0–3 and the “column” index from 0–2 a[1][0] a[1][1] a[1][2] a[2][0] a[2][1] a[2][2] a[3][0] a[3][1] a[3][2] © David J. Barnes and Michael Kölling

2D arrays contd. This is really an array where each element is itself an array a is a valid variable, representing the whole thing a[0] is a valid variable, representing the first “row” a[0][2] is a valid variable, representing the last element in the first “row”

2D arrays contd. a a[0] a[1] a[2] a[3] a[0][0] a[0][1] a[0][2] a[1][0] a[0][1] a[0][2] a[0] a[1][0] a[1][1] a[1][2] a[1] a a[2][0] a[2][1] a[2][2] a[2] a[3][0] a[3][1] a[3][2] a[3]

2D arrays contd. Given that an array is an object, this is really just a special case of an array of objects The single statement int[][] a = new int[4][3]; declares the array variable a, creates the array, and populates it!

2D arrays examples A 2D array is normally processed with two nested for-loops private int sum(int[][] a) { int sum = 0; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) sum = sum + a[i][j]; return sum; }

2D arrays examples Suppose we want to return the index of the first row containing any element that is true private int firsttrue(boolean[][] a) { for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) if (a[i][j]) return i; return -1; }

2D arrays example We will illustrate the use of 2D arrays further with a case study of The Game of Life http://en.wikipedia.org/wiki/Conway’s_Game_of_Life In the next lecture… And there’s plenty of practice in the lab too