10 Structured and Object Oriented Programming in JavaServer
10 – Structured and Object Oriented Programming in Java-Server Pages Mark Dixon 1
Questions: HTML in Java • Are these correct (assume variables and fields exist)? Mark Dixon s += <td> + r. get. String("Model"); s = s r. get. String("Length"); h = "<div>" + h + "</div>"; 2
Questions: SQL in Java • Are these correct (assume variables and fields exist)? id = 4; sql = SELECT * FROM Customer; sql = sql " WHERE [Cust. ID] = " + id + "; "; Mark Dixon 3
Session Aims & Objectives • Aims – To highlight that the object oriented techniques covered earlier in your course can be used in JSP • Objectives, by end of this week’s sessions, you should be able to: – create a class definition in server-side code – create an instance of a class – create a class definition from a class diagram Mark Dixon 4
Structured Paradigm • Program made up of – data structures, and – routines (procedures and functions) that process the data within those structures. • Each routine should – perform single, clearly identifiable operation – be self-contained • Abstract data type = structure + procedures Mark Dixon 5
Procedures • named group of instructions Mark Dixon 6
Parameters • effectively local variable, value set in call Mark Dixon 7
Functions • like procedure, but returns a result Mark Dixon 8
Self-Contained Routines • Self-contained: – no references to external items (objects & variables) Double res; Double a 1(Double h, Double f){ return (h + f) * f; } void a 2(){ res = 0. 0; } Mark Dixon yes no 9
Question: Self-Contained • Are the following routines self contained? Double g; Double w; Double Square(Double n 1){ return n 1 * n 1; } Double u(Double num){ return num * (w + g); } Mark Dixon 10
Object-Oriented Paradigm • A program is made up of a number of objects that communicate with each other by passing messages • Each object contains – attributes/properties that represent its state, and – operations/methods that represent its behaviour • Objects often mirror the real world – Customers – Students – Patients Mark Dixon 11
Classes and Instances • Object Classes – general descriptions of types of objects, e. g. student, product, customer, lecturer, and room. • Object Instances – specific items of a given class, e. g. • • Mark Dixon each of you could be an instance of the student class Room 214 could be an instance of the room class I could be an instance of the lecturer class Bolt could be an instance of the part class 12
Object Concepts - Implementation • Properties – implemented as – data structures (variables, arrays, and types). • Methods – implemented as either – a procedure (to perform some processing), or – a function (to return a value). • Object oriented paradigm builds on (rather than replaces) the structured paradigm Mark Dixon 13
Class Diagrams • Used to describe structure of object classes: Class Name Module Code: string Title: string Get. Title(): string Set. Title(t: string) Count(): integer Mark Dixon Class Attributes/Properties Class Operations/Methods 14
Implementing Class Diagrams Module Code: String Title: String Get. Title(): string Set. Title(t: string) Count(): integer class Module{ String public Code; String public Title; public String Get. Title(){}; public void Set. Title(String t){}; public Integer Count(){}; } Mark Dixon 15
Example: Animals Mark Dixon 16
Example: Student Mark Dixon 17
Public and Private • Control access to properties and methods class a{ public Double x; private Double y; } public void Reset. Y(){ y = 0; } a b = new a(); this works (x is public) b. x = 5; b. Reset. Y(); this works (Reset. Y is public) b. y = 10; this will fail (y is private) Mark Dixon 18
Packages • Package = group of classes • In Java – All classes • must be created inside a package Mark Dixon 19
Benefits of OOP in code • Procedures and Functions are part of object – encapsulation • Related Data and Operations together • Private keyword – restrict access to data • Clearer code • Less prone to error Mark Dixon 20
Example: Counter (html) <html> <head><title>Pets</title> </head> <body> <form method="post"> <input name="btn. Reset" type="submit" value="Reset" /> <input name="btn. Up" type="submit" value="Up" /> <p><%=msg%></p> </form> </body> </html> Mark Dixon 21
Example: Counter (code) Counter. java Counter. jsp package My. Package; <%@page import="My. Package. *"%> public class Counter{ private int m. Count; <%! Counter c = new Counter(); Declaration tag public int Get. Count(){ (instances, return m. Count; %> functions, } <% procedures) public void Reset(){ int msg; if (request. get. Parameter("btn. Reset") != null){ } m. Count = 0; c. Reset(); public void Up(){ } else if (request. get. Parameter("btn. Up") != null){ m. Count = m. Count + 1; } c. Up(); } public void Down(){ m. Count -= 1; msg = c. Get. Count(); } %> } Mark Dixon 22
Questions: OOP class Counter{ private int m. Count; • How many – classes – properties – methods – functions 1 public int Get. Count(){ return m. Count; } 1 public void Reset(){ m. Count = 0; } 4 public void Up(){ m. Count = m. Count + 1; } public void Down(){ m. Count -= 1; } 2 } – procedures Mark Dixon 3 Double Twice(Double x){ return x * 2; } 23
Tutorial Exercise: Counter • Task 1: Get the Counter example from the lecture working. • Task 2: Modify your code – so that the value cannot go below 0 or above 10. Mark Dixon 24
- Slides: 24