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

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Written by: Dr. JJ Shepherd
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Road Map Introduction to object oriented programming. Classes
Chapter 41 Defining Classes and Methods Chapter 4.
Classes and Objects Systems Programming.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
CS180 Chapter 4 2/1/08. Announcements Project 3 is out –2 weeks due to the exam Exam –Thursday, February 7, 8:30 – 9:30 pm, WTHR 104 –Covers chapters.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Understanding class definitions Looking inside classes.
Introduction to Programming with Java, for Beginners Scope.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,
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 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Written by: Dr. JJ Shepherd
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
CSH Intro. to Java. The Big Ideas in Computer Science Beyond programming Solving tough problems Creating extensible solutions Teams of “Computational.
CS180 Recitation Chapter 7: Defining Your Own Classes II.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Defining Your Own Classes II
Java Programming: Guided Learning with Early Objects
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Defining Classes and Methods
Group Status Project Status.
Defining Classes and Methods
Java Programming Language
Defining Classes and Methods
Classes CS 21a: Introduction to Computing I
Defining Classes and Methods
A simple function.
Review for Midterm 3.
Presentation transcript:

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, 10:00pm Exam 1  Wednesday, October 1, 6:30 – 7:30 pm, CL  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 operations with the class Instance variables – data held within an object 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 typeParameters 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 outside 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”); 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.

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