Introduction to JavaLecture 3Topics• Inheritance• Variable Scope• Static Variables• Access Control• Packages• Input/Output• RecursiveInheritance• We have a class called “Student”Inheritance• Now we need following classes:– Student• MSStudent– MSFEStudent– MSEEStudent• PHDStudent– ControlStudent– CommunictionStudent• Copy and Paste?Inheritance• MSStudent is a subclass of Student• MSFEStudent is a subclass of MSStudentpublic class MSStudent extends Student{}public class MSFEStudent extends MSStudent{}Inheritancepublic class MSStudent extends Student{// Add extra varibles and methods}Now, MSStudent class can use everything the Student has, do everything the Student do.Inheritance• Single inheritanceA class can have only one parent• UML diagram+name+uscid+usename+department+degreeStudentMSStudent PHDStudentMSStudentMSStudentAny Questions?Variable Scope• Variables are accessible inside {}Variable Scope• this keyword– Refer to object itself• Object level myCount is set to be newValuevoid setCount(int newValue){this.myCount = newValue;}• public static void main(String[] args) { }• The meanings of– public– static– voidStatic• Local variables in methods• Instance variable in objects• Static variable– Called class variable– Changing one value of a static variable in one object will change it all of the others– static int myCount = 0• Static Methods– main, math methodsStatic• Example from MIT course “Introduction to Java”Static• Example from MIT course “Introduction to Java”Access Control• Public & Private– Public: other classes can use– Private: only the class can use• Protect private class information• Separate class implementation from interface• Example from MIT course “Introduction to Java”• Example from MIT course “Introduction to Java”Any Questions?Packages• Packages are similar with directories– A group of developed classes• Classes in one packages are designed to realize similar purpose• Import is needed to use classes in other packages– import java.util.*;Packages• Example Packages:• java.lang:basic language functionality and fundamental types• java.util: collection data structure classes• java.io: file operations• java.net: networking operations, sockets, DNS lookups, ...• java.sql: Java Database Connectivity (JDBC) to access databases• java.awt: basic hierarchy of packages for native GUI components• javax.swing: hierarchy of packages for platform-independent rich GUI components• java.applet: classes for creating an appletMath Packageimport java.lang.Math; //Import the java Math packagepublic class Example {public static void main(String[] args) {int number1 = 620;int number2 = 703;System.out.println("The minimum of " + number1 + " and " + number2 + " is " + Math.min(number1, number2));}} min- static methodMath Package• Provide many math functions– static abs(int/long/float/double value)– static double pow(double x, double y); – static double exp/log(double d); – static double sqrt(double d); – static double cos/sin/tan/atan(double d); – static double ceil/floor(double a); http://docs.oracle.com/javase/7/docs/api/java/lang/Math.htmlAny Questions?Introduction to JavaInput/OutputInput/Output• Input: Use Scanner class to read text files• Example:FileReader reader = new FileReader(“input.txt”);Scanner in = new Scanner(reader);• Methods of Scanner:– in.next();– in.nextLine();– in.hasNextLine();– in.close();Input/Output• Output: Use PrinWriter class• Example:PrintWriter out = new PrintWriter(“output.txt”);• Methods:– out.print();– out.println();– out.close();Introduction to JavaRecursionRecursion• Any routine that calls itself is recursive• Two types of cases• Example: n factorial– base cases: 1! = 1– recursive cases: n! = n*(n - 1)!Recursion• Every recursive case should lead to a base case finallyRecursion – Binary SearchRecursion – Binary Search• int NOT_IN_ARRAY = -1;• int ARRAY_UNORDERED = -2;• int LIMITS_REVERSED = -3;• int binarySearch( int[] array, int lower, int upper, int target ){• int center, range;• range = upper - lower;• if (range < 0) {• return LIMITS_REVERSED;• } else if( range == 0 && array[lower] != target ){• return NOT_IN_ARRAY;• }• if( array[lower] > array[upper] )• return ARRAY_UNORDERED;• center = ((range)/2) + lower;• if( target == array[center] ){• return center;• } else if( target < array[center] ){• return binarySearch( array, lower, center - 1, target );• } else {• return binarySearch( array, center + 1, upper, target );• }• }Program Design• Good Design– Correct/No bugs– Easy to understand– Easy to extend– Good performance (running time/memory usage…)Program DesignProgram Design-Name• Classes: Nouns, uppercase first letter– CreditCards, Count, Example, …• Method: Verbs, lowercase first letter– min(), setValue(), draw(), …• Variables: Nouns, lowercase first letter– x, minValue, myCount, …Program Design• Design– Think classes design before coding program – Think algorithms before coding methods• Reuse– Use existing code or classes in packages• Test– Test your code– Test your
View Full Document