©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements Animated Version.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Introduction to Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Ch2: Getting Started with Java - Objectives After.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
IT151: Introduction to Programming
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. slide 1 CS 125 Introduction to Computers and Object- Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 1 Introduction to Object-Oriented Programming and.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes Part 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. slide 1 CS 125 Introduction to Computers and Object- Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
Chapter Chapter 1 Introduction to Object-Oriented Programming and Software Development.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Lecture 1 Introduction to Computers and Object-
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 1 Introduction to Object-Oriented Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 1 Introduction to Object-Oriented Programming and.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 1: Introduction to Object-Oriented Programming.
BCS 2143 Introduction to Object Oriented and Software Development.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes CS 21a: Introduction to Computing I First Semester,
Arrays : Objectives After you have read and studied this chapter, you should be able to –Manipulate a collection of data values, using an array. –Declare.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Sample Slides - 1 Sample Animated Slides for Wu, Intro to OOP.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
BCS 2143 Object Oriented Design Using UML. Objectives Objects Interactions Finding Classes Relationship Between Classes Attribute and Operation Class.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Chapter 1 Introduction to Object-oriented Programming.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
Chapter 2 Getting Started with Java. Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java.
 2005 Pearson Education, Inc. All rights reserved. 1 A class A class is the blueprint from which objects are generated. In other words, if we have six.
1 Training Lecture 1. 2 Primitive Data Declaration and Assignments Code State of Memory int firstNumber, secondNumber; firstNumber = 234; secondNumber.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 1 Introduction to Object-Oriented Programming and.
Chapter 7- Defining Your Own Classes Part 2 : Objectives After you have read and studied this chapter, you should be able to –Describe how objects are.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 4 : Defining Your Own Classes Part 1 - Objectives After you have read and studied this chapter, you should be able to Define a class with multiple.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
Introduction to OOP with Java 4th Ed, C. Thomas Wu
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Your Own Classes Part 1
Defining Your Own Classes
Pemrograman Dasar Methods PTIIK - UB.
Object Oriented Programming in java
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
In this class, we will cover:
Intro to OOP with Java, C. Thomas Wu
Presentation transcript:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive variables: declaration and creation Animated Version

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Objectives After you have read and studied this chapter, you should be able to Describe the difference between object declaration and creation Understand the rules for data names Declare, create, and use objects Describe the difference between primitives and objects Declare, create, and use primitives

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Objects In Java, everything is modeled by an object: –Circle –Room –NimPlayer Objects have Attributes and Behavior –Attributes: The set of descriptions needed for the object –Behavior: The things that the object can do Examples: –Teacher –Account –Coin

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Classes To create an object inside the computer program, we must provide a definition for objects—how they behave and what kinds of information they maintain —called a class. An object is called an instance of a class. Examples: –Teacher class; instances are joel and shannon –Account class; instances are account123 and account456

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Messages and Methods To instruct a class or an object to perform a task, we send a message to it. You can send a message only to the classes and objects that understand the message you sent to them. A value we pass to an object when sending a message is called an argument of the message. Some give answers, some don’t –deposit(12) –balance()

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter account456 : BankAccount account789 : BankAccount account123 : BankAccount Sample Instance Data Value (Attributes) current balance All three BankAccount objects possess the same instance data value current balance. The actual dollar amounts are, of course, different.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter How do you implement it? Implement: to write the ideas in a program We need to be able to: –Create objects –Give them a name for reference later –Pass messages to them

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Declaring an Object creates memory space for it, and gives it a name. The name is called an identifier. Object Declaration Account myAccount; Accountcustomer; Studentjan, jim, jon; Vehiclecar1, car2; Object Name One object is declared here. Object Name One object is declared here. Class Name This class must be defined before this declaration can be stated. Class Name This class must be defined before this declaration can be stated.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Identifiers Names for pieces of data, objects, classes, methods Must be: –One Word –Letters, numbers, $, _ –Start with a letter –Not a reserved Java word Convention –Class names start with upper case letters (Account) –Object names start with lower case letters (myAccount)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Object Creation myAccount = new Account (“Shannon Duvall”) ; More Examples customer = new Customer( ); jon= new Student(“John Java”); car1= new Vehicle( ); Object Name Name of the object we are creating here. Object Name Name of the object we are creating here. Class Name An instance of this class is created. Class Name An instance of this class is created. Argument or Parameter The name of the account holder is given. Argument or Parameter The name of the account holder is given.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Declaration vs. Creation Customer myCustomer; myCustomer = new Customer( ); Customer myCustomer; myCustomer = new Customer( ); 1. The identifier myCustomer is declared and space is allocated in memory. 2. A Customer object is created and the identifier myCustomer is set to refer to it. 1 2 myCustomer 1 : Customer 2

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Name vs. Objects Customer myCustomer; myCustomer = new Customer( ); Customer myCustomer; myCustomer myCustomer = new Customer( ); : Customer Created with the first new. Created with the second new. Reference to the first Customer object is lost.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Shortcut Customer myCustomer; myCustomer = new Customer( ); Customer myCustomer; myCustomer = new Customer( ); Customer myCustomer = new Customer();

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Sending a Message myAccount. deposit ( 400 ) ; More Examples account.addInterest(12); student.setName(“john”); car1.startEngine( ); Object Name Name of the object to which we are sending a message. Object Name Name of the object to which we are sending a message. Method Name The name of the message we are sending. Method Name The name of the message we are sending. Argument or Parameter The argument we are passing with the message. Argument or Parameter The argument we are passing with the message.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Program1 class Program1 { public static void main(String[ ] args) { AccountmyAccount; myAccount = new Account(“Shannon Duvall”); myAccount.deposit(500); myAccount.addInterest(12); myAccount.printAccountInfo(); } Declare a name Create an object Use an object

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter name rate amount :Account Execution Flow myAccount.deposit(400); Account myAccount; myAccount State-of-Memory Diagram.02 0 S D myAccount = new Account(“S D”); Program Code 400

Objects vs. Primitives 4 th Ed Chapter Primitives Smallest pieces of data (like letters, numbers) Building blocks for objects Don’t have attributes and behavior Still declare and create them Types have lower-case letters since they aren’t classes: int double char boolean String (kinda)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Just like declaring objects! Primitive Declaration int width; doubleearnings; charfirstLetter; booleanisCorrect; String word; Variable Name Same rules for names apply. Variable Name Same rules for names apply. Type int, double, char, boolean Type int, double, char, boolean

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Primitive Creation width = 24; More Examples earnings = ; firstLetter= ‘J’; isCorrect= false; word = “John”; Variable Name Name of the variable we are creating here. Variable Name Name of the variable we are creating here. Value To create it, just give it a value that matches its type. It is now said to be initialized. Value To create it, just give it a value that matches its type. It is now said to be initialized.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Shortcut int width; width = 24; int width = 24;

Use the variables Now you can use your variables to do calculations or to issue commands between objects. Each “behavior” is a method. Using the behaviors is “calling” the methods. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter