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.
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.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
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.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
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 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
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
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.
Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double.
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 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,
Object Oriented Programming Lecture 2: BallWorld.
Java Programming Language Lecture27- An Introduction.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Primitive data types Lecture 03. Review of Last Lecture Write a program that prints the multiplication table of 5. class MultiplicationTable { public.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Java: Base Types All information has a type or class designation
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
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.
Chapter 6 Arrays Fall 2012 CS2302: Programming Principles.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
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
Chap 2. Identifiers, Keywords, and Types
Agenda Types and identifiers Practice Assignment Keywords in Java
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: ArrayList<Object> arrayRefVar; Fall 2012 CS2302: Programming Principles

Declaring and Creating in one step: Basic Operations Creation: arrayRefVar = new ArrayList<Object>(); Declaring and Creating in one step: ArrayList<Object> myList = new ArrayList<Object>(); Fall 2012 CS2302: Programming Principles

Class definition public class PartialArrayOfObject { /*Where to store the data.*/ private ArrayList<Object> data; /* Constructor: Initialize the array to the given size. This will be the maximum number that can be held.*/ public PartialArrayOfObject(int size) { data = new ArrayList<Object>(); numStored = 0; } /*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 System.out.println("Partial array is full"); }else { for(int j = numStored; j > index; j--) { data[j] = data[j-1]; data.add(index,val); // put the new value in place 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(); /*Fill the array and display each element*/ for(int i=0; i<5;i++){ intArr.add(new Integer(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