Announcements • Final Exam: TBD Static Variables and Methods • static means “in class” methods and variables • static variable: one per class (not one per object) • static method: no invoking object (invoke with className.method()) • Example: Math class methods threeToFifth = Math.pow(3.0, 5.0); Static Variable Example public class MyClass { … private final int STARTID = 1; //the following are one per object private int IDNum = -1; //the following are one per class private static int nextIDNum = STARTID; public MyClass() { … IDNum = nextIDNum; nextIDNum++; } Client Program for MyClass public static void main(String [] args) { … myClass first = new myClass(); //IDNum is 1 myClass second = new myClass(); //IDNum is 2 } Static Method Example public class MyClass { … private final int STARTID = 1; //the following are one per object private int IDNum = -1; //the following are one per class private static int nextIDNum = STARTID; … public static int GetNextID() { // Called by MyClass.GetNextID() return nextIDNum; } } Client Program for MyClass public static void main(String [] args) { … myClass first = new myClass(); //IDNum is 1 myClass second = new myClass(); //IDNum is 2 System.out.println(“Next ID Num is:” + MyClass.GetNextID()); //Displays ID 3 } Other Class Methods • boolean equals( objType comObj) – compares invoking object with passed object – Returns true if both are “equal”, false if not • void display() – Displays attributes • String toString() – Converts all attributes to String and returns String public class MyClass { … private final int STARTID = 1; //the following are one per object private int IDNum = -1; //the following are one per class private static int nextIDNum = STARTID; … public boolean equals(MyClass compare) { if (IDNum == compare.IDNum) return true; else return false; } } Project • Turn in to TA – – – – .java File for main() .java File for class .doc “What I learned” file .txt Data File • Confirm that TA Received Your Program • No Late Projects Accepted! Documentation • Comments: – Document Approach to Problem • Outline Design Above main() and class • Briefly Describe Methods – Comment Any Unobvious Code • Analysis: – What Did You Learn from the Project? – What Went Wrong? What Went Right? – What Would You Do Differently? Final Exam • • • • 2 Hours 200 Points 30% of Grade Must Take to Pass Course Need to Know for Final • Everything Through Exam 2 • Plus: – Passing Arrays to Methods – Array Initialization – Classes • • • • Constructors Accessor Methods Mutator Methods toString() and equals() Methods Final Exam • 200 Points Total – 30% of Final Grade • 90 points Matching or Short Answer or Multiple Choice (Terminology, Operators, JAVA Basics) • 30 Points Program Output • 80 Points Programming (Full Programs) Final Exam • Study Previous Exams and Quizzes • Make Sure to Study Arrays, and Classes • Have fun 8) !! What Have You Learned? • Logical Thinking, Design, Planning • “Real World” Issues (Users, Run-time problems, “off-by-one”, Commenting, etc.) • Programming: – – – – – – – Variables Constants Selection Iteration Files Methods Classes Future • • • • cs116 Data Structures (lists, stacks) Searching and sorting Classes – Inheritance – Encapsulation – Templates