ECE 122. Engineering Problem Solving with Java

Similar documents
Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

Review: Array Initializer Lists

7.3 Arrays of Strings (Objects) 7.3 Arrays of Strings (Objects) 7.3 Tunes.java. 7.3 Arrays of Objects 9/11/13. ! A UML diagram for the Tunes program

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

H212 Introduction to Software Systems Honors

Chapter 6: Arrays. Presentation slides for. Java Software Solutions. for AP* Computer Science 3rd Edition

11/19/2014. Arrays. Chapter 6: Arrays. Arrays. Arrays. Java Software Solutions for AP* Computer Science A 2nd Edition

New Concepts. Lab 7 Using Arrays, an Introduction

&KDSWHU$UUD\VDQG9HFWRUV

Arrays Chapter 7. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

2/2/18. Assignment 2 is due Wednesday 14 February at 11:59pm. Reading for Monday s lecture is the remainder of Chapter 7 and all of Chapter 8

Introduction to Programming Using Java (98-388)

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

array Indexed same type

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Lecture 7 Objects and Classes

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Class object initialization block destructor Class object

ECE 122. Engineering Problem Solving with Java

Chapter 4 Defining Classes I

Chapter 7: Arrays CS 121. April 9, Department of Computer Science College of Engineering Boise State University. Chapter 7: Arrays CS / 41

Practice Questions for Chapter 9

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Chapter 7. Arrays are objects that help us organize large amounts of information

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

JAVA GUI PROGRAMMING REVISION TOUR III

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

CS360 Lecture 5 Object-Oriented Concepts

TOPICS TO COVER:-- Array declaration and use.

Assignment 1 is due tonight at 11:59pm. Assignment 2 is due next Thursday at 11:59pm

Frequently Asked Questions

Lecture 5: Methods CS2301

CS111: PROGRAMMING LANGUAGE II

CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Example: Computing prime numbers

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Syntax of Variable Declarations. Variables Usages as Expressions. Self-assignment Statements. Assignment. Scoping and Naming Rules

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Types, Values and Variables (Chapter 4, JLS)

DATA STRUCTURES CHAPTER 1

CS1150 Principles of Computer Science Methods

Methods Summer 2010 Margaret Reid-Miller

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Programming - 2. Common Errors

Defining Classes and Methods

Defining Classes and Methods

CSE 11 Midterm Fall 2009

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Pace University. Fundamental Concepts of CS121 1

Java Programming Training for Experienced Programmers (5 Days)

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Computer Science II (20082) Week 1: Review and Inheritance

Chapter 6 Introduction to Defining Classes

A Short Summary of Javali

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

CS110D: PROGRAMMING LANGUAGE I

ECE 122. Engineering Problem Solving with Java

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

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

Java Review. Java Program Structure // comments about the class public class MyProgram { Variables

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Chapter 9 Introduction to Arrays. Fundamentals of Java

Object Oriented Methods : Deeper Look Lecture Three

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Object Oriented Programming

Methods and Data (Savitch, Chapter 5)

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Lecture Notes for CS 150 Fall 2009; Version 0.5

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CS111: PROGRAMMING LANGUAGE II

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Nested Loops. A loop can be nested inside another loop.

Lecture 10 Declarations and Scope

Programming Language (2) Lecture (4) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University

Anatomy of a Class Encapsulation Anatomy of a Method

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

Lecture 6 Introduction to Objects and Classes

Separate Compilation Model

And Even More and More C++ Fundamentals of Computer Science

Do not start the test until instructed to do so!

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

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

JAVA OBJECT-ORIENTED PROGRAMMING

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Computer Science 1 Ah

Class, Variable, Constructor, Object, Method Questions

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

Transcription:

ECE 122 Engineering Problem Solving with Java Lecture 12 Arrays of Objects

Outline Problem: How can I represent groups of objects in an array Previously considered arrays of primitives This can get complicated How many references are there to objects? Arrays as parameters Arrays can be used as input and return values from methods

One dimensional Array We have introduced one-dimensional array in our previous lectures. An array is a collection of variables of the same type, referred by a common name. type array-name [] = new type[size]; int age[] = new int[5]; //declaration age[0] = 20; //assignment

Definitions Using Array Literals Used when exact size and initial values of an array are known in advance used to initialize the array handle int[] count = { 5,6,3,10 }; Visualize the results of the above command count [0] [1] [2] [3] 5 6 3 10

Contrast Definitions count [0] [1] [2] [3] 0 0 0 0 Elements are primitive values ( 0s in this case) STUDENTS [0] [1] [2] [6] Bashful Doc... Sneezy Elements are objects

Arrays of Objects The elements of an array can be object references The following declaration reserves space to store 5 references to String objects String[] words = new String[5]; It does NOT create the String objects themselves Initially an array of objects holds null references Each object stored in an array must be instantiated separately

Arrays of Objects The words array when initially declared: words - - - - - At this point, the following reference would throw a NullPointerException: System.out.println (words[0]);

Arrays of Objects After some String objects are created and stored in the array: words - - friendship loyalty honor

Arrays of Objects Keep in mind that String objects can be created using literals Following declaration creates an array object called verbs Fills it with four String objects created using string literals String[] verbs = {"play", "work", "eat", "sleep"}; These are referenced by verbs[0] through verbs[3].

Definitions Using Array Literals Create an array of strings String [] names = { "Bashful", "Doc, "Sneezy"}; names [0] [1] [2] Bashful Doc Sneezy

Arrays as Objects In Java, an array is an object. If the type of its elements is anytype, the type of the array object is anytype[ ]. There are two ways to declare an array: anytype [ ] arrname; or anytype arrname [ ]; The difference becomes significant only when several variables are declared in one statement: int [ ] a, b; // both a, b are arrays int a [ ], b; // a is an array, b is not

Arrays as Objects As with other objects, the declaration creates only a reference, initially set to null. An array must be created before it can be used. There are two ways to create an array: arrname = new anytype [ length] ; or Brackets, not parens! arrname = new anytype [ ] { val1, val2,, valn };

Array s Length The length of an array is determined when that array is created. The length is either given explicitly or comes from the length of the { } initialization list. The length of an array arrname is referred to in the code as arrname.length. length appears like a public field (not a method) in an array object.

Arrays as Parameters An entire array can be passed as a parameter to a method The reference to the array is passed, Makes the formal and actual parameters aliases of each other Changing an array element within the method changes the original An individual array element can be passed to a method as well, The type of the formal parameter is the same as the element type. In this case, the call is by value.

Passing to Methods Example: /** * Swaps a [ i ] and a [ j ] */ public void swap (int a[ ], int i, int j) { int temp = a [ i ]; a [ i ] = a [ j ]; a [ j ] = temp; }

Returning Arrays from Methods As for other objects, an array can be returned from a method. The returned array is usually constructed within the method or obtained from calls to other methods. The return type of a method that returns an array with sometype elements is designated as sometype [ ].

Returning from Methods Example: public double[ ] solvequadratic (double a, double b, double c) { double d = b * b - 4 * a * c; if (d < 0) return null; d = Math.sqrt(d); double roots[ ] = new double[2]; roots[0] = (-b - d) / (2*a); roots[1] = (-b + d) / (2*a); return roots; } Or simply: return new double[ ] { (-b - d) / (2*a), (-b + d) / (2*a) };

Command-Line Arguments The signature of the main method indicates that it takes an array of String objects as a parameter These values come from command-line arguments that are provided when the interpreter is invoked For example, the following invocation of the interpreter passes three String objects into main: > java StateEval pennsylvania texas arizona These strings are stored at indexes 0-2 of the array parameter of the main method

Summary We can now make complicated data structures Objects still the basic units of data storage Arrays are fundamental Most data is stored in arrays Allows for easy data access Command line arguments are a good example of an array of strings More to come: two dimensional arrays