Object Oriented Programming Object and Classes Lecture 3 MBY.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chapter 2 - Introduction to Java Applications
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 3 Variables Primitive Data Types calculations & arithmetic operators Readings: Chapter 2.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2008.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. Modified for use with this class. All rights reserved. 1 Introduction to Computers and Programming in JAVA: V Primitive.
© 2006 Pearson Education Using Objects 1 of 32 USING OBJECTS What are Objects? Properties and Capabilities Classes and Instances Making Instances A Sample.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Chapter 2 - Introduction to Java Applications
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Java Objects and Classes. 3-2 Object Oriented Programming  An OO program models the application as a world of interacting objects.  A typical Java program.
Introduction To Object-Oriented Programming. Object-Oriented Programming Class: Code that defines the behavior of a Java programming element called an.
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.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Introduction to Programming Writing Java Beginning Java Programs.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
Introduction to Java Programming. History F James Gosling and Sun Microsystems F Oak F Java, May 20, 1995, Sun World F HotJava –The first Java-enabled.
The Java Programming Language
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary.
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
1 Java Programming 1 Introduction to Basic GUI Lesson 4.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
Introduction to Programming Writing Java Beginning Java Programs.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
Using Objects 1 of 22 Computer Programming Object Classes.
September 7, 2006Using Objects 1 of 32 USING OBJECTS What are Objects? Properties and Capabilities Classes and Instances Making Instances A Sample Program.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
Classes, Interfaces and Packages
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing a Line of Text 2.2.1Compiling and Executing.
Variables in Java A variable holds either
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
Yanal Alahmad Java Workshop Yanal Alahmad
2.5 Another Java Application: Adding Integers
Objectives Identify the built-in data types in C++
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to Java Applications
MSIS 655 Advanced Business Applications Programming
Chapter 2 - Introduction to Java Applications
Anatomy of a Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Object Oriented Programming in java
Chapter 2 - Introduction to Java Applications
F II 2. Simple Java Programs Objectives
Presentation transcript:

Object Oriented Programming Object and Classes Lecture 3 MBY

What are Objects? Building blocks of a software – A set of cooperating objects that work together by sending messages to each other Object model tangible things – school – Car Objects model conceptual things – meeting – date Objects model processes – finding a path through a maze – sorting a deck of cards

What does an Object have? Objects have – capabilities: what they can do, how they behave. Capabilities are also know as behavior. Capabilities include constructors commands and queries. – properties: features that describe them Properties can be attributes, components, associations.

Example of Capabilities Example: Jugs are capable of performing specific actions - constructor: be created. - commands: add water, empty yourself. - queries: reply whether lid is open or closed, or whether jug is full or empty.

Examples of Properties and object state Example: Jugs contain the following properties - attributes: color, material, smell - components: lid, container - associations: a jug can be associated with the room it’s in. Object State: collection of all of an object’s properties; changes if any property changes - some don’t change, e.g., steering wheel of car - others do, e.g., car’s color

Classes Object class – a class is a category of object – defines capabilities and properties common among a set of individual objects all trash cans can open, close, empty their trash – defines template for making object instances particular trash cans may have a metal casing, be blue, be a certain size, etc.

Classes Classes implement capabilities as methods – a method is a sequence of statements in Java – objects cooperate by sending messages to others – each message “invokes a method” i.e., Java executes the sequence of statements in the method in response to a message. Classes implement properties as instance variables – slot of memory allocated to the object that can hold a potentially changeable value

Example of class String city = “Lahore”; – Why the “S” in String is not in small caps like int, double, etc. – String is an existing Java class and it is used to store values specified as strings. – As a rule of thumb in Java anything specified in double quotes “ ” is a String.

Syntax for Using Existing Classes Existing classes are instantiated into objects by this syntax. String city = new String() Tells the compiler to search for the definition of a class named “String” A unique name for the object in the memory “new” keyword to reserve the space for the type String in the memory String() is a constructor of the class “String”

Constructor Constructor is a special method that is called whenever a class is instantiated (created) – another object sends a message that calls a constructor – A constructor is the first message an object receives and cannot be called subsequently – establishes initial state of properties for instance Constructors have special syntax: – must always have same name as class name

Constructors (cont’d) A class could have many constructors e.g. String city = new String() & String city = new String(“Lahore”) Both construct a String object but in the first no initial value is given to the String and in the second a value “Lahore” is given.

Welcome Program class Hello { public static void main(String[] args) { System.out.println(“Welcome to java”); } } Hello.java class is a keyword in Java language, used to define a class. Hello is the name of class. The class keyword must be followed by the its name. By convention name of the class must be started with capital letter. Name of class can be any valid identifier. Normally class is given a name of a noun.

Welcome Program (Cont.) class Hello { public static void main(String[] args) { System.out.println(“Welcome to java”); } } Hello.java Java program must be written in form of classes. Program file must be save with the same name as the class. main function is the entry point in Java. Class definition starts with opening curly bracket and end with closing curly bracket similarly as function/method in java.

What is Java Package, and API? Java’s numerous predefined classes are grouped into categories of related classes called packages. The packages are referred to collectively as the Java class library, or the Java applications programming interface (Java API)

Core Packages and Extension Packages The packages of the Java API are split into core packages and extension packages. The names of the packages begin with either “java” (core packages) or “javax” (extension packages). e.g java.lang, java.util, java.io etc are core packages whereas javax.swing are extension packages.

Including a package import key word is used to include a package in the program. A package must be imported in java program before using classes contained in that package. import javax.swing.JOptionPane is the syntax to import JOptionPane class in the program which is available in java extention package, swing. Why did we not import any package in our first program ?

Automatical inclusion Java automatically imports classes from package java.lang, such as class System. Therefore, import statements are not required for classes in package java.lang.

Another Example // An addition program. // Java extension packages import javax.swing.JOptionPane; // import class JOptionPane public class Addition { // main method begins execution of Java application public static void main( String args[] ) { String firstNumber; // first string entered by user String secondNumber; // second string entered by user int number1; // first number to add int number2; // second number to add int sum; // sum of number1 and number2 // read in first number from user as a String firstNumber =JOptionPane.showInputDialog( "Enter first integer" );

Addition Example Cont. // read in second number from user as a String secondNumber = JOptionPane.showInputDialog( "Enter second integer" ); // convert numbers from type String to type int number1 = Integer.parseInt( firstNumber ); number2 = Integer.parseInt( secondNumber ); // add the numbers sum = number1 + number2; // display the results JOptionPane.showMessageDialog( null, "The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE) System.exit( 0 ); // terminate application } // end method main } // end class Addition

Dialogues Following dialogues will be displayed in response to our addition example