Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
Arrays and Strings.
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Java Review Interface, Casting, Generics, Iterator.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0.
Improving structure with inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts.
Improving structure with inheritance (Chapters 8 and 9)
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Programming with Collections Collections in Java Using Arrays Week 9.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Java Syntax Primitive data types Operators Control statements.
Grouping Objects 2 Collections and the for-each loop Collections and the while loop.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
More about inheritance Exploring polymorphism 5.0.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
More about inheritance Exploring polymorphism 3.0.
March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Introduction to Java Java Translation Program Structure
ArrayList By Neil Butcher. What is the difference between an ArrayList and an Array? An ArrayList is in many ways similar to an array, but has a few subtle.
Understanding class definitions
Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i.
Introduction to Java Primitive Types Operators Basic input and output.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
OOP with Objective-C Categories, Protocols and Declared Properties.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
Solution of Mid. Term 2009 Consider the following C++ declarations and assignments. int i, j, k ; float x, y ; char c ; bool test ; i = 35 ; j= 5 ; k =
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lecture 17: Polymorphism (Part II)
Data Types.
Understanding class definitions
null, true, and false are also reserved.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
More about inheritance
Building Java Programs
Manipulating Pictures, Arrays, and Loops part 3
Manipulating Pictures, Arrays, and Loops
Lecture Notes – Week 4 Chapter 5 (Loops).
Understanding class definitions
COS 260 DAY 4 Tony Gauvin.
Interfaces.
C Programming Pointers
Improving structure with inheritance
Presentation transcript:

Objects First With Java A Practical Introduction Using BlueJ Casting Week

2 Main concepts to be covered Casting numbers Casting objects The instanceof operator

3 Casting numbers We can easily assign an int to a double because there is no possibility of a loss of precision In the following example, the value 3.0 will be assigned to d int i = 3; double d = i;

4 Casting numbers There would be a compilation error though if we tried to write: double d = 3.9; int i = d; If we want to assign a double to an int (or, for example, to a float) then we need to tell the compiler that we accept the possible loss of precision

5 Casting numbers This process is known as casting We need to cast the number to an int by writing (int) in front of it In the following example, the value 3 will be assigned to i double d = 3.9; int i = (int) d;

6 Casting objects Consider the dome-v2 project The CD class has the following method to return the number of tracks public int getNumberOfTracks() { return numberOfTracks; }

7 Casting objects Suppose we wanted to write a method in the Database class to print the number of tracks of a CD that was stored at a particular position in the array list The Item object returned from the get method would need to be cast into a CD object in order to call the getNumberOfTracks method

8 Casting objects public void printNumberOfTracks(int index) { CD cd = (CD) items.get(index); System.out.println("The number of tracks on the CD is " + cd.getNumberOfTracks()); }

9 The instanceof operator The printNumberOfTracks method could be improved by checking whether the returned Item object is actually a CD object, and only casting it to CD if it is. This could be achieved using the instanceof operator. The expression anObject instanceof AClass evaluates to true if the type of anObject is AClass (or some subclass of Aclass). Otherwise it evaluates to false.

10 The instanceof operator public void printNumberOfTracks(int index) { Item item = items.get(index); if (item instanceof CD) { CD cd = (CD) item; System.out.println("The number of tracks on the CD is " + cd.getNumberOfTracks() + "."); } else { System.out.println("The item is not a CD."); }

11 Review Casting can be used with numbers to, for example, assign a double to an int. Casting can be used with objects when it is required to call a method that only exists in a subclass of the declared type. The instanceof operator can be used to check the type of an object.