Recitation 02/6/2009 CS 180 Department of Computer Science, Purdue University.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Advertisements

Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Selection Statements & Exam 1 Review Recitation – 2/13/2009 CS 180 Department of Computer Science, Purdue University.
Access to Names Namespaces, Scopes, Access privileges.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Arrays Recitation – 10/(9,10)/2008 CS 180 Department of Computer Science, Purdue University.
Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.
Classes and Instances. Introduction Classes, Objects, Methods and Instance Variables Declaring a Class with a Method and Instantiating an Object of a.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes Part 1.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Chapter 7: User-Defined Methods
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
COMP More About Classes Yi Hong May 22, 2015.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Writing Classes (Chapter 4)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Classes - Intermediate
01/24/2005 Introduction to Programming with Java, for Beginners A Closer Look at Constructors and Methods.
Comp1004: Building Better Objects II Encapsulation and Constructors.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Defining Your Own Classes II
Information and Computer Sciences University of Hawaii, Manoa
Andrew(amwallis) Classes!
Chapter 7 User-Defined Methods.
Building Java Programs
Anatomy of a class Part I
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
Defining Your Own Classes
Classes & Objects: Examples
Group Status Project Status.
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Namespaces, Scopes, Access privileges
Anatomy of a Java Program
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Defining Classes and Methods
Classes, Objects and Methods
Building Java Programs
Methods/Functions.
Corresponds with Chapter 5
ITE “A” GROUP 2 ENCAPSULATION.
Anatomy of a class Part I
CSG2H3 Object Oriented Programming
Presentation transcript:

Recitation 02/6/2009 CS 180 Department of Computer Science, Purdue University

2 Announcements & Reminders Project 1 grades out  Solution up & test cases on the Web Project 2 was due on Wednesday Project 3 is out Mentoring program w/ Debbie will be in LWSN B131 on Tuesdays Exam 1 is Feb. 18th (Less than a couple of weeks. Yikes! Better Start Studying!)  Expect 3 programming questions and multiple choice questions points

Conventional Class Definition Structure Why Are Conventions Useful??

4 What Your Class Would Like import java.util.Date; /** * Book -- a book that knows only its name * Henry David Thoreau **/ class Book { private String name; private Date dateMade; public Book( ) { name = “I have no name”; dateMade = new Date(); } public String getName( ) { return name; } public void setName(String newName) { name = newName; } Import Statement Comments Class Name Data Member Constructor Methods

5 CookBook.java class CookBook { private int numRecipes; private String name; //Constructor(s) //Methods (e.g., getter & setter) public static void main(String[] args){ CookBook cookBook1; cookBook1.setName(“Cooking!”); System.out.println(cookBook1.getName()); cookBook1.setName(“Cooking part Deux!”); System.out.println(cookBook1.getName()); } ColorBook.java class ColorBook { private int numImages; private String name; //Constructors(s) //Methods (e.g., getter & setter) public static void main( String[] args ) { ColorBook colorBook1; colorBook1 = new ColorBook( ); colorBook1.setName(“CB1”); colorBook1.setNumImages(35); ColorBook colorBook2 = new ColorBook(); colorBook2.setName(“CB2”); System.out.println(colorBook1.getName()); System.out.println(colorBook2.getName()); } Class Definition and Object Usage Convention Why is it useful for each class to have its own main method? In what order would you develop these classes?

6 More On The Main Method... You can use the “java ” only if.java has a main method “java ” runs only the main method that exists in.java

7 Access Modifiers CookBook.java class CookBook { private int numRecipes; private String name; //Constructor(s) public int getNumRecipes(){ return numRecipes; } public void setNumRecipes(int num){ numRecipes = num; } //Rest of Methods public static void main(String[] args){ //Statements } Why? What’s “static” about?

8 Constructor class CookBook { private int numRecipes; private String name; public CookBook (){ numRecipes = 0; name = “Joe Blog”; } public CookBook(String newName){ name = newName; numRecipes = 0; } public CookBook(String newName, int num){ numRecipes = num; name = newName; } //Rest of Methods } Defining even ONE Constructor precludes you from getting the default Constructor Why have multiple Constructors ?

9 Passing by Reference vs. Passing By Value CarDealer.java class CarDealer { private static Car lastCar; public static void lastCarSold(Car lCar){ lastCar = lCar; } public static void main( String [] arg ) { Car c1 = new Car(“Honda”); c1.setOwner(“Jonny B. Quick”); lastCarSold(c1); c1.setOwner(“Jonny’s Mama”); System.out.println(lastCar.getOwner); } class objects are transferred as references when they are passed as parameters to a method. In contrast, basic data types like int and double are passed by value. Program output : Jonny’s Mama Note: Car class defined in another file in the same directory

10 Defining Class Constants class BookStore{ private static final int zipCode = 47906; private final String name = “Jays”; //rest of class } Why is this bad?

11 Calling Methods class Lion { public void eatYou( ) {System.exit(0);} public void finishingMove( ) { eatYou(); } Class Jungle{ public void welcome( ) {System.out.println(“Welcome!”);} public void wildLife( ) { Lion l1 = new Lion( ); welcome(); l1.eatYou( ); } When you call a method that’s within the same class, you can call the method by just using its name. If you call a method that is in a different class, then you must refer to that method using a. (dot) notation that first references the separate class object.

Identifier Types Identifiers can be declared almost anywhere in a program. There are three main types of declarations:  Data members of a class Declared outside any method Usually at the beginning of the class definition  As formal parameters of a method  Within a method -- local variables

Sample Matching

Notice how one can hide data members by declaring a local variable with the same name

Things to Remember A local variable can be declared just about anywhere!  Its scope (the area of code from where it is visible) is limited to the enclosing braces. Statements within a pair of braces are called a block. Local variables are destroyed when the block finishes execution. Data members of a class are declared outside any method. Their scope is determined by public and private modifiers.

17 The Quiz What’s the difference between a.class file and a class definition? When you would make a function “static”? 17