Upload
ashley-walton
View
219
Download
0
Embed Size (px)
Citation preview
Web Programming
Assistant Professor
Xiaozhong Liu
http://scholarwiki.indiana.edu/S517/S517.html
Student…Student…
public class student {private String First_name;private String Last_name;private int ID;
public String getName () {return First_name + " " + Last_name;
}
public int getSUID () {return SUID;
}
public student () {//Constructor!
}}
Attributes or FieldsAttributes or Fields
BehaviorsBehaviors
Constructor(s)Constructor(s)
Student…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;
}}
Lab – Part ILab – 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.w hat is the possible methods of the class? (at least, install application, uninstall application, update memory…)3.design at least two constructors for this class4. 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 classJava 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 classJavadoc for class
Generate Javadoc for each class, attribute, or methodEclipse: source -> Generate Element Comment
Javadoc for classJavadoc for classGenerate class HTML map:Eclipse: Project -> Generate Javadoc
Lab – Part IILab – Part II
Generate Javadoc for the laptop class you just defined:
1.Read the javadoc tutorial: http://students.cs.byu.edu/~cs240ta/fall2012/tutorials/javadoctutorial.html2.Generate comments for class, attribute, and method by using Eclipse3.Generate Javadoc by using Eclipse, and open your class HTML by using browser.
DebugDebug
Break Point for debug…Break Point for debug…
Debug As…Debug As…
DebugDebug
Run your code line by line…Run your code line by line…
Monitor variables in the memoryMonitor variables in the memory
Step into or Step OverStep into or Step Over
DebugDebug
Debug is important to tell the information flow Debug is important to tell the information flow between different objects.between different objects.
Help you to test your code!Help you to test your code!
Help you to understand the code!Help you to understand the code!
Monitor the variable value change in the memory…Monitor the variable value change in the memory…
Lab 3: Debug CodeLab 3: Debug Code
Debug your code for Laptop-Application project.Debug your code for Laptop-Application project.
Set a break point in the main function.Set a break point in the main function.
Monitor the information (variable) flow by using Monitor the information (variable) flow by using “step into”. “step into”.
Debug your Assignment 2 – step into one method, Debug your Assignment 2 – step into one method, i.e., getVotes method, and monitor the variable i.e., getVotes method, and monitor the variable value changes. value changes.
Static attribute/methodStatic 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…Class, instance, properties…
public class student {IDNameGPArequired_creditmin_GPA
}
Class, instance, properties…Class, instance, properties…
public class student {IDNameGPArequired_creditmin_GPA
}
instance independent!
public class student {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 () {…
}}
LibraryLibrary
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…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…Relationship between classes…Inheritance, PolymorphismInheritance, Polymorphism
Relationship between classes…Relationship between classes…
public class Undergrad_student extends student {private String Major;
public String getMajor () {return Major;
}}
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;
}}
inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support.
Abstract classAbstract 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 ClassSuper 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);
}
}
Call some method from SuperClass
SubclassSubclass
• 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…Relationship between classes…
Manager
Part_time
Salesman
Full_time
Employee
CEO
Lab 4: Class Relationship Lab 4: Class Relationship
• Design a list of classes for Library• 1. Library resources (i.e., books and journals), which at
least has name, publisher, author, and year information.• 2. Books, with ISBN number, can be kept by users no
more than 7 days. If overdue, user need to pay $0.5 per fay.
• 3. Journals, with with vol number, can be kept by users no more than 20 days. If overdue, user need to pay $0.1 per fay.
• 4. User. Each user can check out no more than 3 library resources. You need to design a method to calculate how much your need to pay when he/she return a resource.
Practice: Practice:
Design a library system (object oriented design)Design a library system (object oriented design)
1.1.What is library resources?What is library resources?2.2.What is library books?What is library books?3.3.What is library journals?What is library journals?4.4.What is library digitalized journals?What is library digitalized journals?5.5.How about library category? How about library category?