Week3 - Classes and Methods CS 180 Sunil Prabhakar...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Classes and Methods CS 180 Sunil Prabhakar Department of Computer Science Purdue University
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Objectives Review Methods Constructors Call-by-value Overloading Private and public modifiers Scope and extent this keyword Static methods and data
Background image of page 2
User-defined Classes Create a class whenever no existing class fits our needs. Data members each objects gets its own copy Methods only methods defined for a class can be called on an object of that class ( encapsulation ). 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 class { } Template for Class Definition Import Statements Class Comment Class Name Data Members Methods (incl. Constructor)
Background image of page 4
Student 5 import javax.swing.*; /************************************/ /* Java class for a single student */ /* Author: Sunil Prabhakar */ /* Date: September 8, 2009 */ /************************************/ class Student { private String name; private String id; public Student ( String studentName ){ name = studentName; id = “”; } public void setName ( String studentName ){ name = studentName; } public String getName (){ return name; } public String getId (){ return id; } public void setId ( String studentId ){ id = studentId; } }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Constructors Special type of method. Called whenever a new object is created. Special syntax: name is same as class name; called using new ClassName(…); no return type (or return statement); If none defined, compiler adds a default one (with no parameters) 6 public Student ( String studentName ) { ... }
Background image of page 6
Call-by-Value When a method is called: temporary memory space is created for the method parameters local data Passed arguments are copied to corresponding parameters left-to-right association must be assignment-compatible pass-by-value; call-by-value method execution begins 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Call-by-Value Example class MyClass { public double myMethod ( int one, double y ) { int i=5; one += 6; i *= y; return i; } } MyClass myObj; int x, y; myObj = new MyClass () ; x = 10; y = 20; y = ( int )myObj.myMethod ( x, y ) ; System.out.println ( x + " " + y ) ;
Background image of page 8
one y 2 i Memory Allocation for Parameters class MyClass { public double myMethod ( int one, double y ){ int i=5; one += 6; i *= y; return i; } } MyClass myObj; int x, y; myObj = new MyClass () ; x = 10; y = 20; y = ( int ) myObj.myMethod ( x, y ) ; System.out.println ( x + " " + y ) ; 2 10 x y 20 1 1 10 20.0 5.0
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
class MyClass { public double myMethod ( int one, double y ){ double i=5; one += 6; i *= y; return i; } } Memory Allocation for Parameters MyClass myObj; int x, y; myObj = new MyClass () ; x = 10; y = 20; y = ( int )myObj.myMethod ( x, y ) ; System.out.println ( x + " " + y ) ; 10 x y 20 3 4 100 one y i 3 20.0 10 5.0 100.0 16
Background image of page 10
11 Objects and Methods When we pass an object, we are actually passing the reference (name) of an object it means a duplicate of an object is NOT created in the called method The return value is also similarly copied since the reference is copied, the actual object does not get destroyed!
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/26/2012 for the course STAT 350 taught by Professor Staff during the Spring '08 term at Purdue University.

Page1 / 41

Week3 - Classes and Methods CS 180 Sunil Prabhakar...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online