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 - - 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,
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
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.
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
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
- perations with the class
Instance variables – data held within an
- bject of a class
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()
Methods - example
public class Car { private int carType; public int getCarType(int n) { …… return carType;
//mandatory return statement
} }
Return type Parameters
Car myCar = new Car(); ....... int type = myCar.getCarType(5);
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
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 { private int a; public int add(int a) { return this.a + a; } } A more clear version avoids ambiguity class A { private int a; public int add(int b) { return a + b; } }
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
- utside the class
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; }
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
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”);
dCar 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.
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.
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.
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
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
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