Download presentation
Presentation is loading. Please wait.
Published byRoman Dickes Modified over 9 years ago
1
Java Classes ISYS 350
2
Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the properties (fields) and methods a particular type of object can have. – One or more object can be created from the class. – Each object created from a class is called an instance of the class.
3
Business Entity Classes Customer entity with properties: – CID – Cname – City – Rating Employee entity with properties: – EID, Ename, HireDate, Salary, Sex, etc. An instance of this type of object related to one record in the database table.
4
Adding Class to a Java Web Project Java classes used with a web application must be stored as a “Package”. Step 1: Create a new package – Right click the Source Packages folder and select New/Java Package – Name the new package For example, myPackage Step 2: Creating new class in the package – Right click the package folder and select New/Java Class – Name the class
5
Class Code Example: Properties defined using Public variables public class empClass { public String eid; public String ename; public Double salary; public Double empTax() { return salary *.1; }
6
Using Class Must import the package: <%@page import="myPackage.*" % Define a class variable: empClass e1 = new empClass();
7
Example of using a class <% empClass e1 = new empClass(); e1.eid="E2"; e1.ename="Peter"; e1.salary=6000.00; out.println(e1.empTax()); %>
8
Creating Property with Property Procedures Steps: – Declaring a private class variable to hold the property value. – Writing a property procedure to provide the interface to the property value.
9
empClass Example Use a private variable to store a property’s value. private String pvEID, pvEname, pvSalary; Use set and get method to define a property: public void setEID(String eid){ pvEID=eid; } public String getEID(){ return pvEID; } Note: “void” indicates a method will not return a value.
10
Code Example: empClass2 public class empClass2 { private String pvEID; private String pvEname; private double pvSalary; public void setEID(String eid){ pvEID=eid; } public String getEID(){ return pvEID; } public void setEname(String ename){ pvEname=ename; } public String getEname(){ return pvEname; } public void setSalary(double salary){ pvSalary=salary; } public double getSalary(){ return pvSalary; } public double empTax() { return pvSalary *.1; }
11
Using the Class <% empClass2 e1 = new empClass2(); e1.setEID("E1"); e1.setEname("Peter"); e1.setSalary(6000); out.println(e1.empTax()); %>
12
How the Property Procedure Works? When the program sets the property, the set property procedure is called and procedure code is executed. The value assigned to the property is passed in the value variable and is assigned to the hidden private variable. When the program reads the property, the get property procedure is called.
13
Encapsulation Encapsulation is to hide the variables or something inside a class, preventing unauthorized parties to use. So methods like getter and setter access it and the other classes access it through property procedure.
14
Property Procedure Code Example: Enforcing a maximum value for salary public void setSalary(double salary){ if (salary > 150000) { pvSalary = 150000; } else { pvSalary = salary; }
15
Implementing a Read-Only Property: Declare the property with only the get procedure public void setHireDate(String hdate) throws ParseException{ DateFormat format = new SimpleDateFormat("dd/MM/yyyy"); pvHdate=format.parse(hdate); } public Date getHireDate(){ return pvHdate; } public Double YearsEmployed() { Double years; Date date = new Date(); years = (date.getTime() -pvHdate.getTime())/(24*60*60*1000)/365.25 ; return years; }
16
Constructor A class may have a constructor. When a class is created, its constructor is called. A constructor has the same name as the class, and usually initialize the properties of the new object. Example: public empClass(){ } public empClass(String EID,String ENAME, double SALARY) { pvEID=EID; pvEname=ENAME; pvSalary=SALARY; }
17
public class empClass { private String pvEID; private String pvEname; private double pvSalary; public empClass() { } public empClass(String EID,String ENAME, double SALARY) { pvEID=EID; pvEname=ENAME; pvSalary=SALARY; } public void setEID(String eid){ pvEID=eid; } public String getEID(){ return pvEID; } public void setEname(String ename){ pvEname=ename; } public String getEname(){ return pvEname; } public void setSalary(double salary){ pvSalary=salary; } public double getSalary(){ return pvSalary; } public double empTax() { return pvSalary *.1; }
18
Example <% empClass E1 = new empClass(); E1.setEID("E1"); E1.setEname("Peter"); E1.setSalary(5000); out.println(E1.empTax()); empClass E2=new empClass("E2","Paul",6000); out.println(E2.empTax()); %>
19
Overloading A class may have more than one methods with the same name but a different argument list (with a different number of parameters or with parameters of different data type), different parameter signature.
20
Method Overloading Example public double empTax() { return Salary *.1; } public double empTax(double sal) { return sal *.1; }
21
Database Handling Classes
22
Data Source Database Classes Forms Reports
23
Single-Record-Handling Classes – Retrieves a single record from the database and makes it available to your application in the form of an object. – The fields in the record are exposed as the object’s properties. – Any actions performed by the data (updates, calculations, etc.) are exposed as the object’s methods.
24
Example Customer Class: – Properties: CID, Cname, City, Rating – Method: public Boolean GetCustData(String cid) – This method will retrieve customer record based on the cid. – If record exists, it will initialize properties using the retrieved record; otherwise this function return false to signal the customer does not exist.
25
Code Example: Customer Class package myPackage; import java.sql.*; public class Customer { private String pvCID, pvCname, pvCity, pvRating ; public Customer(){} public void setCID(String cid){ this.pvCID=cid; } public String getCID(){ return pvCID; } public void setCname(String cname){ this.pvCname=cname; } public String getCname(){ return pvCname; } public void setCity(String city){ this.pvCity=city; } public String getCity(){ return pvCity; } public void setRating(String rating){ this.pvRating=rating; } public String getRating(){ return pvRating; }
26
public Boolean GetCustData(String cid) { Connection connection = null; String DBUrl="jdbc:derby://localhost:1527/CRMDB"; Boolean RecExist=false; try { connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); String strSQL="select * from customer where cid='" + cid + "'"; ResultSet rs = SQLStatement.executeQuery(strSQL); if (rs.next()) { pvCID=rs.getString("CID"); pvCname=rs.getString("CNAME"); pvCity=rs.getString("CITY"); pvRating=rs.getString("Rating"); rs.close(); RecExist=true; } else { System.out.print("Customer not exist!"); rs.close(); RecExist=false; } catch(SQLException e) { System.out.println(e.getMessage()); } finally { return RecExist; }
27
Using Class Must import the package: <%@page import=“myPackage.*" % Define a class variable: Customer C1 = new Customer();
28
Enter CID form Enter CID:
29
JSP Using Class <% String custID = request.getParameter("cid"); String City, Cname, Rating; Customer C1 = new Customer(); if (C1.GetCustData(custID)) { City=C1.getCity(); Cname = C1.getCname(); Rating = C1.getRating(); } else { City="NA"; Cname = "NA"; Rating = "NA"; } %> Cname: "> City: "> Rating: ">
30
Other Methods Insert a record Updating a record Deleting a record Note: CRUD –Create or add new entries – Read, retrieve, search, or view existing entries – Update or edit existing entries – Delete/deactivate existing entries
31
Java Servlet
32
What is Java Servlet? It is a java class that serves a client request and receives a response from the server. Servlets are most often used to: Process or store data that was submitted from an HTML form Provide dynamic content such as the results of a database query. It is not a web page and cannot run by itself. A servlet is called by a HTML form’s action attribute:
33
Adding a Servlet Servlet is a class with a “java” extension: – Ex: FVServlet.java It must belong to a package: – Ex:ServletPackage FVServlet.java
34
Servlet’s processRequest Method This method use the same request and response objects as JSP. For example, it uses the request.getParameter method to read the data submitted with http request: – myPV=request.getParameter("PV"); It uses the out.println statement to send HTML code to browser: out.println(" ");
35
Example: Future Value Calculator: Requesting FVServlet servlet Enter present value: Select interest rate: 4% 5% 6% 7% 8% Select year: 10-year 15-year 30-year
36
FVServlet protected void processRequest(HttpServletRequest request, HttpServletResponse response) String myPV, myRate, myYear,qString; myPV=request.getParameter("PV"); myRate=request.getParameter("Rate"); myYear=request.getParameter("Year"); double FV, PV, Rate, Year; PV=Double.parseDouble(myPV); Rate=Double.parseDouble(myRate); Year=Double.parseDouble(myYear); FV=PV*Math.pow(1+Rate,Year); out.println("FutureValue is:"+ FV);
37
Straight Line Depreciation Table Enter Property Value: Enter Property Life:
38
String strValue, strLife; strValue=request.getParameter("pValue"); strLife=request.getParameter("pLife"); double value, life, depreciation,totalDepreciation=0; value=Double.parseDouble(strValue); life=Double.parseDouble(strLife); NumberFormat nf = NumberFormat.getCurrencyInstance(); out.println("Straight Line Depreciation Table" + " "); out.println("Property Value: "); out.println("Property Life: "); depreciation=value/life; totalDepreciation=depreciation; out.println( " "); out.println(" Year Value at BeginYr "); out.println(" Dep During Yr Total to EndOfYr "); out.println(" "); for (int count = 1; count <= life; count++) { out.write(" "); out.write(" " + count + " "); out.write(" " + nf.format(value) + " "); out.write(" " + nf.format(depreciation) + " "); out.write(" " + nf.format(totalDepreciation) + " "); value -= depreciation; totalDepreciation+=depreciation; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.