Download C# Classes

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Classes
ISYS 350
Introduction to Classes
• Two basic uses of class:
– 1. A class is a way to organize functions (methods)
to perform calculations.
• Example: Java Math class offers methods such as pow,
sqrt etc. to perform basic math calculations.
– 2. A class is used to define an object.
Declaring a Class
Example:
class MyClass {
method declarations
}
Defining Functions (Methods)
• Modifier ReturnType functionName(dataType arg1,..,dataType argN)
{
• Statements;
• return return_value;
}
– 1. Modifiers—such as public, private.
– 2. The return type—the data type of the value returned by the
method, or void if the method does not return a value.
– 3. The method name
– 4. Optional list of parameters in parenthesis separated by a
comma
– 5. Any method that is not declared void must contain a return
statement with a corresponding return value, like this:
return return_Value;
Example: myFinancial Class
• Define a myFinancial class with two methods:
– myPmt(double loan, double rate, doule term)
– myFv(double pv, double rate, double years)
Code Example: myFinancial Class
public class myFinancial {
public double myFV(double pv, double rate, double years)
{
double fv;
fv=pv*Math.pow(1+rate,years);
return fv;
}
public double myPmt(double loan, double rate, double term)
{
double pmt;
pmt=(loan*rate/12)/(1-Math.pow(1+rate/12,-12*term));
return pmt;
}
}
Adding Class to a Java Web Project
• Java classes used with a web application must be stored as
a “Package”. A package may have several classes.
• 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
Using Class
• Must import the package:
• <%@page import="myPackage.*" %>
• Define a class variable:
• myFinancial myFin=new myFinancial();
FV HTML Form
<form name="fvForm" method="post" action="computeFV.jsp">
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>
<input type="submit" value="ComputeFVJSP" name="btnCompute" />
computeFV.jsp
<%@page import="myPackage.*" %>
<%
String myPV, myRate, myYear;
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);
myFinancial myFin=new myFinancial();
FV=myFin.myFV(PV, Rate, Year);
out.println("FutureValue is:"+ FV);
%>
Use classes to define objects
• 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.
Business Entity Classes
• Customer entity with properties:
– CID
– Cname
– City
– Rating
• Employee entity with properties:
– EID, Ename, HireDate, Salary, Sex, etc.
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;
}
}
Using Class
• Must import the package:
• <%@page import="myPackage.*" %>
• Define a class variable:
• empClass e1 = new empClass();
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>
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.
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.
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;
}
}
Using the Class
<body>
<%
empClass2 e1 = new empClass2();
e1.setEID("E1");
e1.setEname("Peter");
e1.setSalary(6000);
out.println(e1.empTax());
%>
</body>
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.
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.
Property Procedure Code Example:
Enforcing a maximum value for salary
public void setSalary(double salary){
if (salary > 150000)
{
pvSalary = 150000;
}
else
{
pvSalary = salary;
}
}
Constructor
• A class may have a constructor, but is not
required to 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;
}
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;
}
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>
Inheritance
• The process in which a new class can be based
on an existing class, and will inherit that class’s
interface and behaviors. The original class is
known as the base class, super class, or parent
class. The inherited class is called a subclass, a
derived class, or a child class.
Employee Super Class with Three
SubClasses
All employee subtypes
will have emp nbr, name,
address, and date-hired
Each employee subtype
will also have its own
attributes
Java Class Inheritance:
extends
public class secretary extends empClass{
private double pvWPM;
public void setWPM(double wpm){
pvWPM=wpm;
}
public double getWPM(){
return pvWPM;
}
}
Example
secretary S1 = new secretary();
S1.setEID("E3");
S1.setEname("Mary");
S1.setSalary(4500);
out.println(S1.empTax());
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.
Method Overloading Example
public double empTax()
{
return Salary * .1;
}
public double empTax(double sal)
{
return sal * .1;
}
Java Servlet
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 be opened by
itself.
• A servlet is called by a HTML form’s action
attribute:
• <form name="fvForm" method="post" action="FVServlet">
Adding a Servlet
• Servlet is a class with a “java” extension:
– Ex: FVServlet.java
• It must belong to a package:
– Ex:
ServletPackage
• FVServlet.java
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>");
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>
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);
Servlet: depTableServlet
to create 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>
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;
}