Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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. Business entity classes: – Employee, Customer, etc.

3 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

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

5 Using Class Must import the package: <%@page import="myPackage.*" % Define a class variable: empClass e1 = new empClass();

6 Example of using a class <% empClass e1 = new empClass(); e1.eid="E2"; e1.ename="Peter"; e1.salary=6000.00; out.println(e1.empTax()); %>

7 Creating Property with Property Procedures Implementing a property with a public variable the property value cannot be validated by the class. We can create read-only or write-only properties with property procedure. Steps: – Declaring a private class variable to hold the property value. – Writing a property procedure to provide the interface to the property value.

8 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.

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

10 Using the Class <% empClass2 e1 = new empClass2(); e1.setEID("E1"); e1.setEname("Peter"); e1.setSalary(6000); out.println(e1.empTax()); %>

11 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.

12 Anatomy of a Class Module Class Module Public Variables & Property Procedures Public Procedures & Functions Exposed Part Private Variables Private Procedures & Functions Hidden Part Private variables and procedures can be created for internal use. Encapsulation

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

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

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

17 Property Procedure Code Example: Enforcing a maximum value for salary public void setSalary(double salary){ if (salary > 150000) { pvSalary = 150000; } else { pvSalary = salary; }

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

19 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.

20 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

21 Java Class Inheritance: extends public class secretary extends empClass{ private double pvWPM; public void setWPM(double wpm){ pvWPM=wpm; } public double getWPM(){ return pvWPM; }

22 Example secretary S1 = new secretary(); S1.setEID("E3"); S1.setEname("Mary"); S1.setSalary(4500); out.println(S1.empTax());

23 Method Override : If a class inherits a method from its super class, then there is a chance to override the method class Animal{ public void move(){ System.out.println("Animals can move"); }} class Dog extends Animal{ public void move(){ System.out.println("Dogs can walk and run"); }} public class TestDog{ public static void main(String args[]){ Animal a = new Animal(); // Animal reference and object Animal b = new Dog(); // Animal reference but Dog object a.move();// runs the method in Animal class b.move();//Runs the method in Dog class }}

24 final Class and final Methid A final class cannot be subclassed. – public final class MyFinalClass {...} – public class ThisIsWrong extends MyFinalClass {...} // forbidden A final method can't be overridden by subclasses. public class MyClass { public void myMethod() {...} public final void myFinalMethod() {...} } public class AnotherClass extends MyClass { public void myMethod() {...} // Ok public final void myFinalMethod() {...} // forbidden }

25 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.

26 Method Overloading Example public double empTax() { return Salary *.1; } public double empTax(double sal) { return sal *.1; }

27 Database Handling Classes

28 Data Source Database Classes Forms Reports

29 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.

30 Example Customer Class: – Properties: CID, Cname, City, Rating – Method: public Boolean GetCustData(String cid) – This methid 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.

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

32 public Boolean GetCustData(String cid) { Connection connection = null; String DBUrl="jdbc:odbc:myCustomer"; 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()); }

33 Using Class Must import the package: <%@page import=“myPackage.*" % Define a class variable: Customer C1 = new Customer();

34 Enter CID form Enter CID:

35 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: ">

36 Adding a AddNewCustomer Method public Boolean addNewCustomer() This method assumes user initializing properties using data from a form, then call this method to add the record to database.

37 public Boolean addNewCustomer() { Connection connection = null; String DBUrl="jdbc:odbc:myCustomer"; Boolean addSuccess=false; try { connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); String strSQL; strSQL = "Insert into Customer values ('"; strSQL += pvCID + "','" + pvCname + "','"; strSQL += pvCity + "','" + pvRating + "')"; int Count; Count=SQLStatement.executeUpdate(strSQL); if (Count==1) { addSuccess=true;} } catch(SQLException e) { System.out.println(e.getMessage()); } finally { return addSuccess; }

38 Customer Data Entry Form Enter CID: Enter Cname: Enter City: Enter Rating:

39 <% Customer C1 = new Customer(); C1.setCID(request.getParameter("CID")); C1.setCname(request.getParameter("cname")); C1.setCity(request.getParameter("city")); C1.setRating(request.getParameter("rating")); if (C1.addNewCustomer()) out.println("Adding sucessful"); else out.println("Adding unsuccessful"); %>

40 Adding a Class to a C# Project Project/Add Class – Assigna meaningful name. Steps: – Adding properties Property procedures: Set / Get Or declare Public variables – Adding methods – Adding events, exceptions

41 Class Code Example: Properties defined using Public variables class emp { public string eid; public string ename; public double salary; public double empTax() { return salary *.1; }


Download ppt "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."

Similar presentations


Ads by Google