Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal

Slides:



Advertisements
Similar presentations
1 Arrays, Strings and Collections [1] Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering.
Advertisements

Arrays.
Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.
RAPTOR Syntax and Semantics By Lt Col Schorsch
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,
1 Various Methods of Populating Arrays Randomly generated integers.
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Programming with Collections Collections in Java Using Arrays Week 9.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Arrays and Strings A way to make oodles of variables, and a deeper look at classes.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
ArrayList, Multidimensional Arrays
Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Java – Part II Lecture Notes 4. Arrays An array is a data structure that groups and organizes data l Array is a list of values (int, double, aggregates)
1.00/ Lecture 8 Arrays and Vectors. Arrays-1 Arrays are a simple data structure Arrays store a set of values of the same type – Built-in types.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Java SE 8 for Programmers, Third Edition
Chapter 11. Multidimensional Arrays and Vectors Multidimensional Arrays Vectors Computer Programming with JAVA.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
CSE 1201 Object Oriented Programming ArrayList 1.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Multidimensional Arrays l Vectors Multidimensional Arrays.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
1 Unit-2 Arrays, Strings and Collections. 2 Arrays - Introduction An array is a group of contiguous or related data items that share a common name. Used.
Non-Static Classes What is the Object of this lecture?
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Chapter 8: Loops, Arrays, Strings Loop statements –do –while –for Arrays –declaration, allocation, initialization, access –multi-dimensional –heterogeneous.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter VII: Arrays.
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Array, Strings and Vectors
Numeric Arrays Numeric Arrays Chapter 4.
Object Oriented Programming in java
Introduction to Data Structure
Arrays.
Presentation transcript:

Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal

Recap Assignment 4 Flow Control –Conditions / Decisions –Loops

Today Assignment 04 Arrays – Strings Thinking in Java – Chapter 4,5

Arrays Need for an Array [group] –To group elements of a particular type –To apply a procedure to individual elements within a larger group Example: assume we have 50 Circles –We do not want to create 50 different variables. –What would the code to find the sum of their areas look like? –Why 50? Why any pre-determined number? Why not let the user determine the number?

Java Arrays The Array is an ordered group data elements of a single data type (primitive or reference). Java arrays are objects (reference type) Arrays can be declared and initialized seperately –Declaration is by specifying the data type –Initialization requires the number of elements to be stored. int[] intArray; //square brackets declare the array intArray = new int[10]; // memory for 10 int’s are allocated and intArray points to the array note the “new” statement int intArray[] = new int[10] // alternative – same result

Index The value within the square brackets define the order of the element of interest within the array. (Its index) Java arrays start counting the index at 0. (not 1) intArray[2] = 7; // the third element in the array is 7

.length Java arrays are objects. –Slightly slower. –Lots of convenience Each array has a variable “.length“. This variable remembers the storage capacity the array. Very convenient in setting up loops int index = 0; while ( index < intArray.length ) { System.out.println (intArray[index++]); }

Initialization of Elements Nothing to be done for primitives Object arrays are reference types –A new object needs to be initialized at the reference point. Circle[] circleArray = new Circle[10]; for (int j=0; j<10; j++) { circleArray[j] = new Circle(); }

Multi Dimensional Arrays Arrays do not have to represent a one dimensional relationship (queues) between elements. Multi-dimensional structures (tables, time-series) can also be abstracted –Note that humans have difficulty in imagining beyond three dimensions Cell[][] spreadsheet = new Cell[25][50]; spreadsheet[row][column]; classroom[][][] university= new classroom[10][8][4]; university[building][floor][room]

Pre-known Data Alternative way to initialize an array involves providing a listing of all the data to be stored. int[] fibonacci = {0,1,1,2,3,5,8,13}; For multi-dimensional arrays: int[][] matrix= { {0,1,2},{1,0,1},{34,23,6} };

java.lang.Vector Array size is fixed once the array is initialized. When more data needs to be added than the size allows, a new array of a larger size needs to be initialized and the elements of the old array copied into the new one. Vector Class is a data structure that has no size limitation. Vectors are utilized through the methods the class provides. Some methods: public Vector(int initialSize); public Vector(); // create empty vector public void addElement(Object obj); public Object elementAt(int index); public void insertElementAt(int index); public void removeElementAt(int index); public int indexOf(Object obj);

Character Arrays (String) Text is an array of characters (string) Java provides the String class for convenient manipulation of these character arrays. Using the String class is almost as easy as using primitive data types.

String Points to watch out for –The content cannot be subjected to comparison by the “==“ operator. the.equals() method needs to be used. firstString.equals(String secondString) –String object does not allow altering the content directly. Useful to use helper classes

StringBuffer String class is not designed to modify the character contents (value) but to manage the container StringBuffer class should be used to add to or strip characters from the array.

StringBuffer methods Some examples: –public int charAt(int index) –public StringBuffer append(“anytype” t) –public StringBuffer insert(int offset, “anytype” t) –public deleteCharAt(int index) –public delete(int start, int end) –public StringBuffer subString(int start, int end) –public String reverse() –…

preparation - chapter 4,5 Chapter 4 –Constructor –Overloading –this() for calling one constructor from others –static –finalize() the method that is called before the object is deleted Chapter 5 –package * –public – private – protected – "friendly"

Assignment 05 Add the following to the Circle class –Variable for name –Variable for color Substantial changes in CircleApplication –Create 100 circles Each should have a name in the form of “Circle-XXX" according to its order of creation. Use Math.random() to assign the following properties –Color: Either Red, Green, Blue, Yellow, Puple, Cyan, Orange, Brown, Black, or White (use an array for color list) –Coordinates for the center and the radius

Assignment 05 –Reports to be displayed: List the circles in alphabetical order, displaying the properties of each. Sum of all areas and the average of all areas The number of circles for each color List of red circles sorted in order of size. The farthest and closest circles (in terms of their centers) to a randomly determined point.

Next week Preparation: Thinking in Java Chapter 6