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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Road Map Introduction to object oriented programming. Classes
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
The different types of variables in a Java program.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Saravanan.G.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
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.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Primitive Variables.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
Objects and Classes Mostafa Abdallah
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.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Department of Computer Engineering Methods Computer Programming for International Engineers.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Methods.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Topic: Classes and Objects
Chapter 7 User-Defined Methods.
Yanal Alahmad Java Workshop Yanal Alahmad
University of Central Florida COP 3330 Object Oriented Programming
Road Map Introduction to object oriented programming. Classes
Chapter 4 Procedural Methods.
Class Constructor Recall: we can give default values to instance variables of a class The “numberOfPlayers” variable from the “PlayerData” class before.
IDENTIFIERS CSC 111.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 6 Methods: A Deeper Look
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Variables ICS2O.
More on Classes and Objects
Chapter 2: Java Fundamentals
Classes Lecture 7 from Chapter /1/11.
Chapter 7 Procedural Methods.
elementary programming
Object Oriented Programming in java
In this class, we will cover:
Chapter 7 Objects and Classes
Presentation transcript:

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 of the class must be declared. NULL Value

3 Constructor... A class contains a special kind of method called constructor whose name is as same as the class name and no any return type. class Student { Student()‏ { } class Student { Student()‏ { } } Since This constructor does not take any parameters, it is called as the “default constructor”

4 Example 1 : (Cont)... class Student { int age; char className; float average; char grade; Student()‏ { } class Student { int age; char className; float average; char grade; Student()‏ { } class Example { public static void main(String arg[])‏ { Student stu=new Student(); } class Example { public static void main(String arg[])‏ { Student stu=new Student(); }

5 Example 2... Write a JAVA program to store the information of two students. Each student have a Age, Gender, Marks for mathematics, Marks for statistics, Marks for computer science and average of three subjects. AttributeStudent 1Student 2 Age 2123 GenderFM Marks for Mathematics6789 Marks for Statistics7654 Marks for Computer Science7895

6 Example 2 (cont)... Step 1: Declare a class to represent student information. class Student { int age; char gender; int marks_maths; int marks_stat; int marks_cs; float average; } class Student { int age; char gender; int marks_maths; int marks_stat; int marks_cs; float average; }

7 Example 2 (cont)... Step 2: Define the driver class and the main method. class Example2 { public static void main() { } class Example2 { public static void main() { }

8 Example 2 (cont)... Step 3: Create objects from Student class. Since there are two students, we need two objects. class Example2 { public static void main(String arg[]) { Student stu1=new Student(); Student stu2=new Student(); } class Example2 { public static void main(String arg[]) { Student stu1=new Student(); Student stu2=new Student(); }

9 Example 2 (Cont)... stu1 stu2 RAM Student Methods age: 32 bits marks_maths: 32 bits marks_stat: 32 bits marks_cs: 32 bits gender: 16 bits average: 32 bits Student Methods age: 32 bits marks_maths: 32 bits marks_stat: 32 bits marks_cs: 32 bits gender: 16 bits average: 32 bits

10 Example 2 (cont)... Step 4: Assign the values for attributes. class Example2 { public static void main() { Student stu1=new Student(); Student stu2=new Student(); stu1.age=21; stu2.age=23; } class Example2 { public static void main() { Student stu1=new Student(); Student stu2=new Student(); stu1.age=21; stu2.age=23; }

11 Example 2 (Cont)... stu1 stu2 RAM Student Methods age: 21 marks_maths: 67 marks_stat: 76 marks_cs: 78 gender: F average: 0.0 Student Methods age: 23 marks_maths: 89 marks_stat: 54 marks_cs: 95 gender: M average: 0.0

12 Example 2 (cont)... Print the information of each student.

13 Example 2 (cont)... class Example2 { public static void main() { int tot=stu1.marks_math+stu1.marks_stat+stu1.marks_cs; stu1.average=(float)tot/3; } class Example2 { public static void main() { int tot=stu1.marks_math+stu1.marks_stat+stu1.marks_cs; stu1.average=(float)tot/3; } Calculate the average of three subjects for each student.

14 Example 2 (cont)... NOTE: The attributes agegendermarks_math marks_stat marks_csavg are called as instance variables. The common characteristic mentioned above take different values for each student. Hence they are declared in the class, but outside the methods.

15 Example 2 (cont)... Calculate the class average of three subjects. If the same value of an attribute is shared by all the class instances are named as class variables and are defined as ‘Static’. They are declared in the classes, but outside the methods. Class average for one subject does not take different values from one student to another. There is only one value for all the objects created from the same class.

10/13/1016 Example 2 (cont)... Modify the Student class in order to store the class averages for three subjects. class Student { int age; char gender; int marks_maths, marks_stat,marks_cs; float average; static float classAvg_maths; static float classAvg_stat; static float classAvg_cs; } class Student { int age; char gender; int marks_maths, marks_stat,marks_cs; float average; static float classAvg_maths; static float classAvg_stat; static float classAvg_cs; }

17 Example 2 (cont)... class Example2 { public static void main() { float avg =(float)(stu1.marks_maths+stu2.marks_maths)/2; Student.classAvg_maths=avg; } class Example2 { public static void main() { float avg =(float)(stu1.marks_maths+stu2.marks_maths)/2; Student.classAvg_maths=avg; } Since the class variables are shared by all the instances of the class in which they are defined, they can be accessed directly through the identifier of the class.

18 Summary... Student 1 Methods age: 21 marks_cs: 78 gender: F average: 0.0 Student 2 Methods age: 23 marks_cs: 95 gender: M average: 0.0 Student Methods class_average_maths = 78

03/09/1019

20 Methods... Methods are the functions that operate on instances of classes in which they are defined. Objects can communicate with each other using methods. One class may contain one or more methods.

21 Method Definition... Method definition has four parts. Name of the method Return type List of parameters Body of the method Method Signature

22 Method Definition (cont)... return_type method_name (parameter list)‏ { Method_body } return_type method_name (parameter list)‏ { Method_body } Method Signature

23 Naming Conventions... Method names begin with lower case letters. Do not start with numbers or symbols #,%, etc). If the method name contains more than one word, the starting letter of the second word is capital or they are combined using the underscore (_). The symbols #, *, %, etc are not included in the method names. Do not leave spaces in between the words. Always use the meaningful names.

24 Return type... The return type is the primitive data type or object that a method returns. If the method does not return any value, then the return type is void. If the return type is not void, then the returning value should have the same type as the return type.

25 Parameter List... The parameter list is a set of variable declarations. The parameters are separated by commas,. The parameters become local variables in the body of the method whose values are passed when the method is called. void calculate(int num, float avg)‏ { } void calculate(int num, float avg)‏ { }

03/09/1026 Method body... The operations performed by a particular method, should be included in the method body. Method body contains, - Expressions - Methods calling statements - Control structures - Creation of objects NOTE: If the returning type of the method is not void, then the method should return a value. return num;

27 Method types... There are two major types of methods Instance Methods (non- static methods)‏ Class methods (Static methods)‏ Class methods (Static methods)‏

28 Instance Methods... Instance methods apply and operate on the instances of the class. Access Modifiers can be used for instance methods. Instance methods can access all the instance variables and class variables declared in the same class that the method defines. An Instance method can declare any number of local variables which are accessible only by the method in which they defines.

29 Call instance methods... Instance methods are accessed using the dot operator. object_reference Dot Operator method_name (parameter list)‏

30 Class/Static Methods... Class methods operate on the classes and they are defined using the keyword ‘static’. Access Modifiers can be used for class methods. class methods can access only the class variables declared in the same class that the method defines. They are not allowed to access the instance variables.

03/09/1031 Call class/static methods... Class methods are accessed using the dot operator and the name of the class which defines the method. Class_name Dot Operator method_name (parameter list)‏ NOTE: main method is also a class method

32 Example 3... Write a JAVA program to define a calculator. The calculator should be able to store two numbers and perform following operations on them. 1. Add 2. Subtract 3. Multiply 4. Divide Example 4... Write a JAVA program to calculate the surface area and the volume of a cube. You must define separate operations to calculate the surface area and the volume.

33 this keyword... ‘this’ keyword is used inside any instance method to refer to the current object which the current method has been called. ‘this’ keyword is important where a reference to an object of the current class type is required. Methods declared with static (class methods) can not use ‘this’ keyword

34 this keyword (cont)... class Student { int tot; void increment()‏ { this.tot=100; } class Student { int tot; void increment()‏ { this.tot=100; } class Test { public static void main(String arg[])‏ { Student st=new Student(); st.increment(); } class Test { public static void main(String arg[])‏ { Student st=new Student(); st.increment(); } The object ‘st’ calls the instance method ‘increment()’. Hence, ‘this’ refers to the object ‘st’.

35 Method overloading... JAVA allows to have more than one method with the same name, but different method signatures. This difference occurs by means of the parameter list, but not by return type. The differences between the parameter lists are obtained based on the number or data types of the parameters passes to the methods.

36 Example 5... class Example5 { void display(int num)‏ { System.out.println(num); } void display(float num)‏ { System.out.println(num); } class Example5 { void display(int num)‏ { System.out.println(num); } void display(float num)‏ { System.out.println(num); } Overloaded methods

37 Example 6... Write a JAVA program to calculate the average of numbers based on following options. Average of two integer numbers Average of three integer numbers Average of an integer number and a float number Average of two integer numbers and a float number Average of three float numbers Average of two float numbers and two integer values Average of two double values Average of three numbers of type integer, double and float You must use overloaded methods to perform the required operations.

03/09/1038 Constructor overloading... As same as the other methods, constructors can also be overloaded, with the same name (name of the class), but different parameter lists. Overloaded constructors enable creation of the objects with required properties. class Student { int age; Student()‏ { } Student(int age)‏ { this.age=age; } class Student { int age; Student()‏ { } Student(int age)‏ { this.age=age; } Overloaded Constructors

39 Calling overloaded Constructors... class Example { public static void main(String arg[])‏ { Student st=new Student(23); } class Example { public static void main(String arg[])‏ { Student st=new Student(23); } The required parameters are passed when the constructor is called.

03/09/1040 Calling another constructor... A constructor can call another constructor using ‘this’ keyword. class Student { int age; Student()‏ { System.out.println(“Welcome to Student class”); } Student(int age)‏ { this(); this.age=age; } class Student { int age; Student()‏ { System.out.println(“Welcome to Student class”); } Student(int age)‏ { this(); this.age=age; } The calling statement of another constructor should be the first statement of current constructor