BBIT 212/ CISY 111 Object Oriented Programming (OOP) Objects and classes, object interactions in Java 22.02.2011.

Slides:



Advertisements
Similar presentations
1 Inheritance Classes and Subclasses Or Extending a Class.
Advertisements

10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Object Oriented Programming with Java
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
CSCI 160 Midterm Review Rasanjalee DM.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Web Application Development Slides Credit Umair Javed LUMS.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
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.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
LESSON 4 - Variables JAVA PROGRAMMING. Variables Variable is a named storage location that stores data and the value of the data may change while the.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The Java Programming Language
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.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
ECE122 Feb. 22, Any question on Vehicle sample code?
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
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.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Introduction to Programming in Java
Object Oriented Programming (OOP) LAB # 8
Starting JavaProgramming
null, true, and false are also reserved.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 1: Computer Systems
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Recap Week 2 and 3.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Expressions and Assignment
Focus of the Course Object-Oriented Software Development
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Class.
In this class, we will cover:
Presentation transcript:

BBIT 212/ CISY 111 Object Oriented Programming (OOP) Objects and classes, object interactions in Java

(General!) Recap The two steps of Object Oriented Programming – Making Classes: Creating, extending or reusing abstract data types. – Making Objects interact: Creating objects from abstract data types and defining their relationships.

22 nd Feb RECAP Anatomy of a Java Program – Comments – Reserved words – Modifiers – Statements – Blocks – Classes – Methods – The main method

Recap From the notes! – Variables What they are? Declaring Initializing * – Operators Numeric operators Shorthand operators Increment and Decrement operators – Constants – Expressions, Statements and Blocks

Variables – A variable is a “place-holder” (in memory) representing data of a certain type – Variable declaration tells the compiler “the name of the variable as well as what type of data it represents” datatype variableName; E.g. – int radius; – double pie; – If variables are of the same type, they can be declared together, as follows: datatype variableName1, variableName2,..., variableNameX; E.g: double pie, perimeter, area;

– Declaring and initializing variables in one step – Variables often have initial values. You can declare a variable and initialize it in one step. int radius = 7; This is equivalent to the next two statements: int radius; radius = 1; – You can also use a shorthand form to declare and initialize variables of the same type together. For example, int i = 1, j = 2;

(Numeric) Operators

Shorthand Operators – Very often the current value of a variable is used, modified, and then reassigned back to the same variable. – E.g.: i = i + 8; – Java allows you to combine assignment and addition operators using a shorthand operator. For example, the example statement can be written as: i += 8;

Shorthand Operators (2)

– There are two more shorthand operators for incrementing and decrementing a variable by 1. This is handy because that is often how much the value needs to be changed.

Variable types – A variable's data type determines the values it may contain, plus the operations that may be performed on it – A primitive type is predefined by the language and is named by a reserved keyword. Java has eight such: Byte, short, int, long, float, double, boolean, char – REF: Primitive types in Java.pdf

Today 1.Objects and classes 2.Object interactions 3.Others 1.A sample form of the exercise from last week + introduce Flow control 2.CAT next week

Recap Exercise (in groups!) – Creating a table (as reference) to primary school students for Perimeter and Area of a Circle RadiusPerimeterArea

What you should know by now! – The anatomy of a class – The main method (signature, use) – Variables declaration and use

Today 1.Objects and classes 2.Object interactions 3.Others 1.A sample form of the exercise from last week + introduce Flow control 2.CAT next week

Are objects same as classes?

– NO! – A class is a data structure (like a variable definition) that will be used later on – An object is an instance (a specific use of a class)

E.g. – A person class Defines some properties that ANY person can have name, date of birth, identification, etc For a specific person (e.g. named “Mary” or “Tom”), we need to create an instance of a Person and set values of person’s name, date of birth, identification, etc accordingly – A Person Object (say Mary) Mary, 01/01/80, 144

public class Person{ String name; String dateOfBirth; int identification; public static void main (String args []) { System.out.println ("This is a Peron Class"); }

public class Person{ String name; String dateOfBirth; int identification; // a method to print details of a person public void printDetails(){ System.out.println("Name is:" + name); System.out.println(" Born: " + dateOfBirth); System.out.println(" ID Number: " + identification ); } public static void main (String args []) { System.out.println ("This is a Peron Class"); }

How are objects created? – A class constructor is used to create instances of a class by using the new keyword – Recall

public class Person { String name; String dateOfBirth; int identification; // a method to print details of a person public void printDetails(){ System.out.println("Name is:" + name); System.out.println(" Born: " + dateOfBirth); System.out.println(" ID Number: " + identification ); } //Constructor a Person class that receives the person details Person(String a,String b, int c ) { name = a; dateOfBirth = b; identification = c; }

public class Person { String name; String dateOfBirth; int identification; // a method to print details of a person public void printDetails(){ System.out.println("Name is:" + name); System.out.println(" Born: " + dateOfBirth); System.out.println(" ID Number: " + identification ); } //Constructor a Person class that receives the person details Person(String a,String b, int c ) { name = a; dateOfBirth = b; identification = c; } public static void main (String args []) { //using the constructor to create an instance of a Person Person mary = new Person("Mary", "01/01/90",144); mary.printDetails(); }

Demo

Class constructor – Constructor name is class name. A constructors must have the same name as the class its in – Default constructor. If you don't define a constructor for a class, a default parameterless constructor is automatically created by the compiler. The default constructor calls the default parent constructor (super() from the Object class) and initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans).

– Default constructor is created only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created. – Differences between methods and constructors. There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value. There is no return statement in the body of the constructor. The first line of a constructor must either be a call on another constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.

Today 1.Objects and classes 2.Object interactions 3.Others 1.A sample form of the exercise from last week + introduce Flow control 2.CAT next week

We will create a sub class Student in the Person class

Today 1.Objects and classes 2.Object interactions 3.Others 1.A sample form of the exercise from last week + introduce Flow control 2.CAT next week

Practice !