You are on page 1of 37

Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf.com/' to remove this watermark.

Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf.com/' to remove this watermark. Demo Watermark

Classes and Methods

Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf.com/' to remove this watermark.

Data Field radius = 5

Classes

A Circle object

Method findArea

data field 1

data field n

method 1

Behavior method n ...

Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf.com/' to remove this watermark. Demo Watermark

An object

...

State

Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf.com/' to remove this watermark.

– Variables to store data items – Differentiate multiple objects of a class – They are called data members

• Data Fields

Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf.com/' to remove this watermark. Demo Watermark

• Methods

– Operations of a class – They operate on variables of the class

Class Definition

eepdf.com/' to remove this watermark.. Demo Watermark radius Circle Graphical notation for objects .eepdf.com/' to remove this watermark.. radius = 5 findArea Example Please purchase 'eePDF Office to PDF Converter' on 'http://www. Graphical notation for classes new Circle() circlen: Circle circle1: Circle new Circle() radius = 2 .Please purchase 'eePDF Office to PDF Converter' on 'http://www.

Please purchase 'eePDF Office to PDF Converter' on 'http://www. Demo Watermark • Class variables • Instance variables .com/' to remove this watermark.eepdf.com/' to remove this watermark. Fields in Class Definition Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf.

eepdf. it is reflected for all objects.com/' to remove this watermark. Fields in Class Definition • Class Variable – Only one copy is shared with all the objects of the class – Shared among all objects of a class – Exists even if no object has been created – Also called as static fields – Declared using the keyword static – If value is changed. Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www.Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf. .com/' to remove this watermark.

com/' to remove this watermark. address etc. Please purchase 'eePDF Office to PDF Converter' on 'http://www. name .Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf. Instance variables .eepdf. Demo Watermark • Variables associated with each object • A separate value for each instance of a class • For example.com/' to remove this watermark.

double radius. Demo Watermark // instance variables double xCenter.com/' to remove this watermark.eepdf. Please purchase 'eePDF Office to PDF Converter' on 'http://www. double zCenter.com/' to remove this watermark. } .eepdf.Please purchase 'eePDF Office to PDF Converter' on 'http://www. double yCenter. Example Class Sphere Definition public class Sphere { // class variable static double PI=3.14.

com/' to remove this watermark. Demo Watermark • Instance Methods • Class Methods .eepdf.Please purchase 'eePDF Office to PDF Converter' on 'http://www. Methods in Class Definition Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf.com/' to remove this watermark.

eepdf.eepdf.Please purchase 'eePDF Office to PDF Converter' on 'http://www. • Static methods • Can not refer to an instance variable • Example: main method • Builtin methods in standard class Math . Class Methods • Execute class methods even when no objects of a class exist • Declared using the keyword static. Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www.com/' to remove this watermark.com/' to remove this watermark.

Instance Methods Please purchase 'eePDF Office to PDF Converter' on 'http://www.com/' to remove this watermark. Demo Watermark • Can execute when objects exist.eepdf.com/' to remove this watermark. • Example: calculate area() .Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf.

volume(). – Objects can also access in the same way Please purchase 'eePDF Office to PDF Converter' on 'http://www.sqrt(Math.com/' to remove this watermark.Please purchase 'eePDF Office to PDF Converter' on 'http://www. Demo Watermark • Instance variables and methods • Accessing Class Methods – double ballVolume = ball. .eepdf.PI). Accessing Variables and Methods – double rootPi = Math.eepdf.com/' to remove this watermark.

//class variable with fixed value static int count = 0.Please purchase 'eePDF Office to PDF Converter' on 'http://www.com/' to remove this watermark. // Class variable to count objects // Instance variables double radius. // 3D coordinates double yCenter. Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www. // of a sphere // Plus the rest of the class definition.eepdf. } .eepdf.com/' to remove this watermark. // of the center double zCenter.14... // Radius of a sphere double xCenter. Defining Classes class Sphere { static final double PI = 3.

Please purchase 'eePDF Office to PDF Converter' on 'http://www.com/' to remove this watermark.eepdf.eepdf. Demo Watermark • Self contained block of codes • Reusable • Can be executed from anywhere in the program • Calling a method Defining Methods . Please purchase 'eePDF Office to PDF Converter' on 'http://www.com/' to remove this watermark.

Basic Structure of a method Please purchase 'eePDF Office to PDF Converter' on 'http://www.com/' to remove this watermark.eepdf. Demo Watermark return_type methodName( arg1. . arg2... // To return a value from a method .Please purchase 'eePDF Office to PDF Converter' on 'http://www..eepdf. argn ) { code here } • Return Statement return return_value.com/' to remove this watermark.

else result = num2.eepdf. Demo Watermark } return value .eepdf. int num2) { int result = 0.Please purchase 'eePDF Office to PDF Converter' on 'http://www. if (num1 > num2) result = num1.com/' to remove this watermark.com/' to remove this watermark. Example returnValueType methodName parameters return result. public static int max(int num1. method heading method body modifier Please purchase 'eePDF Office to PDF Converter' on 'http://www.

Parameters and Arguments • Parameter – has a name and a type – Appear in definition of the method – Defines type of value to be passed to method when called • Arguments – Actual value that is passed to a method when executed – Must be consistent with the type specified in parameter definition .com/' to remove this watermark.Please purchase 'eePDF Office to PDF Converter' on 'http://www. Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf.eepdf.com/' to remove this watermark.

0.eepdf.eepdf. Please purchase 'eePDF Office to PDF Converter' on 'http://www.mean( 3.com/' to remove this watermark. x = obj. Demo Watermark public static void main(String[] args){ .. } double mean( double value1 ..Please purchase 'eePDF Office to PDF Converter' on 'http://www.0 . return result. 5.0 ).com/' to remove this watermark. double value2 ){ double result = ( value1 + value2 )/ 2. } Example .

changeMe(d).com/' to remove this watermark.eepdf. System. Please purchase 'eePDF Office to PDF Converter' on 'http://www.0. Passed by Value .0.eepdf. } double d = 2.out.Please purchase 'eePDF Office to PDF Converter' on 'http://www. Demo Watermark public void changeMe(double d) { //this has no effect on d outside of this method! d = 345.println(d).com/' to remove this watermark.

. // Return current object count } } Class Method Definition .eepdf. Demo Watermark class Sphere { // Class definition as before.eepdf. Please purchase 'eePDF Office to PDF Converter' on 'http://www.com/' to remove this watermark.com/' to remove this watermark. // Static method to report the number of objects created static int getCount() { return count..Please purchase 'eePDF Office to PDF Converter' on 'http://www.

// Return current object count } // Instance method to calculate volume double volume() { return 4. } // Plus the rest of the class definition. Example Class class Sphere { static final double PI = 3. // Class variable that has a fixed value static int count = 0.com/' to remove this watermark.Please purchase 'eePDF Office to PDF Converter' on 'http://www. // of the center double zCenter.eepdf. } . Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www. double xCenter. // of a sphere // Static method to report the number of objects created static int getCount(){ return count. // 3D coordinates double yCenter.eepdf.0/3. // Class variable to count objects // Instance variables double radius.14.0*PI*radius*radius*radius..com/' to remove this watermark..

static int count = 0. double xCenter = 10. Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www. class Sphere { static final double PI = 3.com/' to remove this watermark. } // Plus the rest of the class definition.eepdf.0.out.0/3. } .Please purchase 'eePDF Office to PDF Converter' on 'http://www.com/' to remove this watermark. volume=s1. System.0*PI*radius*radius*radius. count=Sphere.0.volume(). public static void main (String[] args){ int count.0. System. double yCenter = 10.0.println(volume). // Static method to report the number of objects created static int getCount(){ return count. Sphere s1=new Sphere().14.. double volume.println(count).getCount().out.eepdf.. // Return current object count } // Instance method to calculate volume double volume() { return 4. // Instance variables double radius = 5. double zCenter = 10.

Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf.com/' to remove this watermark. } .Please purchase 'eePDF Office to PDF Converter' on 'http://www.radius = radius.com/' to remove this watermark.eepdf. The Variable this • Every instance method has a variable with the name this that refers to the current object for which the method is being called void changeRadius(double radius) { // Change the instance variable to the argument value this.

// of a sphere // Rest of the class.0. // Class variable to count objects // Instance variables double radius = 5.0. } . // 3D coordinates double yCenter = 10.14. // Class variable that has a fixed value static int count = 0.. // of the center double zCenter = 10. Initializing data members: The ordinary way class Sphere { static final double PI = 3. Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf.eepdf. // Radius of a sphere double xCenter = 10..0.com/' to remove this watermark.com/' to remove this watermark.Please purchase 'eePDF Office to PDF Converter' on 'http://www.0.

eepdf.Please purchase 'eePDF Office to PDF Converter' on 'http://www.com/' to remove this watermark. • There are two kinds of initialization blocks: – static initialization block – non-static initialization block Please purchase 'eePDF Office to PDF Converter' on 'http://www. Demo Watermark Using Initialization Blocks .eepdf.com/' to remove this watermark.

eepdf. Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www.com/' to remove this watermark. • Here is an example: static { // whatever code is needed for initialization goes here } .com/' to remove this watermark. static initialization block • A static initialization block is a block defined using the keyword static and is executed once when the class is loaded. • A static initialization block can initialize only static data members of the class. and preceded by the static keyword. { }.eepdf. • A static initialization block is a normal block of code enclosed in braces.Please purchase 'eePDF Office to PDF Converter' on 'http://www.

random()). example = new TryInitialization(). System.com/' to remove this watermark. }} static initialization block Running initialization block. // Display values } System.println("\nFirst object:"). i<values. for(int i=0.println().out. example. static { System. // Start a new line } public static void main(String[] args) { TryInitialization example = new TryInitialization().length. example.print(" " + value). Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www.println("Running initialization block.").listValues().out.println(). // Start a new line for(int value : values) { System.0*Math.} } // List values in the array for an object void listValues() { System. i++) { values[i] = (int)(100. First object: 40 97 88 63 58 48 84 5 32 67 Second object: 40 97 88 63 58 48 84 5 32 67 .listValues().println("\nSecond object:"). class TryInitialization { static int[] values = new int[10].eepdf.Please purchase 'eePDF Office to PDF Converter' on 'http://www.out.out.out. System.eepdf.com/' to remove this watermark.out.

eepdf. First object: 66 17 98 59 99 18 40 96 40 21 Running initialization block. – This block appears without the static keyword. Non-static initialization block – non-static initialization block is executed for each object that is created and thus can initialize instance variables in a class. Second object: 57 86 79 31 75 99 51 5 31 44 .Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf.com/' to remove this watermark. { // whatever code is needed for initialization goes here } Running initialization block.com/' to remove this watermark. Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www.

Please purchase 'eePDF Office to PDF Converter' on 'http://www. • Examine its output.eepdf.eepdf. LAB PRACTICE .com/' to remove this watermark.com/' to remove this watermark.Please purchase 'eePDF Office to PDF Converter' on 'http://www. Demo Watermark • Compile & execute “TryInitialization” class with static initialization and non-static initialization block.

speedUp(int) . Add methods like changeGear(int). 1.com/' to remove this watermark. LAB PRACTICE • • Make a class Bicycle. printStates() etc.com/' to remove this watermark. 5. Change speed Change gear Print states Apply brakes Print states .eepdf. Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www. 2. Make a main method to create 2 bicycle objects and call functions of the class in the following sequence. identify its data members for example “speed=0”. 3. 4. “gear=1” etc. applyBrakes().Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf.

out. void changeGear(int newValue) { gear = newValue. } void printStates() { System.com/' to remove this watermark.eepdf. } void applyBrakes() { speed = 0. Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www. int gear = 1.println(" speed:"+speed+" gear:"+gear). } } Solution .Please purchase 'eePDF Office to PDF Converter' on 'http://www. } void speedUp(int increment) { speed = speed + increment.com/' to remove this watermark.eepdf. class Bicycle { int speed = 0.

bike1.changeGear(3).printStates(). bike1.com/' to remove this watermark. bike1.printStates(). Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf. bike2.speedUp(10). bike2.changeGear(2). class BicycleDemo { public static void main(String[] args) { // Create two different Bicycle objects Bicycle bike1 = new Bicycle().speedUp(10).Please purchase 'eePDF Office to PDF Converter' on 'http://www. bike1. bike2.com/' to remove this watermark.applyBrakes(). Bicycle bike2 = new Bicycle().speedUp(10). bike2. bike2.eepdf. // Invoke methods on those objects bike1.changeGear(2). } } Solution .printStates().

and gear for the two bicycles: speed:10 gear:2 speed:20 gear:3 .com/' to remove this watermark. Solution Please purchase 'eePDF Office to PDF Converter' on 'http://www. Demo Watermark The output of this test prints speed.eepdf.Please purchase 'eePDF Office to PDF Converter' on 'http://www.com/' to remove this watermark.eepdf.

com/' to remove this watermark. height.com/' to remove this watermark. .(dimensions are width. The class should have a method that can return volume.eepdf. LAB PRACTICE 1. 2. Create a class Box that uses a parameterized method to initialize the dimensions of a box.Please purchase 'eePDF Office to PDF Converter' on 'http://www. Obtain an object and print the corresponding volume in main() function.eepdf. 3. depth of double type). Demo Watermark Please purchase 'eePDF Office to PDF Converter' on 'http://www.

com/' to remove this watermark.Please purchase 'eePDF Office to PDF Converter' on 'http://www.com/' to remove this watermark.eepdf. Demo Watermark Solution .eepdf. Please purchase 'eePDF Office to PDF Converter' on 'http://www.

com/' to remove this watermark.eepdf.com/' to remove this watermark.Please purchase 'eePDF Office to PDF Converter' on 'http://www.eepdf. Demo Watermark Solution . Please purchase 'eePDF Office to PDF Converter' on 'http://www.

0 Volume is 162.eepdf.Please purchase 'eePDF Office to PDF Converter' on 'http://www. Demo Watermark Volume is 3000.0 .eepdf.com/' to remove this watermark.com/' to remove this watermark. Output Please purchase 'eePDF Office to PDF Converter' on 'http://www.