defining classes and methods
play

Defining classes and methods Recitation 09/(25,26)/2008 CS 180 - PowerPoint PPT Presentation

Defining classes and methods Recitation 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University Announcements Project 4 is out 2 week project Milestone Due October 1, 10:00pm Final Submission Due October 8,


  1. Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University

  2. Announcements � Project 4 is out � 2 week project � Milestone Due October 1, 10:00pm � Final Submission Due October 8, 10:00pm � Exam 1 � Wednesday, October 1, 6:30 – 7:30 pm, CL50 224 � Covers chapters 1-5 � Sample exam on course webpage. � Exam review at the study group meeting: Tuesday, September 30, 5-7 pm, LWSN B116

  3. Announcements (contd.) � Consulting hours � Students are expected to go through the project spec in detail before approaching the TAs for help. � Do not approach TAs during help hours with errors in programs unless you have tried enough to fix the problem yourself. � Recall the way to go about fixing errors: Syntax error – check the highlighted line carefully � Logic error – Use print statements to identify the problem � Runtime error – Check the line where the exception is being � thrown.

  4. Classes � Ways to organize data � Use to create objects containing data � Data can be of primitive type and objects of other classes. � Contain methods to perform certain operations � Accessors � Mutators � Java program consists of objects of class type � Objects can interact with one another � Program objects can represent objects in real world

  5. Terminology � Class – a blueprint for an object � Object – an instance of a class � Real world analogy – Your TA’s hummer is an instance of a class Car � Method/function – means to perform operations with the class � Instance variables – data held within an object of a class

  6. Methods Used to provide an interface to the class � Helper methods – break up your code into � understandable chunks Types of methods � � Void - do not return anything Return statements are optional � E.g - return; � � Methods returning a value Mandatory return statements � Usage: Name of the object followed by a dot followed � by the name of the method. E.g: keyboard.nextInt() or myCar.getCarType() �

  7. Methods - example public class Car { private int carType; Return type Parameters public int getCarType(int n) { …… return carType; //mandatory return statement } Car myCar = new Car(); ....... } int type = myCar.getCarType(5);

  8. Scope Can only use variables within the current scope � Loop/conditional scope: if a variable is declared within an if/else � block or any type of loop, it is not accessible outside the block Includes any declarations in the for loop declaration � Method scope: local variables declared within a method are not � accessible outside the method Includes argument list � Class scope: manageable with public/private modifiers � When given the choice, Java will always choose the � variable with closest scope If you have a class variable x and a local variable x declared, x � refers to the local variable, this.x refers to the class variable

  9. this keyword this is a pointer to an object’s self � Always used implicitly, but sometimes useful to be used � explicitly In case of ambiguity resulting from having a local � variable with the same name as a class variable, use this to refer to the class’s variable. class A A more clear version avoids ambiguity class A { { private int a; private int a; public int add(int a) public int add(int b) { { return a + b; return this.a + a; } } } }

  10. The public and private modifiers � Type specified as public � Any other class can directly access that variable/method by name � Classes generally specified as public � Instance variables usually specified as private � Private members not directly accessible from outside the class

  11. Mutators and accessors Mutator � Method to modify the value of a private instance variable. � public void setCarType(int carType) { this.carType = carType; } Accessor � Method to access the value of a private instance variable. � public int getCarType() { return carType; }

  12. Mutators and accessors � Note: � Obviously, mutators and accessors should be declared as public methods. � Accessor methods Usually no parameter � Return type same as that of the variable to be accessed. � � Mutator methods Usually one parameter with the new value of the � instance variable. Usually of void return type � � What about instance variables that we do not want to be accessed or changed from outside ? Do not have to write an accessor or mutator method �

  13. Variables of a Class Type � Data of primitive type stored in the memory location assigned to the variable � For class type, the object itself is not stored in the instance variable � Stored elsewhere in memory � Variable contains address of where it is stored (i.e the reference to the object) Car a = new Car(“hummer”); d Car b = new Car(“hummer”); The two variables might contain the same data, but in different locations. The value of each variable is the address of the memory location where the objects are stored – which is different. Recall that (a==b) will only compare the addresses, resulting in the value false.

  14. More on class types � Assignment operator used with objects Only memory address is copied � This creates an alias. � E.g Car a = new Car(“hummer”); � Car b = a; // Now b and a refer to the same object in the memory � Parameters of class type Memory address of actual parameter passed to formal � parameter Actual parameter thus can be changed by class � methods � Return type of a method as class type When a method returns a class object, only the � memory address is returned.

  15. Equality Comparison � For primitives, compare for equality with == � For objects, when to use ==, when to use equals() method? When using ==, you are comparing addresses � True: if they are aliases � The two variables refer to the same object in the memory � False: if they are not aliases (even if they hold the same data) � Write your own equals method for your classes. � For example, for class Car, compare if they belong to the � same make, same model, etc.

  16. Exam review � Chapter 1: Introduction to Computers and Java Object-oriented programming principles � Programming Languages and Compilers � Java Byte-Code etc. � � Chapter 2 : Basic Computation Java data types, variables � assignment statements, variables and expressions � Class String – string processing � Keyboard and Screen I/O �

  17. Exam review � Chapter 3: Flow of Control: Branching branching statements - Boolean type and expressions � If-else and switch statements � Enumerations � � Chapter 4 : Flow of Control: Loops Use while , do , and for in a program � Use the for-each with enumerations �

  18. Exam review � Chapter 5: Defining classes and methods � Define a Java class, its methods � Describe use of parameters in a method � Use modifiers public , private � Define accessor, mutator class methods

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend