Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
http://scholarwiki.indiana.edu/S517/S517.html Web Programming Assistant Professor Xiaozhong Liu Object Oriented Programming (OOP) • An OO program models the application as a world of interacting objects. Anything in Java is an object. • An object can create/access other objects. • An object can call another object’s (and its own) methods. Object Oriented Programming (OOP) • A particular type of objects in Java is described by a Class • One class has a list of properties, and a list of methods – i.e., computer class, and student class • An object in java is called an instance of a class. You can create/use more than one objects (instances) of the same class. 3-3 Student… Attributes or Fields public class student { private String First_name; private String Last_name; private int ID; Behaviors public String getName () { return First_name + " " + Last_name; } public int getIUID () { return ID; } } Student… Attributes or Fields public class student { private String First_name; private String Last_name; private int ID; Behaviors public String getName () { return First_name + " " + Last_name; } public int getSUID () { return SUID; } public student () { //Constructor! } } Constructor(s) Attributes and Fields private [static] [final] datatype name; Private or Public: share the target attribute to other class or not? data type variable type: int, double… or Class name Static: the target attribute is shared by all objects of the class Final: the target attribute is a constant Student… public class student { private String First_name; private String Last_name; private int ID; public String major; } public class IUclass { public String class_name; private int ID; public student [ ] studs; } public class Application { public static void main(String[] args) { student stu1 = new student(); student stu2 = new student(); student [ ] studs = new student [2]; studs[0] = stu1; studs[1] = stu2; IUclass S517 = new IUclass(); S517.class_name = "Web Programming"; S517.studs = studs; } } Student… public class student { private String First_name; private String Last_name; private int ID; public String major; } public class IUclass { public String class_name; private int ID; public student [ ] studs; } public class Application { public static void main(String[] args) { student stu1 = new student(); student stu2 = new student(); student [ ] studs = new student [2]; studs[0] = stu1; studs[1] = stu2; IUclass S517 = new IUclass(); S517.class_name = "Web Programming"; S517.studs = studs; } } Encapsulation and Information Hiding • A class interacts with other classes only through constructors and public methods • Other classes do not need to know the mechanics and implementation details of a class to use it effectively • Encapsulation facilitates team work and program maintenance (making changes to the code) Constructor – initialize an object public class IUclass { public String class_name; private int ID; public student [ ] studs; public IUclass (String class_name, int ID) { this.class_name = class_name; this.ID = ID; } public void addStudent (student stud) { … } public void dropStudent (student stud) { … } } Method belongs to a class public class IUclass { public String class_name; private int ID; public student [ ] studs; public IUclass (String class_name, int ID) { this.class_name = class_name; this.ID = ID; } public IUclass (int ID) { this.ID = ID; } public void addStudent (student stud) { } public void addStudent (int Student_ID) { } public void dropStudent (student stud) { } public void dropStudent (int Student_ID) { } } Lab – Part I Design a laptop class. You will need to tell: 1.what is the attributes of the class?(at least, memory, hard drive size, type…) 2.what is the possible methods of the class? (at least, install application, uninstall application, update memory…) 3.design at least two constructors for this class 4. which attribute/method should be private/public? 5.create at least two instances from another class. * Do you need to design another class – Application? Java Doc for class Example: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html VERY IMPORTANT! To tell the class usage. What is the class properties? What is the functionality of each class method? Usage? You can create your Java Doc easily! Javadoc for class Generate Javadoc for each class, attribute, or method Eclipse: source -> Generate Element Comment Javadoc for class Generate class HTML map: Eclipse: Project -> Generate Javadoc Lab – Part II Generate Javadoc for the laptop class you just defined: 1. Read the javadoc tutorial: http://students.cs.byu.edu/~cs240ta/fall2012/tutorials/javadoctut orial.html 2. Generate comments for class, attribute, and method by using Eclipse 3. Generate Javadoc by using Eclipse, and open your class HTML by using browser. Static attribute/method public class student { private String First_name; private String Last_name; private int ID; public String major; static int number_of_students = 50000; static public int get_num_of_students () { return number_of_students; } } number_of_students is instance independent!!! Class, instance, properties… public class student { ID Name GPA required_credit min_GPA } Class, instance, properties… public class student { ID Name GPA required_credit min_GPA } public class student { instance independent! public int ID; public String Name; public double GPA; public static int required_credit; public static double min_GPA; public double Calculate_GPA () { … } public static void print_min_GPA () { … } } Library Library is used to organize classes For instance: Java.util - miscellaneous utility classes Java.io – IO classes Java.sql – Database classes Import … if you want use some classes or a library… Graduate Student… public class Graduate_student { private String First_name; private String Last_name; private int ID; private String Advisor; private String Research_area; public String getName () { return First_name + " " + Last_name; } public int getIUID () { return IUID; } public int getResearchArea () { return Research_area; } } Relationship between classes… Inheritance, Polymorphism Relationship between classes… public class Undergrad_student extends student { private String Major; public String getMajor () { return Major; } } inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. public abstract class student { protected String First_name; protected String Last_name; protected int IUID; public String getName () { return First_name + " " + Last_name; } public int getIUID () { return IUID; } } Abstract class public abstract class SomeClass { ... public abstract void SomeMethod(); } Some of the methods in a class can be declared abstract and left with only signatures defined A class with one or more abstract methods must be declared abstract Super Class Call some method from Super Class public class graduate_student extends student { String research; public graduate_student(String first_name, String last_name, int ID, String research) { super(first_name, last_name, ID); this.research = research; } public void print_student_info () { super.print_student_info(); System.out.println("Research Area: " + research); } } Subclass • Inherits attributes and methods of its superclass • You can add new attributes and methods • You can re-define (override) a method of the superclass • You MUST provide its OWN constructors • Does not have direct access to its superclass’s private attributes and methods Relationship between classes… Manager Part_time Salesman Full_time Employee CEO Practice: Design a library system (object oriented design) 1. 2. 3. 4. 5. What is library resources? What is library books? What is library journals? What is library digitalized journals? How about library category?