Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and.

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

Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand the.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
OOP Languages: Java vs C++
Java: Chapter 1 Computer Systems Computer Programming II Aug
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Java: Chapter 1 Computer Systems Computer Programming II.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
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.
The Java Programming Language
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Types in programming languages1 What are types, and why do we need them?
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Copyright Curt Hill Variables What are they? Why do we need them?
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 5 Introduction to Defining Classes
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Object-Oriented Programming Fundamentals. Comp 241, Fall Example Colored points on the screen What data goes into making one? Coordinates Color.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Topic: Classes and Objects
JAVA MULTIPLE CHOICE QUESTION.
Introduction to Classes and Objects
“Form Ever Follows Function” Louis Henri Sullivan
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
null, true, and false are also reserved.
Java Programming Language
An overview of Java, Data types and variables
Chapter 1: Computer Systems
Value Types and Reference Types
Focus of the Course Object-Oriented Software Development
Object-Oriented Programming
Object Oriented Programming in java
Chap 2. Identifiers, Keywords, and Types
Subtype Substitution Principle
Classes and Objects Object Creation
Chapter 5 Classes.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Objects and Classes Chapter Nine

Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and methods are closely related NOT randomly thrown together

Example of an object A balloon: describe what the balloon looks like - then make as many copies of the object as wanted - needed. “Constructor” a method most often used to initialize variables So what’s so special about it: Same name as class, never returns a value.

Balloon will be seen again in Chapter 10 In chapter 10 with more “Bells and whistles” page

Basics of Balloon A circle “balloon” which you easily could be made more interesting

The program also displays two action buttons one makes it larger, the other smaller X and Y coordinates place the balloon anywhere in the frame (screen) Data and methods are packaged = an object

Classes Java and other OOPs languages can not write instructions directly that define a balloon as a single object. Java makes the programmer define what a balloon is (what it looks like) A class is like a template (generic) and an instance is concrete (object)

Plan first, then build the real thing is designed and as a bonus you can get as many as you need (ie. More cars, trucks, balloons). Class Balloon {some private variables diameter = 10; xCoord =20; yCoord = 20; } // All the new balloons will have the same initial values

Public Balloon ( int initalDiameter, int initalX, int initalY) { diameter = initalDiameter; xCoord = initalX; yCoord = initialY; } //If not noted otherwise set to these values int = zero boolean = false char = \u000 (blank space) and any object = null

Public or private if private we have control no one outside the class can “see” alter out variables or methods “Encapsulation” or information hiding if private Variables... Public one Public two Public three

// some methods in the class (public) can be seen the private ones can not. myBalloon = new Balloon (20, 50, 50); diameterX coord Y coord

Remember from earlier? Balloon anotherBalloon; anotherBalloon = new Balloon (20, 100, 100); Balloon blueBalloon = new Balloon (20, 100,100); // as many balloons as you want/need all instances or objects

A class in the java library TextField data = new TextField ( ); // use Text sometimes similar to drawString for messages on screen public void display (Graphics g) { g.drawOval ( xCoord, yCoord, diameter,diameter); }

Another method Public void actionPerformed (ActionEvent event) { if (event.getSource ( ) = = grow) // make the Balloon larger myBalloon.changeSize(10); if (event.getSource ( ) = =shrink) myBalloon.changeSize( - 10); // smaller repaint( ); }

Class PlayBalloon (two main objects 1. User interface object created from the class PlayBalloon. This is created by the browser or Appletviewer. 2. Balloon object, balloon, created from the class Balloon. This is created by the user interface object. Example pgm pages See it on web

Public data = X and Y coordinates Private data = only accessed within class (not from the outside world) Names of classes same as variables except underscore and $ dollar sign the reserved word “this” if a method needs to refer to the object it is presently working with it can refer to it using “this”

// an example private Scrollbar slider; slider.addAdjustmentListener(this); “instanceof” // example segment if (event.getSource( ) instancesof TextField) handleTextFields(ActionEvent event); // almost same code for instanceof Button //instead of TextField in above code

Built in types (primitive) boolean, char, byte, short, long, float, double Objects created from classes using new //Strings Example String S1 = “abc”, S2 = “abc”; if (S1 = = S2) // Will be false because this tests if they are the same object You can compare if (S1.equals(S2))

Object description & Garbage collection Java has automatic garbage collection some languages do not - So you do not have to worry about freeing memory “overloading” use of methods with same name as each other but difference is in signature Some examples: abc (int x, int y) { } abc ( float y, int z) { } abc ( int a, int b, int c) { } may return void of some type value

“overriding” a method that extend another “static” java reserved word that makes a variable or method of static to be used anywhere within the class (a static variable is “owned” by the class) classes and files: You can have more than one class in a file but only one can by public. Lots of classes within one java source pgm but when compiled each class will have its own file (.class file)