Recitation 02/6/2009 CS 180 Department of Computer Science, Purdue - - PowerPoint PPT Presentation
Recitation 02/6/2009 CS 180 Department of Computer Science, Purdue - - PowerPoint PPT Presentation
Recitation 02/6/2009 CS 180 Department of Computer Science, Purdue University Announcements & Reminders Project 1 grades out Solution up & test cases on the Web Project 2 was due on Wednesday Project 3 is out
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 -- 100 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 * * @author 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 <className>” only if <className>.java has a main method “java <className>” runs only the main method that exists in <className>.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? Why? 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
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
- utside any method. Their scope is
determined by public and private modifiers.
The Quiz
What’s the difference between a .class file
and a class definition?
When you would make a function “static”?
17