Arrays, Casting & User Defined Types

Slides:



Advertisements
Similar presentations
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Advertisements

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.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 6 Decision Making, Control Structures & Error Handling.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
1 Lecture # 4. * An array is a group of contiguous or related data items that share a common name. * Each value is stored at a specific position * Position.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
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.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Java FilesOops - Mistake Java lingoSyntax
BIT115: Introduction to Programming
int [] scores = new int [10];
Chapter 9 Introduction to Arrays Fundamentals of Java.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Array in C# Array in C# RIHS Arshad Khan
Sections 10.1 – 10.4 Introduction to Arrays
GC211 Data Structure Lecture 1 Sara Alhajjam.
Introduction to Computing Using Java
CISC124 Labs start this week in JEFF 155: Meet your TA.
Building Java Programs
Statements, Comments & Simple Arithmetic
Building Java Programs Chapter 7
CSC 113 Tutorial QUIZ I.
5 Variables, Data Types.
Building Java Programs
Building Java Programs
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Java Tutotrial for [NLP-AI] 2
Building Java Programs
Building Java Programs
Introduction To Programming Information Technology , 1’st Semester
ARRAYS 1 GCSE COMPUTER SCIENCE.
Can store many of the same kind of data together
Object Oriented Programming in java
BIT115: Introduction to Programming
python.reset() Also moving to a more reasonable room (CSE 403)
CS2011 Introduction to Programming I Arrays (I)
MSIS 655 Advanced Business Applications Programming
16 Strings.
int [] scores = new int [10];
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:
Bugs & Debugging - Testing
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays in Java.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Java Programming Language
Java Basics Data Types in Java.
Creating and Modifying Text part 3
Building Java Programs
Building Java Programs
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Agenda Remarks about last homeworks Class methods(static methods)
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
How do you do the following?
Presentation transcript:

Arrays, Casting & User Defined Types 11 Arrays, Casting & User Defined Types

Previously Types of Errors When errors appear Compiler Errors Run-time Errors Fixing Errors Find Errors

Overview Data Structures Arrays Java Arrays Java Enhanced for loop User Defined Data Types

Data Structures A data structure is an organised collection of related data Related data items stored together Data structures contain more than one item of data Variables stores data Arrays are data structures

Data Structures To represent a car we would use a data structure composed of its characteristics What information related to a car would you store? Colour Maker Registration int, RGB encoding String String

Arrays Group of data items All items of the same type, e.g. int Items accessed by integer indexes Starting index with zero Last index is one less than the length Of pre-determinate size The length of an array is the number of items it contains The length is fixed at creation time

Java Arrays Arrays are variables Arrays must be declared <type>[] <identifier>; or <type> <identifier>[]; Examples: String[] astrCityNames; or String astrCityNames[]; int[] aiAmounts; or int aiAmounts[]; Variable identifiers always start by lowercase letters, ‘_’, ‘$’ or ‘£’ (camel format)

Java Arrays Access to the size of the array by using length astrCityNames.length Trying to access items out of the size of the array will throw an exception ArrayIndexOutOfBoundsException of type IndexOutOfBoundsException

What is the length of the above array? Java Arrays Arrays initialisation Initialised when declared String[] astrCityNames = {"Valencia", "Madrid", "London", "Rome"}; What is the length of the above array? 4

Java Arrays Declare, create and initialise // Declaration String[] astrCityNames; // Creation astrCityNames = new String[4]; // Initialisasion astrCityNames[0] = "Valencia"; astrCityNames[1] = "Madrid“; astrCityNames[2] = "London“; astrCityNames[3] = "Rome";

Java Arrays Arrays when created are automatically initialised to default values Primitives to zero int[] aiAmounts = new int[2]; System.out.println("Amount at 0 is " + aiAmounts[0]); System.out.println("Amount at 1 is " + aiAmounts[1]); Amount at 0 is 0 Amount at 1 is 0

Java Arrays Arrays when created are automatically initialised to default values (continuation) Objects to null String[] astrCityNames = new String[2]; System.out.println("City at 0 is " + astrCityNames[0]); System.out.println("City at 1 is " + astrCityNames[1]); City at 0 is null City at 1 is null

Lets do an exercise for arrays in Eclipse Java Arrays Lets do an exercise for arrays in Eclipse

Java Enhanced for loop Enhanced for loop Java 1.5 or higher Simplifies looping through a collection or array String[] astrNames = {"Adam", "Amadeo", "John"}; for (String strName : astrName) { System.out.println(strName); } // end for String astrNames astrName Adam Amadeo John

Java Enhanced for loop Previous is equivalent to String[] astrNames = {"Adam", "Amadeo", "John"}; for (int iIndex = 0; iIndex < astrName.length(); ++iIndex) { String strName = astrName[iIndex]; System.out.println(strName); } // end for

User Defined Data Types A data structure Contains different data types Example of car Colour Maker Registration Lets create the car data type! int, RGB encoding String String

User Defined Data Types class Car { // Member variables int miColour; // RGB String mstrMaker; String mstrRegistration; } // end class Car Member variables if not explicitly initialised then they are initialised to default values by Java Initialisation can be done at declaration time

User Defined Data Types class Car { // Member variables int miColour; // RGB String mstrMaker; String mstrRegistration; public static void main(String[] astrArgs) { Car myCar; // define myCar = new Car(); // create instance of the data type myCar.miColour = 0xff0000; // red - rrggbb myCar.mstrModel = "Ferrari"; } // end main() } // end class Car My car is red and it is a Ferrari I WISH! Comments should exist but we don’t have space in the presentation

User Defined Data Types Lets build the car project in Eclipse!