Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It...

38
Java Classes ISYS 350

Transcript of Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It...

Page 1: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Java Classes

ISYS 350

Page 2: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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.

Page 3: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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.

Page 4: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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

Page 5: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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;

}

}

Page 6: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Using Class

• Must import the package:• <%@page import="myPackage.*" %>

• Define a class variable:• empClass e1 = new empClass();

Page 7: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Example of using a class

<body> <% empClass e1 = new empClass(); e1.eid="E2"; e1.ename="Peter"; e1.salary=6000.00; out.println(e1.empTax()); %> </body>

Page 8: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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.

Page 9: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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.

Page 10: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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; } }

Page 11: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Using the Class

<body> <% empClass2 e1 = new empClass2(); e1.setEID("E1"); e1.setEname("Peter"); e1.setSalary(6000); out.println(e1.empTax()); %> </body>

Page 12: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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.

Page 13: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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.

Page 14: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Property Procedure Code Example:Enforcing a maximum value for salary

public void setSalary(double salary){if (salary > 150000) { pvSalary = 150000; } else { pvSalary = salary; }}

Page 15: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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; }

Page 16: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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; }

Page 17: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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; } }

Page 18: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Example

<body> <% 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()); %> </body>

Page 19: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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.

Page 20: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Method Overloading Example

public double empTax()

{

return Salary * .1;

}

public double empTax(double sal)

{

return sal * .1;

}

Page 21: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Database Handling Classes

Page 22: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Database Handling Classes

Data SourceDatabaseClasses

FormsReports

Page 23: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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.

Page 24: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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.

Page 25: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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;}

Page 26: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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; }

}}

Page 27: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Using Class

• Must import the package:• <%@page import=“myPackage.*" %>

• Define a class variable:• Customer C1 = new Customer();

Page 28: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Enter CID form

<form action="getCustomerClass.jsp" method="post"><p>Enter CID: <input name="cid" type="text" size="20" maxlength="20" ></p>

<input type="submit" value="Submit"></form>

Page 29: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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: <input type="text" name="name" size="20" value="<%=Cname%>"><br> City: <input type="text" name="city" size="20" value="<%=City%>"><br> Rating: <input type="text" name="rating" size="3" value="<%=Rating%>"><br>

Page 30: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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

Page 31: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Java Servlet

Page 32: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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:• <form name="fvForm" method="post" action="FVServlet">

Page 33: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Adding a Servlet

• Servlet is a class with a “java” extension:– Ex: FVServlet.java

• It must belong to a package:– Ex: ServletPackage

• FVServlet.java

Page 34: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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("<html>");• out.println("<head>");

Page 35: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Example: Future Value Calculator:Requesting FVServlet servlet

<form name="fvForm" method="post" action="FVServlet"> Enter present value: <input type="text" name="PV" value="" /><br><br> Select interest rate: <select name="Rate"> <option value=.04>4%</option> <option value=.05>5%</option> <option value=.06>6%</option> <option value=.07>7%</option> <option value=.08>8%</option> </select><br><br> Select year: <br> <input type="radio" name="Year" value="10" />10-year<br> <input type="radio" name="Year" value="15" />15-year<br> <input type="radio" name="Year" value="30" />30-year<br><br> <br> <input type="submit" value="ComputeFVJSP" name="btnCompute"/> </form>

Page 36: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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);

Page 37: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

Straight Line Depreciation Table

Straight Line Depreciation Table <form name="depForm" method="post" action="depTableServlet"> Enter Property Value: <input type="text" name="pValue" value="" /><br> Enter Property Life: <input type="text" name="pLife" value="" /><br> <input type="submit" value="Show Table" name="btnShowTable" /> </form>

Page 38: Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.

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" + "<br>"); out.println("Property Value: <input type='text' name='pValue' value='" + nf.format(value) + "' /><br>"); out.println("Property Life: <input type='text' name='pLife' value='" + life + "' /><br>"); depreciation=value/life; totalDepreciation=depreciation; out.println( "<table border='1' width='400' cellspacing=1>"); out.println("<thead> <tr> <th>Year</th> <th>Value at BeginYr</th>"); out.println("<th>Dep During Yr</th> <th>Total to EndOfYr</th></tr> </thead>"); out.println("<tbody>"); for (int count = 1; count <= life; count++) { out.write("<tr>"); out.write(" <td width='10%'>" + count + "</td>"); out.write(" <td width='30%'>" + nf.format(value) + "</td>"); out.write(" <td width='30%'>" + nf.format(depreciation) + "</td>"); out.write(" <td width='30%'>" + nf.format(totalDepreciation) + "</td>"); value -= depreciation; totalDepreciation+=depreciation; }