JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

Slides:



Advertisements
Similar presentations
Web Application Development Slides Credit Umair Javed LUMS.
Advertisements

C++ Classes & Data Abstraction
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Written by: Dr. JJ Shepherd
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Road Map Introduction to object oriented programming. Classes
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
1. 2 Introduction to Methods  Method Calls  Parameters  Return Type  Method Overloading  Accessor & Mutator Methods  Student Class: Revisited.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Inheritance #1 First questions Similar to Python? What about visibility and encapsulation? – can an object of the child class access private members.
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.
UML Basics & Access Modifier
Chapter 4 Objects and Classes.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Introduction to Java Classes and Objects. What is a class A class is description of a structure that contains both data and methods – Describes a set.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Classes, Interfaces and Packages
Written by: Dr. JJ Shepherd
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Topic: Classes and Objects
GC211 Data structure Lecture 3 Sara Alhajjam.
Examples of Classes & Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Creating Your OwnClasses
CS 302 Week 11 Jim Williams, PhD.
Classes In C#.
Classes & Objects: Examples
Implementing Classes Chapter 3.
Assignment 7 User Defined Classes Part 2
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
JAVA CLASSES.
Object-Oriented Programming
CIS 199 Final Review.
Dr. R Z Khan Handout-3 Classes
Chapter 7 Objects and Classes
Presentation transcript:

JAVA PROGRAMMING PART III

METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])

METHOD STATEMENT is option Public : other class Private : same class Protected : same package or the extended class in other package Default : same package [static] is option Called from static method in the same class Called from method in different class by class name the data type that return from method Begin with small letter and follow () is option

ABSTRACT DATA TYPE User define data type as array and structure in imperative is not secure as simple type. How properties of data type to be more secure should be is the interesting topic. How and where to store data is not necessary to know when we used data. Don’t allow to access the data directly. It can access data used some operations. Data type have all above properties is “abstract data types”

Object Oriented Programming Encapsulation Information hiding Inheritance Dynamic binding

Abstract Data Type #include int count(int i){ return i++; } void main (){ int c = 0; c = count(c); } public class Counter{ private int c; private Counter(){ c=0;} public void reset(){ c=0;} public void count(){ c++;} public void show(){ System.out.println(“value:”+c); } public static void main(String args[]){ counter c1 = new counter(); c1.show; }

Consider this program class Counter{ private int c; private counter(){ c=0;} public void reset(){ c=0;} public void count(){ c++;} public void show(){ System.out.println(“value:”+c); } public class MyProgram{ public static void main(String args[]){ counter c1 = new counter(); c1.show(); c1.count(); c1.show(); }

Objects Tied 2 things together Data (Attributes) Method (Behaviors) There are 2 properties Encapsulation Information hiding

Classes and Instances Classes is definition of object. They are consisted of data (attribute) and method (behavior) Objects is something that have the attribute and behavior as define in class.

Classes and Instances class Counter{ private int c; private counter(){ c=0; } public void reset(){ c=0; } public void count(){ c++; } public void show(){ System.out.println(“value:”+c); } public class MyProgram{ public static void main(String args[]){ new counter(); System.out.println(“Counter is created”); } 1 2

Instance and Reference Variable Reference Variable used to point to instance of class for refer any member and method in the class. Counter Instance c Counter() Reset() Count() Show() mycounter

Classes and Instances class Counter{ private int c; private counter(){ c=0; } public void reset(){ c=0; } public void count(){ c++; } public void show(){ System.out.println(“value:”+c); } public class MyProgram{ public static void main(String args[]){ counter mycounter; new counter(); mycounter = new counter(); mycounter.show(); mycounter.count(); mycounter.show(); } 1 2

Try to design class of vector Member (x,y) Method Construct Method add Method subtract Method conjugate

CLASS ClassName attributes method() Counter data Counter() reset() count() show() Example

CLASS Definition CLASS define as format class [extends ] may be public private protected if empty mean default is identifier; [extends ] identify the parent-class if there is

Attribute and Method Definition Attribute define as format Method define as format ; [ ] [static] ( [ ]) { }

Example No1 //Sawasdee.java class Sawasdee{ String data=‘Sawasdee ’; public show(String s){ System.out.println(data+s); }

Example No2 class Student { public int id; private String name; private double gpa; public Student() { id = 0; name =“”; gpa = 0.0} public void setName(String n) {name = n;} public String getName() { return name;} public void SetGpa(double g) { gpa = g;} public double getGpa() { return gpa;} }

Instance of Class Define as format = new Example Counter c1 = new Counter(); Student x = new Student();

Instance of Class public class StudentInstance{ public static void main(String args[]) { Student x = new Student(); x.id = 123; // x.name = “Herry Potter”; x.setName(“Herry Potter”); x.setGpa(4.00); System.out.println(x.id+”,”+x.getName()); System.out.println(x.id+”,”+x.getGpa()); }

Method Construtors to initial attribute of class Accessors for read Mutators for write public Student() { id = 0; name =“”; gpa = 0.0} public String getName() { return name;} public double getGpa() { return gpa;} public void setName(String n) {name = n;} public void setGpa(double g) { gpa = g;}

Constructor Method Name same as class Can be more than one, overload constructors class Student { public int id; private String name; private double gpa; public Student() { id = 0; name = null; gpa = 0.0;} public Student(int I, String n) {id = i; name = n;} public Student(Student s) {id = s.id; name = s.name;} public void setName(String n) {name = n;} public String getName() { return name;} public void SetGpa(double g) { gpa = g;} public double getGpa() { return gpa;} }

Test Constructor Method public class StudentOverCons{ public static void main(String args[]) { Student x = new Student(); Student y = new Student(123,”John Rambo”); Student z = new Student(y); }

This References Used for reference the instance of class, but when class is defined, nobody know the instance will be what its name is. Class Complex { private double r,i; Complex(double r, double i) { this.r = r; this.i = i} public void add(Complex c) { this.r += c.r; this.i += c.i; }

This Constructor Used for reference constructor of class. Java allow used this constructor in constructor method only and in the first line command. Used only one time in method

This Constructor Class Complex { private double r,i; Complex(){this(0.0,0.0);} Complex(double r, double i) { this.r = r; this.i = i} Complex(Complex c) { this(c.r.c.i);} public void add(Complex c){ this.r += c.r; this.i += c.i; } public void print() { system.out.println(this.r+”+i”+this.i); }

Variables and Instances Memory allocation Variable do both allocate memory and reference Instance create reference firstly, then allocate memory. Assignment Variable assign by copy value Instance assign by copy reference

Assignment y x y x 356

Copy value Class VarAssign{ public static void main(String args[]){ int x=1;y=2; x = y; System.out.println(x+”,”+y); y = 3; System.out.println(x+”,”+y); }

Copy reference Class InsAssign{ public static void main(String args[]){ Student x = new Student(123,”Herry Potter”); Student y = new Student(456,”Ron Wisly”); x = y; System.out.println(x.getId()+”,”+x.getName()); System.out.println(y.getId()+”,”+y.getName()); y.setName(“Load Vodermor”); System.out.println(x.getId()+”,”+x.getName()); System.out.println(y.getId()+”,”+y.getName()); }

Lifetime of Instances