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

17
Recitation 02/6/2009 CS 180 Department of Computer Science, Purdue University
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    1

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

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

Recitation 02/6/2009

CS 180

Department of Computer Science,

Purdue University

Page 2: 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 -- 100 points

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

Conventional Class Definition Structure

Why Are Conventions Useful??

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

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 NameData Member

Constructor

Methods

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

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 UsageConvention

Why is it useful for each class to have its own main method?

In what order would you develop these classes?

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

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

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

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?

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

8

Constructorclass 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?

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

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

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

10

Defining Class Constants

class BookStore{

private static final int zipCode = 47906;

private final String name = “Jays”;

//rest of class

}

Why is this bad?

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

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.

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

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

Page 13: Recitation 02/6/2009 CS 180 Department of Computer Science, Purdue University.
Page 14: Recitation 02/6/2009 CS 180 Department of Computer Science, Purdue University.

Sample Matching

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

Sample Matching

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

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

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.

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

17

The Quiz

What’s the difference between a .class file and a class definition?

When you would make a function “static”?

17