Arrays of Objects Fall 2012 CS2302: Programming Principles.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Pointers Prasun Dewan Comp 114.
Wrappers: Java’s Wrapper Classes for the Primitives Types Steve Bossie.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Java Syntax Primitive data types Operators Control statements.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
Objects vs. Primitives Primitive Primitive byte, short, int, long byte, short, int, long float, double float, double char char boolean boolean Object (instance.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
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.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Arrays of Objects 1 Fall 2012 CS2302: Programming Principles.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Primitive Variables.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Chapter One Lesson Three DATA TYPES ©
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Chapter 6 Arrays 1 Fall 2012 CS2302: Programming Principles.
Objectives  File I/O: using Scanner with File  Inserting into Partially Filled Array  Deleting from Partially Filled Array  Static methods and variables.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Java: Base Types All information has a type or class designation
Sixth Lecture ArrayList Abstract Class and Interface
Single Dimensional Arrays
Java: Base Types All information has a type or class designation
CompSci 230 S Programming Techniques
Elementary Programming
Lecture 2: Data Types, Variables, Operators, and Expressions
COP 3503 FALL 2012 Shayan Javed Lecture 8
An Introduction to Java – Part I
Chapter 2.
Java Array Lists 2/2/2016.
Unit-2 Objects and Classes
Lecture 2: Implementing ArrayIntList reading:
null, true, and false are also reserved.
Polymorphism.
Introduction to Java Programming
An Introduction to Java – Part I, language basics
CS 302 Week 8 Jim Williams, PhD.
CS Week 9 Jim Williams, PhD.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Chapter 6 Arrays Fall 2012 CS2302: Programming Principles.
Grouped Data Arrays, and Array Lists.
Recap Week 2 and 3.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
Single-Dimensional Arrays chapter6
Arrays in Java.
Names of variables, functions, classes
Presentation transcript:

Arrays of Objects Fall 2012 CS2302: Programming Principles

Wrappers: Java’s Wrapper classes for the primitive types Contents Wrappers: Java’s Wrapper classes for the primitive types Object class Arrays of Object Fall 2012 CS2302: Programming Principles

Java has a wrapper class for each of the eight primitive data types: Primitives & Wrappers Java has a wrapper class for each of the eight primitive data types: Primitive Type Wrapper Class boolean Boolean float Float byte Byte int Integer char Character long Long double Double short Short Fall 2012 CS2302: Programming Principles

Primitives vs. Wrappers int x = 25; Integer y = new Integer(33); Fall 2012 CS2302: Programming Principles

Use of the Wrapper Classes Java’s primitive data types (boolean, int, etc.) are not classes. Wrapper classes are used in situations where objects are required, such as for elements of a Collection: ArrayList<Integer> a = new ArrayList<Integer>(); a.add(new Integer(2)); Fall 2012 CS2302: Programming Principles

Value => Object: Wrapper Object Creation Wrapper.valueOf() takes a value (or string) and returns an object of that class: Integer i1 = Integer.valueOf(42); Integer i2 = Integer.valueOf(“42”); Boolean b1 = Boolean.valueOf(true); Boolean b2 = Boolean.valueOf(“true”); Fall 2012 CS2302: Programming Principles

Object => Value Each wrapper class Type has a method typeValue to obtain the object’s value: Integer i1 = Integer.valueOf(42); System.out.println(i1.intValue()); Boolean b1 = Boolean.valueOf(“false”); System.out.println(b1.booleanValue()); => 42 false Fall 2012 CS2302: Programming Principles

The Object Class and Its Methods java.lang.Object class The toString() method returns a string representation of the object. The default implementation returns a string consisting of a class name of which the object is an instance, the at sign (@), and a number representing this object. Circle c1 = new Circle(); System.out.println(c1.toString()); Fall 2012 CS2302: Programming Principles

The elements of an array can be object references Array of Object The elements of an array can be object references Declaration: Object [ ] arrayRefVar; Fall 2012 CS2302: Programming Principles

Declaring and Creating in one step: Basic Operations Creation: arrayRefVar = new Object[arraySize]; Declaring and Creating in one step: Object[ ] myList = new Object[10]; Fall 2012 CS2302: Programming Principles

Class definition public class PartialArrayOfObject { /*Where to store the data.*/ private Object[] data; /*How many are actually stored.*/ private int numStored; /* Constructor: Initialize the array to the given size. This will be the maximum number that can be held.*/ public PartialArrayOfObject(int size) { data = new Object[size]; numStored = 0; } /*Get the element at index i.*/ public Object get(int i) { if(0 <= i && i < numStored) { return data[i]; } else { System.out.println(“Index is out of range.”); /*Add an element to the end of the array.*/ public void add(Object val) { ………… } Fall 2012 CS2302: Programming Principles

Class definition /*Add an element to the end of the array.*/ public void add(Object val) { if(numStored < data.length) { data[numStored] = val; numStored++; } else {// no more room System.out.println("Partial array is full"); } /*Insert the string val into the array so that it ends up with the given*/ public void insertAt(Object val, int index) { if(index < 0 || index > numStored) { System.out.println(“Insert index out of bounds”); } else if(numStored >= data.length) {// no more room }else { for(int j = numStored; j > index; j--) { data[j] = data[j-1]; data[index] = val; // put the new value in place numStored++; // one more element stored Fall 2012 CS2302: Programming Principles

Create an integer array public class Test { public static void main(String[] args) { /*create an object array with size 5*/ PartialArrayOfObject intArr = new PartialArrayOfObject(5); /*Fill the array and display each element*/ for(int i=0; i<5;i++){ intArr.add(i); System.out.println(“Element " + i + “ is: ” + intArr.get(i)); } /*Calculate total*/ int total = 0; total += (Integer)intArr.get(i); System.out.println("Total is " + total); Fall 2012 CS2302: Programming Principles