Introduction to Java Lecture 3 Topics Inheritance Variable Scope Static Variables Access Control Packages Input Output Recursive Inheritance 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 MSStudent public class MSStudent extends Student public class MSFEStudent extends MSStudent Inheritance public 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 inheritance A class can have only one parent Student name uscid usename department degree MSStudent PHDStudent UML diagram MSStudent MSStudent Any Questions Variable Scope Variables are accessible inside Variable Scope this keyword Refer to object itself Object level myCount is set to be newValue void setCount int newValue this myCount newValue public static void main String args The meanings of public static void Static 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 methods Static 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 applet Math Package import java lang Math Import the java Math package public 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 method Math 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 html Any Questions Introduction to Java Input Output Input 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 Java Recursion Recursion 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 finally Recursion Binary Search Recursion 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 Design Program 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 design
View Full Document
Unlocking...