Java_Unit-II_Part_I

Click Here for Java_Unit-II_Part_II

1.What is an array? Explain Array types used in java with example?

Ans:

An array represents a group of elements of same data type. It can store a group of elements.

In java arrays are created on dynamic memory i.e. allocated at runtime by JVM.

Arrays are generally categorized into two parts as follows:

 Single dimensional arrays.

 Multi dimensional arrays.

Single dimensional arrays:-

 A list of items can be given one variable name using only one subscript. Such a variable is called single dimensional array.

 Arrays must be declared and created in the computer memory before they are used

 Creation of array involves 3 steps:

1) Declaring the array

2) Creating memory locations

3) Initialization of values into the memory locations.

Declaring the array:

Arrays in java may be declared in two forms:

type arrayname[ ];

(or)

type[ ] arrayname[ ];

Ex:-

int counter[ ];

int[ ] counter;

Creating memory location to array:

After declaration of arrays, memory locations are created by new operator.

arrayname = new type[size];

Ex: counter =new int[5];

These lines create necessary memory locations for the array counter. It is also possible to combine the two steps declaration and creation of memory location as follows:

int number[ ] = new int[5];

Initialization of arrays:-

 Putting values into the array is known a “initialization”. This is done as follows:

arrayname[subscript]=value;

Ex:-

counter[0]=35;

 Java creates arrays starting with the subscript of 0 and ends with a value one less than the size specified.

 Arrays can also be initialized automatically when they are declared, as shown below:

Syntax:

 type arrayname[ ]={list of values};

Ex:-

int number[ ]={5,4,1,8,9};

In the above example array size is not given, the compiler allocates enough space for all elements specified in the list.

Multi-dimensional arrays:

Multidimensional arrays can represent the data in the form of rows and columns i.e. by taking two or more indexes.

Multidimensional arrays include double dimensional and 3 dimensional arrays.

2-Dimensional Arrays: 2-Dimensional Arrays can represent the data in the form of rows and columns i.e. by taking two indexes. Generally, these are used to work with matrices.

In java, a double dimensional array can be declared as follows:

int a[][] = new int[5][5];

Advantages of arrays:

 It is capable of storing many elements at a time

 It allows random accessing of elements i.e. any element of the array can be randomly accessed using indexes.

Disadvantages:

 Predetermining the size of the array is a must.

 There is a chance of memory wastage or shortage.

 To delete one element in the array, we need to traverse throughout the array.

Matrix Multiplication(Two-Dimensional Array):

import java.io.*;

class matrixmult

{

public static void main(String args[]) throws IOException

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int m1[][]=new int[3][3];

int m2[][]=new int[3][3];

int m3[][]=new int[3][3];

int i,j,k;

System.out.println("Enter elements of first matrix ");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

m1[i][j]=Integer.parseInt(br.readLine());

}

}

System.out.println("Enter elements of second matrix ");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

m2[i][j]=Integer.parseInt(br.readLine());

}

}

System.out.println(" elements of first matrix are ....");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

System.out.print(m1[i][j]);

}

System.out.println();

}

System.out.println("Elements of second matrix ");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

System.out.print(m2[i][j]);

}

System.out.println();

}

/*Matrix multiplication*/

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

m3[i][j]=0;

for(k=0;k<3;k++)

{

//m3[i][j]+=m1[i][k]*m2[k][j];

m3[i][j]=m3[i][j]+m1[i][k]*m2[k][j];

}

}

}

System.out.println("matrix multiplication result is ");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

System.out.print(m3[i][j] +" ");

}

System.out.println();

}

}

}

class VectorTest

{

public static void main(String as[])

{

Vector v1=new Vector(10);

v1.add(“10”);

v1.add(“20”);

v1.add(“30”);

System.out.println("Number of Elements:"+v.size());

System.out.println("Vector Elements:"+v1);

}

}




 2. What are Command-Line Arguments? How are they used in Java?

Ans:

Command line arguments are the parameters passed to the executable java program in the form of strings. Command line arguments are collected in main function itself. The argument is an array of strings and can collect the list of strings. File name is not included in the list of arguments as in C language. This is very useful to receive quick inputs.

class Test

{

public static void main(String args[])

{

System.out.println(“Number of arguments=”+as.length);

for(int i=0;i<as.length;i++)</as.length;i++)

System.out.println(as[i]);

}

}

Execute the above program as follows.

java Test 1 2 3

The above command produces the following output:

Number of arguments=3

1

2

3


3.Explain about inheritance in Java?

Ans:

Java classes can be reused in several ways. This is basically done by creating new classes, reusing the properties of existing ones. The mechanism of deriving a new class from old one is called inheritance. The old class is known as base class or super class or parent class and the new one is called the subclass or derived class or child class.

The inheritance allows subclasses to inherit all the variables and the methods of their parent classes.

Single inheritance: If a class is derived from another class, it can be called as single inheritance.


Multiple inheritance: If a class is derived from two or more classes, it can be called as multiple inheritance. Java does not support this type of inheritance.

Multilevel inheritance: If a class is derived from the class, which is derived from another class; it can be called as multilevel inheritance.

Defining a subclass: A subclass is defined as follows:

class subclass extends superclass

{

Variable declarations;

Methods declarations;

}

The keyword extends signifies that the properties of the superclass are extended to the subclass. The subclass will now contain its own variables and methods as well those of the superclass. This kind of situation occurs when we want to add some more properties.


4.Explain Method overriding?

Method overriding:-

A method defined in a super class can be inherited by its subclass and is used by the objects created by its subclass. Method inheritance enables us to define and use methods repeatedly in subclasses without having to define the methods again in the subclass. However, there may be occasions when an object to respond to the same method but have different behavior when that method is called. That means, the super-class method should be override. This is possible by defining a method in the subclass that has the same name, same arguments and same return type as a method in the super-class. When that method is called, the method defined in the subclass is invoked and executed instead of the one in the super-class. This is known as overriding.

class A

{

int i, j;

A(int a, int b) {

i = a;

j = b;

}

void show() {

System.out.println("i and j: " + i + " " + j);

}

}

class B extends A {

int k;

B(int a, int b, int c) {

super(a, b);

k = c;

}

void show() {

System.out.println("k: " + k);

}

}

class OverrideMethod {

public static void main(String args[]) {

B b = new B(1, 2, 3);

b.show();

A a=new A(10,20);

a.show();

}

}


6.When do we declare a method or class as final?

Ans:

All methods and variables can be overridden by default in subclasses. A class that cannot be sub classed is called final class. This is achieved in Java using the keyword final as follows:

final class A

{

Body of the class

}

Making a method final ensures that the functionality defined in the method will never be altered in any way. Similarly, the value of a final variable can never be changed.

7.When do we declare a method or class as abstract?

Ans:

Java allows the programmer to override a method compulsory. This can be done using the keyword abstract in the method defined.

abstract class Shape

{

……..

……..

abstract void draw();

………

……..

}

When a class contains atleast one abstract method, it should be declared as abstract. When using the abstract classes, we must satisfy the following conditions:

 We cannot use abstract classes to instantiate objects directly. For example Shape s=new Shape() is illegal because Shape is an abstract class.

 The abstract methods of an abstract class must be defined in its subclass.

 We cannot declare abstract constructors or abstract static methods.

8. Define Interfaces.

Ans:

Interfaces: Java provides an approach known as interfaces to support the concept of multiple Inheritance. An interface contains methods and variables. All methods of the interface are public and abstract. Interfaces do not specify any code to implement these methods. Therefore it is the responsibility of the class that implements an interface to define the code for implementation of these methods.

The general form of an interface definition is as follows:

interface InterfaceName

{

Variables declaration;

Methods declaration;

}

An interface variable is public, static and final by default. This means all the variables

of the interface are constants. Methods declaration will contain only a list of methods without anybody statements.

Interfaces can be implemented in two ways:

1. Extending interfaces

Interfaces can also be extended. The sub interface will inherit all the members of the

super interface. This is achieved by using the keyword extends.

interface interfacename2 extends interfacename1

{

body of name2

}

Sub interfaces can not define the methods declared in the super interfaces. So class is

used to implement the derived interface, to define all the methods.

2. Implementing interfaces:-

Class must be defined to implement the code for the method of interface.

class classname implements interfacename

{

body of classname;

}

Here classname “implements” the interface interfacename. A class can implements more than one interface, separated by comma.

Example:

interface Area

{

final static double pi=3.14;

double compute (double x, double y);

}

class Rectangle implements Area

{

public double compute(double x, double y)

{

return(x*y);

}

}

class Circle implements Area

{

public double compute(double x, double y)

{

return(pi*x*x);

}

class InterfaceDemo

{

public static void main(String args[])

{

Rectangle r=new Rectangle();

System.out.println(“Area of rectangle is”+r.compute(10,20));

Circle c=new Circle();

System.out.println(“Area of circle is”+c.compute(5.1,0);

}

}





No comments:

Post a Comment