Search This Blog

Tuesday 22 February 2011

Internet Programming Lab








Department of Computer Applications


Manual
for
MC1708 – Internet Programming Lab


Contents
1.     Tools of the Trade
2.     Program #1 – Classes and Objects
3.     Program #2 - Overloading and Overriding


Tools of the Trade
Any programming language requires a text editor to type the source code, a compiler to convert it to machine understandable code and a debugger to guide the programmer in debugging. When these are available as one application we call that an Integrated Development Environment or IDE, in short.Though there is a handful of IDEs available for Java development, we will use Notepad to type our programs and the Command shell to compile and execute them. The following steps are to be followed for the same.

1.     Open Notepad(Start->Programs->Accessories->Notepad) and type the Java code in it.
2.     Save the file with .java extension. The name of the source file should ideally be the name of the class that contains the main method. This is because the Java compiler creates a file with .class extension for each class in the source code. The .class file contains the intermediate byte code. The interpreter takes as parameter the name of the class file that contains the main method because that is the first method to be called in a Java program.
3.     Open a Windows command shell by typing “command” or “cmd” in the Run dialog box(Start->Run).


4.     On the command shell type javac filename.java to compile the file. If Java is not properly installed or the PATH variable does not hold the path to the bin directory of the Java installation, you’ll get an error message. In that case type
set path=%path%;C:\j2sdk1.4.0\bin;
on the command shell.

If Java is installed this should resolve your problem. If problem persists, contact your facilitator or lab coordinator. This method requires you to type the path everytime you open the command window. So you may set the path permanently as follows.

a) Right Click on the “My Computer” Menu/Icon and choose Properties from the Pop up  Menu

b) Choose the “Advanced” Tab and click the “Environment Variables” button

c) Choose “Path” under the System Variables Section.
d) Type the value for path in the Variable Value Text Box and Click OK
5.     If the source code does not contain any syntax errors the class file will be created.
6.     To execute the java program type java classfilename on the command shell.




Program #1
Classes and Objects

Objectives:
4      To understand and implement the object oriented principle of data encapsulation
4      To learn how to define a class in Java
4      To learn how to create objects and invoke methods in Java
4      To learn how to read input and display output in Java

Prerequisites
4      Student should have a sound knowledge of the object oriented principle of data encapsulation
4      Student should know the syntax for declaration of variables, classes and definition of methods in Java

Tools
4      A text editor (preferably Notepad)
4      Java 2 SDK (1.3 or higher)
Or
4      Any IDE for Java (BlueJ, NetBeans, etc.,)

Concept
Class:  A set of objects that share similar characteristics can be represented by a class. It is a template for an object. A class contains data and methods that manipulate those data.
Syntax for class definition:
class classname{
                        Type instance-variable1;
            Type instance-variable2;
                        //…
            Type instance-variableN;

                        Type methodname1(parameter-list){
                                    //body of method
}

            Type methodname2(parameter-list){
                                    //body of method
}
//…
            Type methodnameN(parameter-list){
                                    //body of method
}
}

Syntax for variable declaration:
<access specifier> <modifier> <datatype> variablename [= value];
Syntax for Method Definition
<access-specifier> <modifier> <return type> methodname(parameter-list){
//body of method
}

Object: An object is an instance of a class. An object is created as follows:
class-name object = new class-name();


Aim: To create and define classes for banking operations and use them in a Java program.

Procedure:
1.      Define a class called Account with the following members.
Data members
AcctNo                                   String
AcctHolderName      String
AcctType                   String
Balance                      double

Methods
Parameterized Constructor – to create an account
displayDetails()       - to display all the details regarding the account
getBalance()                        -to return the value  in the variable “Balance”
getAcctNo()              - to return the AcctNo to the calling method
updateBalance(amount,debit/credit) – to update the balance amount, deduct amount if transaction type is debit and add amount if it is credit.
           
2.      Create another class called Bank with the following members
Data members
Accounts                   Array of Account
NoOfAccounts                       integer(static value)

            Methods
createAccount()
deposit(amt,tran_type) – call  updateBalance() and credit the amount to the account
withdraw(amt,tran_type) – call updateBalance() and debit the amount from the account, must check if the balance is greater than or equal to Rs.500 after debiting otherwise rollback the transaction.
(tran_type should be a character variable and can take two possible values – ‘c’ for credit and ‘d’ for debit)
viewBalance(acct_no) – call the displayBalance()

3.      Include the main method and create an object for Bank and invoke the methods.


Additional Program:
1.     Create a class called Contact with the following data members:
Contact Name          , Office Phone No, Residence Phone No, Mobile No, Email Id, and Address. Include methods to create, edit and delete contact information. Write a Java program to use the contact class to store and maintain contact information.

Points to Remember
4      A class is a template for an object
4      An object is an instance of a  class
4      A class contains instance variables and methods
4      Data members of a class are called instance variables because there exists a copy of those variables for each instance of the class.
4      Access to class members are restricted by the use of access specifiers.

Program # 2
Overloading and Overriding

Aim : To understand and apply the object-oriented principles of overloading and overriding in a Java program.

Concept:
#1 Overloading:  Two methods in the same class are said to be overloaded if they have the same name but differ in the number and/or type of parameters. This is one way of achieving polymorphism in Java.

#2 Overriding: In a class hierarchy when a method in the subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. Method overriding forms the basis for dynamic method dispatch.

Procedure:
#1 Overloading:
Modify the Bank class created earlier to include Cheque transactions and write a Java program to perform bank operations.

Procedure:
1.      Create a class called Cheque with the following members.
ChequeNo, accountNumber, payeeName , amountInFigures, amountInWords, dateOfIssue, issuingBank

Have methods to set and get these values

2.      Create a class called Transaction that records the transactions in a bank such as deposit and withdrawal by means of cash and cheque. The class should have the following members
TransactionId, AccountNumber, Date(date of transaction-current date), Particulars(Cash/Cheque), Debit/Credit, Amount(amount of transaction), Balance

3.      Add the following methods to the Bank class.
a.      The deposit method for cash transactions should accept the account Number and amount as parameters and update the Account balance.
b.      The deposit method for cheque transactions must accept a Cheque object as parameter and update the Account balance.
c.       The withdrawal method for cash transactions should accept the account Number and amount as parameters check if sufficient balance will be available if withdrawal is permitted and then deduct the amount from the balance, denying the transaction otherwise.
d.      The withdrawal method for cheque transactions should accept a Cheque object as parameter, check if the account Number is valid and if sufficient amount is available in the account and then deduct the amount from the balance.
e.      The deposit and withdrawal methods should also update the Transaction details by creating a Transaction object with the necessary details.
f.        Include a method to view the last 5 transactions for a particular account.
g.      Write a main method and create an object for Bank class and perform the various operations. Obtain the transaction details such as account Number, amount of transaction, cheque details etc., and pass them as parameters to the corresponding methods.

Points to remember:
4      Method overloading is one of the ways to achieve polymorphism in Java
4      Overloaded methods differ in the type and/or number of parameters
4      Return type alone is not sufficient to distinguish two versions of a method
4      Constructors can also be overloaded
4      Overridden methods allow Java to support run-time polymorphism.
4      Overridden methods should have the same method signature

Program #3
Packages and Interfaces
Aim: To write a Java program using the interfaces to declare functionality and packages to include related classes.

Procedure
1.     Create an interface, CardTransactions to represent the operations on debit/credit cards. Declare abstract methods acceptPayment(), validateCard(), checkCardValidity(), checkAmountAvailabilty().
2.     Implement these interfaces in two classes, DebitCardTransactions and CreditCardTransactions and override the methods in the CardTransactions interface.
3.     The validate card method must accept the card type(Visa, Master or American Express) and card number run an algorithm to check if the card number is valid. It should then call the checkCardValidity() method with the expiry date as parameter. The checkCardValidity() method will return true if the expiry date is greater than the current date and false otherwise.
4.     The acceptPayment() method should get the amount of transaction and check if sufficient funds are available. For credit card it should check if the amount is less than the credit limit and for debit card transactions it should check if the amount is less than the balance in the account. These validations are done by the checkAmountAvailability() method.
5.     Create two classes - CreditCard and DebitCard with necessary instance variables and methods to access and modify them.
6.     Create a package called com.mca.bank.card and place the CreditCard and DebitCard classes into it.
7.     Create a package called com.mca.bank.cardtransaction and place the CardTransactions interface and CreditCardTransactions and DebitCardTransactions classes into it.
8.     Write a program in Java to perform operations on credit and debit cards.

No comments: