Working With Objects Tonga Institute of Higher Education.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Introduction to Methods
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Java Classes Using Java Classes Introduction to UML.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
The Java Programming Language
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Classes CS 21a: Introduction to Computing I First Semester,
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
CPS120: Introduction to Computer Science
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
Chapter 1 Object Orientation: Objects and Classes.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Working With Objects Tonga Institute of Higher Education.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Copyright Curt Hill Variables What are they? Why do we need them?
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Object Oriented Programing (OOP)
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
CPS120: Introduction to Computer Science Variables and Constants.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Chapter 1 Object Orientation: Objects and Classes.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Chapter VII: Arrays.
More About Objects and Methods
Console Output, Variables, Literals, and Introduction to Type
Introduction to Computer Science / Procedural – 67130
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Lecture 11 B Methods and Data Passing
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 6 Methods: A Deeper Look
Introduction to Java part 2
Tonga Institute of Higher Education
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.
Defining Classes and Methods
Tonga Institute of Higher Education
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Tonga Institute of Higher Education
Object Oriented Programming in java
Classes CS 21a: Introduction to Computing I
Chap 2. Identifiers, Keywords, and Types
Presentation transcript:

Working With Objects Tonga Institute of Higher Education

Introduction The building block of an object-oriented language is an object. Object - A self-contained entity that contains data and procedures to manipulate the data. An object is like a tool that we can use to do things. In Java, almost everything is an object.  String  Integer  System  PrintStream  Etc. Java has over 2,700 pre-built objects for us to use. You can find a list of pre-built objects in the Java Developer’s Kit documentation.

Advantages of Object Oriented Programming Code is easier to maintain  Code is more readable  Encapsulation Show what the object can do  This is normally what we want to know Hide how the object does it  This can be very complicated  We often don’t care how it is done Code is easier to re-use  A piece of code, once written, should not be thrown away. It is best to re-use the code in other programs. Example: Millions of people use System.out.println(). But it was only written once. Code development is more efficient  You don’t code the same thing over and over again

Advantage of Encapsulation Show what the object can do.  This is accomplished by exposing the signature. Signature – The combination of method name and parameters used to uniquely identify a method. Hide the code that the object uses.  The person using the object doesn’t know how it works. Therefore, we can change the code in a method and don’t need to update the programs using it if we don’t change the signature. If others are using your signature, do not change it!  If you do, you will cause everybody using your object to crash. class Math { public double getPI() { return 3.14; } class Math { public double getPI() { return ; }

Objects Objects are like primitive data types with extra functionality. Generally, use them like primitive data types. Except:  Capitalize the first letter of an object data type Ex: byte vs. String  They have methods that provide extra functionality Look at Java documentation to find functionality. Examples:  String  String.charAt(int index)  String.compareTo(String anotherString)  Date  Date.getHours()  Character  Character.compareTo(Character anotherCharacter)  They have constructors that let you create them. Primitive data types can have object wrappers.  Ex: The object wrapper for the int primitive data type is Integer.

Classes vs. Objects Object - A self-contained entity that contains data and procedures to manipulate the data. Class - The blue print or design for creating an object. Instantiate – The act of creating an object from a class Instance – An instantiated class/object

Using Object Variables 2 Steps to using variables 1. Declare the variable 2. Instantiate the variable / Initialize the variable

Declaring Object Variables – 1 Declare the variable – Tell the computer to reserve a space in memory for the variable. You need to tell the computer 2 things: 1. Name of the variable 2. Type of the variable (What kind of variable you have) Object types  Integer  String This works exactly the same for primitive variables! Type Name

Declaring Object Variables – 2 Use a name that is easy to remember.  Do not use x, y, z Variable names must start with a letter, underscore or dollar sign. Variables should begin with a lowercase character. Then a capital letter for each next word. Examples  firstName  customerID This works exactly the same for primitive variables!

Instantiating Object Variables / Initializing Object Variables Instantiate – The act of creating an object from a class  Use the new keyword Initialize the variable – Assign an initial value to a variable.  A newly instantiated object can be used to initialize a variable  Char values must be enclosed in single quotes.  String values must be enclosed in double quotes. New KeywordType of Object Parameters may not be required

Declaring and Initializing Object Variables in 1 line You can declare and initialize a variable in 1 line.

Strings are Special They are objects, but you can use them like primitives Is the same as

Demonstration Declaring, Instantiating and Initializing Variables

Constructors Constructor – A method that is automatically executed when an object is created.  This allows you to set initial values for the object.  Many objects have multiple constructors. (They are overloaded) You can find a list of constructors in the Java Developer’s Kit documentation.

Demonstration Constructors

Attributes / Fields Attributes / Fields – A variable that a class allows others to see  Use dot notation to access it Example:. Example: JOptionPane.INFORMATION_MESSAGE  You can find a list of fields in the Java Developer’s Kit documentation.

Demonstration Attributes / Fields

Methods Methods - Pieces of code that perform a single function  Use dot notation to access it Example:. ( )  You can find a list of methods in the Java Developer’s Kit documentation. Calling a Method – The act of using a method We’ve already used a lot of methods:  println(…)  main(…)  intValue(…)

Method Inputs Some methods take inputs  Parameter/Arguments – A piece of information that provides additional information to the method as to how it should behave.  Parameters should be in the parenthesis next to the method name  The order they are passed is important  Values are separated by commas  Even if you aren’t passing any parameters, you still need to use () Example: Integer.intValue() Method InputOutput Input InformationMethod Name

Method Outputs Some methods return outputs  When something is returned, it may or may not be used. The programmer chooses what to do with the data returned.  Only one thing may be returned.  Void means nothing is coming back Function – A method that returns a value Method InputOutput Information

Demonstration Methods

Method Overloading If two methods do the same thing, they should have the same name Overloading - Having multiple methods with the same name but different parameters Java determines the correct method to use by matching up the number and type of arguments. Therefore, you can’t have 2 methods with the same name and same number & type of arguments. Without overloading, we would have to remember more function names. That would make code more complicated.

Demonstration Method Overloading

Members Member – An attribute/field or method. Sometimes used to refer to attributes/fields and methods as a whole.