recitation 02 6 2009
play

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


  1. 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 2

  3. Conventional Class Definition Structure Why Are Conventions Useful??

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

  5. Class Definition and Object Usage Convention ColorBook.java CookBook.java class ColorBook { class CookBook { private int numImages; private int numRecipes; private String name; private String name; //Constructors(s) //Constructor(s) //Methods (e.g., getter & setter) //Methods (e.g., getter & setter) public static void main( String[] args ) { 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()); } System.out.println(colorBook2.getName()); } Why is it useful for each class to } In what order would you develop these 5 have its own main method? 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 6

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

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

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

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

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

  12. 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

  13. Sample Matching

  14. Sample Matching Notice how one can hide data members by  declaring a local variable with the same name

  15. 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.

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

Recommend


More recommend