Lecture 15 Intra Class Method Calling Last time 1 Project 3 due 10 9 2 Exam 1 coming 10 11 3 APIs comments and documentation Today 1 Project 3 due 10 9 2 Exam 1 coming 10 11 3 APIs comments and documentation 10 4 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Project 3 Is Assigned The assignment is on the CMSC 131 web site click Projects link It is due Monday 10 9 at 11 pm The project is open Start now Read entire assignment from beginning to end before starting to code Check out assignment now from CVS Follow the instructions exactly as much of grading is automated CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Exam 1 Is 10 11 in Discussion Section Take the test in your own section Test will cover material since beginning of semester Test will be closed book closed neighbor Study CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 Study When Project and test are very close to one another This is part of college What to do Start project immediately Aim to finish project by Friday Start studying for exam on Saturday even if project not yet finished You can do it CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 Recall Beginning with Project 3 you will be graded on comments as well as indentation Comment Every variable what is variable for Every method what does method do Every class what is class for who wrote it CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Methods Calling Methods Four kinds of methods so far public non static public static private non static private static We have seen that methods can call methods Example Date print Date println methods from Lecture 10 Date print printed date information Date println printed same info followed by carriage return The second called the first to ensure uniformity of date format Question What kind of methods can call other kinds of methods CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 Examples Assume following class specification public class C public void f public static void g private void h private static void i Can Body of f call g Yes Body of g call f No Body of h call g Yes Body of f call h Yes Body of h call f Yes Body of i call g Yes Body of i call h No CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 What Is Rule Recall Static methods are owned by class Other methods are owned by objects So for methods in same class Non static methods may call static or non static methods Static methods may call other static methods Public private doesn t matter in relation to who may call whom CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 When Should One Method Call Another Whenever it call Want to avoid duplication of effort Want to eliminate possibilities for inconsistent behavior Example Date java for this class meeting The method for converting dates to strings public String toString return month separator day separator year The method for printing dates to System out public void print System out print toString Method for printing dates followed by carriage return public void println print System out println CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 Invoking Static Methods May be done Through class Through object C g C obj new C obj g Rule of thumb always invoke through class Reinforces static status of class Avoids confusion between instance static methods CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 Accessing static Variables inside Methods Rules are similar to method invocation Static methods may refer only to local or static variables in body Non static methods may refer to local static instance variables in body Public private doesn t matter in this context Example public class C public int a 0 public static int b 1 public void f public static void g Can body of f refer to a Yes Can body of f refer to b Yes Can body of g refer to a No Can body of g refer to b Yes CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 Course Review So Far Hardware Software CPU Main memory Bit byte word Memory address Operating system Application Machine assembly languages Compilers interpreters Java Virtual Machine Primitive built in types String Expressions Control flow If else else if Blocks Scanner Logical operators Short circuiting Loops final Dangling else Type casting upcasting downcasting Software lifecycle Objects Classes Heap Stack Reference typed variable Pseudo code Object equality Aliasing Constructors Overriding Static public private Stack frame Library Package CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11
View Full Document