(ii) Multiple Dimensional Array.
In Java, arrays can be of either types, primitive data types or reference types. In primitive data type array, all elements are of a definite primitive data type. In references type array all elements are of definite reference type.
<data.type> <array - name>[]; //One Dimensional Array
Syntax to create an array:
<array - name> = new <data-type> [<array-size>]; //One dimensional Array
Initialization of Array : Initialization means to assign the value to the array at the time of constructing and declaring in one statement simultaneously.
<data - type> [] <array - name> = { <initial value list>};
For example :
int an_Array = {1,2,3,4,5,6,7,8,9 }; //One dimensional Array
for(int i=0;i<3;i++)
System.out.println(anArray[i]); //Accessing ith index Value.
int matrix[] [] = {{1,2,3},{4,5,6,},{7,8,9}}; //Two dimensional Array
for (int i=0; i<3; i++)
{ for (int j=0; j=3; j++)
System.out.print(matrix[i][j]);
System.out.println();
}
Inheritance : Inheritance is one of the most important and useful feature of object-oriented programming. There is a main class, called base class or super class, from which a new class id derived, called derived class or sub class. Base class is also called parent class and the derived class is called child class. Inheritance provides a mechanism for deriving new classes from existing ones. In OOP, ther are several type of mechanisms:
(1) Single Inheritance
(2) Multiple Inheritance
(3) Multilevel Inheritance
(4) Hybrid Inheritance
In Java a class can be derived from another class using the general form :
access_specifier class Subclass extends Superclass
{ //Derived class data and methods
}
Example:
class A {
int x;
int y;
int get(int p, int q) {
x = p; y = q; return(0);
}
void show() {
System.out.println(x);
}
}
class B extends A {
void showb() {
System.out.println("B");
}
}
class c extends B {
void display() {
System.out.println("C");
}
}
public static void main(strings args[]) {
A a = new A();
a.get(5,6);
a.show();
}
}
Exception Handling : An exception is a run-time error or an anomalous condition that arises in the program during run time. A Java exception is an object that describes an abnormal condition in the program code. When an abnormal condition is encountered in the object code, an object representing that condition or exception is created an thrown in the method that caused the error. The method can then make the choice to handle the exception itself or pass it on. An exception may also be caught and processed. Exception can also be manually generated by your program code. Exceptions thrown by Java programs are actually the errors that violate the constraints of Java runtime. In Java, exception handling
- try
- catch
- throw
- throws
- finally
The program statements to be monitored for exceptions are kept within a 'try block'. If an exception occurs in the 'try block', then it is thrown. The statements that have to be thrown manually are kept in the 'catch block'. System generated exceptions are automatically thrown by Java runtime environment. The general form of exception handling is :
try
{ //block of code to be monitored
}
catch (Exception_TypeA obj)
{ //exception handler for Exception_TypeA
}
Finally
{ //block of code to be executed
}
All types of exceptions are the subclass of built-in class Throwable. Throwable contains two subclasses, one is exception for the abnormal conditions that the user programs must handle. Exceptions include conditions like divide by zero. This type of exception is called Arithmatic Exception. Another subclass is error that contains the conditions that are not expected to be caught under normal conditions.
Example:
class Multiple_Catch
{
Public static void main(String args[])
{
try
{
int x=0;
int y=y/x;
int a[] = {6};
a[89] = 44;
}
catch (Arithmetic Exception e1)
{
System.out.println("ERROR : Divide by Zero" +e1);
}
catch (Array Index Of Bound Exception e2)
{
System.out.println("ERROR : Array out of Bound" +e2);
}
}
}
Q.2. Explain the concept of E-Mail server.
Ans. E-Mail server : A socket will be created, connect to port 25, which is SMTP port number. Simple mail transfer Protocol describes the format in which e-mail message will be sent. In UNIX OS, sendmail daemon was already implemented to support this features.
Example :
import Java.io.BufferedReader;
import Java.io.FileReader;
import Java.io.IOException;
import Java.io.InputStream;
import Java.io.InputStreamReader;
import Java.io.OutputStream;
import Java.io.OutputStreamWriter;
import Java.io.PrintWriter;
import Java.net.InetAddress;
import Java.net.Socket;
import Java.net.UnknownHostException;
public class SMTPDemo {
public static void main (String args[] throws IOException, UnknownHostException {
String msgFile = "file.txt";
String from = "java2s@java2s.com";
String to = "yourEmail@yourServer.com";
String mailHost = "yourHost";
SMTP mail = new SMTP(mailHost);
if(mail != null) {
if (mail.send(new FileReader (msgFile), from, to)) {
System.out.println("Mail sent.");
} else {
System.out.println("Connect to SMTP sever failed !");
}
}
System.out.pritnln("Done");
}
static class SMTP {
private final static int SMTP_PORT = 25;
Inetdderess mailHost;
Inetddress localHost;
BufferedReader in;
Public SMTP (String host) throws UnknownHostException {
mailHost = InetAddress.getByName (host);
localHost = InetAddress.getLocalHost ( );
System.out.println("mailhost = "+ mailHost );
System.out.println("localhost = "+ localHost);
System.out.println("SMTP construtor done\n");
}
public boolean send (FileReader msgFileReader, String from, String to ) throws IOException {
Socket smtpPipe;
InputStream inn;
OutputStream outt;
BufferedReader msg;
msg = new BufferedReader(msgFileReader);
smtpPipe = new Socket(mailHost, SMTP_PORT);
if (smtpPipe == null) {
return false; }
inn = smtpPipe.getInputStream( );
outt = smtpPipe.getOutputStream( );
in = new BufferedReader(new InputStreamReader(inn));
out = new PrintWriter(new OutputStreamWriter(outt), true);
if (inn = = null \\ out= = null) {
System.out.println("Failed to open streams to socket.");
return false;
}
StringinitialID = in.read.Line( );
System,out.println(initialID);
System.out.println("HELO " + localhost.getHostName( ));
out.println("HELO " + localhost.getHostName( ));
String welcome = in.readLine ( );
System.out.println(welcome);
System.out.println("MAIL From:<" + from + ">");
out.println("MAIL From:<" + from + ">");
String senderOK = in.readLine( );
System.out.println(senderOK);
System,out.println("RCPT TO:<" + to + ">");
out.println("RCPT TO:<" + to + ">");
String RecipientOK = in.readLine( );
System.out.println(recipientOK);
System.out.println("DATA");
out.println("DATA");
String line;
While((line = msg.readLine ( )) != null ) {
out.println(line);
}
System.out.println(".");
out.println(".");
String acceptedOK = in.readLine( );
System.out.println(acceptedOK);
System.out.println("QUIT");
out.println("QUIT");
return true;
}
}
}