Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Basics of Classes in Java.
Advertisements

Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Tutorial 6 February 4th/5th, 2015
Road Map Introduction to object oriented programming. Classes
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.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Introduction to Java Programming, 4E Y. Daniel Liang.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
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.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
1 Classes and Objects in Java Basics of Classes in Java.
1 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java.
(Quickly) More on Null references: A null reference is a pointer to nothing. Pointers = space for address in memory Think of RAM, with each space in memory.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Objects and Classes Mostafa Abdallah
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Introduction to Object-Oriented Programming Lesson 2.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
OOP Basics Classes & Methods (c) IDMS/SQL News
Object Oriented Programming Object and Classes Lecture 3 MBY.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Topic: Classes and Objects
Lecture 3: Introduction to Object and Classes
Classes and Objects in Java
Classes and Objects in Java
Classes and Objects in Java
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Start Simple: Class for rectangle: Class for bank account?
Chapter 9 Objects and Classes
Chapter 9 Objects and Classes Part 01
Dr. R Z Khan Handout-3 Classes
OO Programming Concepts
Chapter 7 Objects and Classes
Presentation transcript:

Lecture 3

2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines the “state” and “behaviour” of each object made from the class. A class essentially serves as a template for an object and behaves like a basic data type, e.g., “int”. It is therefore important to understand: how the properties (fields) and methods are defined in a class how they are used to build a Java program

Classes Contains definition for all Objects of the same type defines the data types and names of properties defines methods that define behavior the “template” for all Object instances of the same type

5 Classes The basic syntax for a class definition: Bare bone class – no fields, no methods public class Circle { // my circle class } class ClassName { [fields declaration] [methods declaration] }

6 Classes

7 Adding Properties (Fields): Add fields public class Circle { double radius; // radius of the circle double x, y; // center coordinate }

8 Constructors Circle() { } Circle(double newRadius) { radius = newRadius; } Notice: no return value – these are not methods! These are constructors. They make a Circle object. Constructors are methods that are invoked to construct objects.

9 Adding Methods A class with only data fields has no life. Objects created from such a class cannot respond to anything. Methods are declared inside the body of the class and after the declaration of data fields and the constructors. The general form of a method declaration is: type MethodName (parameter-list) { Method-body; }

10 Adding Methods to Class Circle public class Circle { double radius; // radius of circle double x, y; // center of the circle //Methods to return circumference and area public double circumference() { return (2*Math.PI*radius); } public double area() { return (Math.PI * Math.pow(radius,2)); } Method Body

11 Data Abstraction Declare the Circle class, have created a new data type – Data Abstraction Can define variables (objects) of that type: Circle aCircle; Circle bCircle;

12 Creating Circle objects cont. Circle aCircle; Circle bCircle; aCircle, bCircle simply refers to a Circle object, not an object itself. (e.g., this is going to be a Circle, but it isn’t yet.) aCircle Points to nothing (Null Reference) bCircle Points to nothing (Null Reference) null

13 Creating objects of a class Objects are created using the new keyword. aCircle and bCircle refer to Circle objects bCircle = new Circle() ; aCircle = new Circle() ;

14 Creating objects of a class aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle; P aCircle Q bCircle Before Assignment P aCircle Q bCircle Before Assignment

15 Automatic garbage collection The object does not have a reference and cannot be used in future. The object becomes a candidate for automatic garbage collection. Java automatically collects garbage periodically and releases the memory used to be used in the future. Q

16 Accessing Object/Circle Data Circle aCircle = new Circle(); aCircle.x = 2.0; // initialize center and radius aCircle.y = 2.0; aCircle.radius = 1.0; ObjectName.VariableName ObjectName.MethodName(parameter-list)

17 Executing Methods in Object/Circle Using Object Methods: Circle aCircle = new Circle(); double area; aCircle.r = 1.0; area = aCircle.area(); sent ‘message’ to aCircle

18 Using Circle Class // Circle.java: Contains both Circle class and its user class //Add Circle class code here class MyMain { public static void main(String[] args) { Circle aCircle; // creating reference aCircle = new Circle(); // creating object aCircle.x = 10; // assigning value to data field aCircle.y = 20; aCircle.radius = 5; double area = aCircle.area(); // invoking method double circumf = aCircle.circumference(); System.out.println("Radius="+aCircle.r+" Area="+area); System.out.println("Radius="+aCircle.r+" Circumference ="+circumf); } Radius=5.0 Area=78.5 Radius=5.0 Circumference =

Encapsulation An object instance owns its state and behavior Java provides access modifiers to define what code can access an object's state and behavior public all code can access the tagged state or behavior private only instances of the enclosing class may access this tagged state or behavior (default) package private protected

Example of public vs private: public class FT { private double rad; private double circ; private double area; public FT() { rad = 1.0; setValues(); } public FT(double r) { rad = r; setValues(); } private void setValues(){ circ = Math.PI * 2 * rad; area = Math.PI * Math.pow(rad, 2); } public void setrad(double r) { rad = r; setValues(); } public String printcirc(){ String s = "Radius: "+rad+" Cicumference: "+circ+" Area: "+area; return s; }

21 Static Variables and Methods Static variables belong to a Class  similar to global variables  Use mostly for constants  easy to create bugs and undefined behavior Static methods belong to a Class  do not have access to object state (object variables, or fields)  cannot call instance methods (because they use object variables)  often have good uses as simple functions  formulas common to all objects in a Class are often written in a static method  Utility classes – think of the java.util… that we’ve imported – has methods we want to be able to use without having the methods attached to an object.

22 Instance Variables, and Methods (variables and methods without "static") Instance variables belong to a specific instance. Instance methods are invoked by an instance of the class.

Static fields and methods public class Creature { private int currentmood = 2; private String name = "Noname"; private static int creaturecount = 0; private String[] moods = {"massively depressed","bored stiff","marginally happy", "ecstatic"}; public Creature() { creaturecount++; } public Creature(int mood) { currentmood = mood; creaturecount++; } public Creature(String Creaturename) { name = Creaturename; creaturecount++; } public Creature(int mood, String Creaturename) { currentmood = mood; name = Creaturename; creaturecount++; } public String getMood() { return (name +"'s current mood is "+moods[currentmood]); } public static String NumberinHerd(){ return ("The current number of creatures is " + creaturecount); } public class mainclass { public static void main(String[] args){ Creature Fluffy = new Creature (3); System.out.println(Creature.NumberinHerd()); System.out.println(Fluffy.getMood()); Creature Bob = new Creature(0, "Bob"); System.out.println(Creature.NumberinHerd()); System.out.println(Bob.getMood()); Creature Killer = new Creature("Killer"); System.out.println(Creature.NumberinHerd()); System.out.println(Bob.getMood()); }

Accessors and Mutators Accessors (e.g., getField) public instance methods that access private data may return different forms of the data simple convention: getSomeProperty() Mutators (e.g., setField) public instance methods that change private data may change more than one private data element simple convention: setSomeProperty(x) Why are these good ideas?

public class FT { private double rad; private double circ; private double area; public FT() { rad = 1.0; setValues(); } public FT(double r) { rad = r; setValues(); } public void setrad(double r) { // Mutator – sets (mutates) value rad = r; setValues(); } public double getrad() { //Accessor – gets (returns) value return(rad); } private void setValues(){ circ = Math.PI * 2 * rad; area = Math.PI * Math.pow(rad, 2); } public getcirc() { return(circ); }

26 The null Value If a data field of a reference type does not reference any object, the data field holds a special literal value, null. (null reference value) Example: Circle circle1 = new Circle(5.0); Circle circle2 = null; System.out.println(circle1.getrad()); // what happens here? System.out.println(circle2.perimeter()); // here? circle2 = circle1; // what happens here? System.out.println(circle2.perimeter()); // here? circle2.setrad(4.2); System.out.println(circle1.getrad()); //

27 2D Arrays one-dimensional arrays to model linear collections of elements. two-dimensional arrays represent a matrix or a table Example:

2-D Arrays Make a one-dimensional array of integers: int[] arr1d = {3,2,4,1,5}; How many elements in the array? (arr1d.length?) How do you access the element at index 3? Now make an array that consists of arrays of ints: int[][] arr2d = {{1,2,3,4},{5,6,7,8},{9,10,11,12}} ; Or (if we don’t know the values: int[][] arr2d = new int[3][4]; Can you make an array of arrays of arrays? (a 3- Dimensional array?)

29 Two-dimensional Array Illustration arr.length? arr[0].length? What data type is arr[2] ?

30 Lengths of Two-dimensional Arrays int[][] x = new int[3][4];

(Quickly) More on Null references: A null reference is a pointer to nothing. Pointers = space for address in memory Think of RAM, with each space in memory having its own unique address. (different spaces in memory do different things) Circle x; x is now a “null pointer” Meaning x can hold an address for a space in memory that is where a Circle will be, but currently it’s just empty x = new Circle(); Makes a space for a circle object in memory memory space holds radius, circumference, area fields Holds getRadius() method, SetValues() method,etc. x now holds the address of this new circle in memory. It “points to” the circle in memory

Matrices: Making Arrays of Arrays 1. double[][] mat = new double[5][]; What have I just made? an array of 5 addresses (that will eventually point to arrays of doubles). If I can’t do this: 2.double[][] mat = new double [][]? Why can I do #1?

To create an array of arrays: You can do: double[][] mat = {{3.2,4.1,2.5},{7.1,8.2,9.3}}; Or double[][] mat = new double[3][]; mat[1] = new double[] {3.1,2.4}; Or double[][] mat = new double[3][]; double[] arr = {7.2,3.1,2.4}; mat[2] = arr; Or double[][] mat; mat = new double[][] {{3.2,4.1,2.5},{7.1,8.2,9.3}};