PowerPoint Slides - Purdue University
Download
Report
Transcript PowerPoint Slides - Purdue University
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
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
2
Conventional Class Definition
Structure
Why Are Conventions Useful??
What Your Class Would Like
import java.util.Date;
Import Statement
/**
* Book -- a book that knows only its name
*
Comments
* @author Henry David Thoreau
**/
class Book {
private String name;
private Date dateMade;
public Book( ) {
Class Name
Data Member
Constructor
name = “I have no name”;
dateMade = new Date();
}
Methods
public String getName( ) {
return name;
}
public void setName(String newName) {
name = newName;
}
}
4
Class Definition and Object Usage
CookBook.java
class CookBook {
ColorBook.java
class ColorBook {
private int numImages;
private int numRecipes;
private String name;
private String name;
//Constructors(s)
//Constructor(s)
//Methods (e.g., getter & setter)
public static void main(String[] args){
//Methods (e.g., getter & setter)
public static void main( String[] args ) {
ColorBook colorBook1;
CookBook cookBook1;
colorBook1 = new ColorBook( );
cookBook1.setName(“Cooking!”);
colorBook1.setName(“CB1”);
System.out.println(cookBook1.getName());
colorBook1.setNumImages(35);
cookBook1.setName(“Cooking part Deux!”);
ColorBook colorBook2 = new ColorBook();
System.out.println(cookBook1.getName());
colorBook2.setName(“CB2”);
}
System.out.println(colorBook1.getName());
}
Why is it useful for each class to
have its own main method?
Convention
System.out.println(colorBook2.getName());
}
}
In what order would you develop
these
5
classes?
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
6
Access Modifiers
CookBook.java
Why?
class CookBook {
private int numRecipes;
private String name;
//Constructor(s)
public int getNumRecipes(){
return numRecipes;
}
Why?
public void setNumRecipes(int num){
numRecipes = num;
}
Why?
//Rest of Methods
public static void main(String[] args){
//Statements
}
}
What’s “static” about?
7
Constructor
class CookBook {
private int numRecipes;
private String name;
public CookBook (){
Why have
multiple
Constructors
?
numRecipes = 0;
name = “Joe Blog”;
}
public CookBook(String newName){
name = newName;
Defining even
ONE
Constructor
precludes you
from getting the
default
Constructor
numRecipes = 0;
}
public CookBook(String newName, int num){
numRecipes = num;
name = newName;
}
//Rest of Methods
}
8
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”);
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
System.out.println(lastCar.getOwner);
}
}
Note: Car class defined in another file in the same directory
9
Defining Class Constants
class BookStore{
private static final int zipCode = 47906;
private final String name = “Jays”;
//rest of class
Why is this bad?
}
10
Calling Methods
class Lion {
public void eatYou( ) {System.exit(0);}
public void finishingMove( ) {
eatYou();
When you call a method that’s
within the same class, you can call
the method by just using its name.
}
}
Class Jungle{
public void welcome( )
{System.out.println(“Welcome!”);}
public void wildLife( ) {
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.
Lion l1 = new Lion( );
welcome();
l1.eatYou( );
11
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
outside 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