Sunday 31 July 2011

JAVA NEXT LAB PROGRAMS

Program Questions:
Develop a Java package with simple Stack and Queue classes. Use JavaDoc
comments for documentation.
ALGORITHM:

step 1:Define package pack1 with the keyword package.
step 2:Define the class ArrayQueue with the integers.
step 3:Using a 1 argument constructor,the max size of the queue is assigned.
step 4:Define the method insert() with an item in object type as argument to insert an item.
step 5:Define the method remove() to delete an item from the queue.
step 6:Define the method peek() to return the item which is at the top of stack.
Step 7:Define the method displayAll() to display the contents of the queue.
step 8:Terminate the class .Create an another package pack1 using the keyword package.
step 9:Define the class ArrayStack with an object array'a' and an integer top as datamembers.
step 10:Using a one argument constructor define the size of the array.
step 11:Define the method push() to push an item into the stack.
step 12:Define the method pop() to delete an item from the stack.
step 13:Define the method peek() to return the item at the top of the stack.
step 14:Terminate the class.
step 15:Import the package pack1,define the class StackQueueDemo.
step 16:Define the main() in the class.
Create objects for the classes ArrayStack and ArrayQueue and access the methods using the objects.
step 17:Display the result.
step 18: terminte the main() and terminate the class.

Saturday 16 July 2011

19.07.2011 and 21.07.2011 LAB PROGRAMS


UNIVERSITY SYLLABUS:
EX NO:2
1.Design a Date class similar to the one provided  in the java.util package.
(First you should finish that progam,then only you can go to next program):
(Search the Date class methods in Internet  and try.First define get method and set the method value into setmethod.Dvelope a code to check whether the given date is actual date or before or  after the date.Here the algorithm for DATE class program.)
ALGORITHIM:
step 1:Define the class MyDate with members date,month,year,hour,min,sec.
step 2:USing a default constructor,set the values using the predefined Dateclass.
step 3:use parameterised constructors to set the values for the members.
step 4:Define the methods getYear(),getMonth(),getDate(),getHours(),getMinutes(),getSeconds to return the year,month,date,hours,minutes and seconds.
step 5:Define the methods setYear(),setMonth(),setDate(),setHours(),setMinutes(),setSeconds() to set the values for year,month,date,hours,minutes and seconds.and also get the date,year,month,time,sec using Scanner.
step 6:Define the method after() with the return type boolean to check whether the given date is after the current date.
step 7:Define the method before() with the return type boolean to check whether the given date is before the current date.
step 8:Define the method compareTo() with the return type integer to compare two dates.it will return 0 if both are same,will return 1 if date is after currrent date else it returns -1.
step 9:terminate the class.                                                 
step 10:Define the datetest and define the main() in it.
step 11:Create many objects for the class MyDate and access the methods of the class using different objects.and compare the date to display the result.
step 12:Display the result.
step 13:terminate the main() and class.
OTHER LAB PROGRAMS:
1.Sting s1=”Hello”; Sring s2=”Hello”;Sring s3=”Good-Bye”;String s4=”HELLO”;
Apply all String comparision methods to the value of s.

2.String s=”Now is the time for all good men”+”to come to the aid of country.”;
Apply all String searching method to the value of s.

3.String s=”APPLY ALL STRING MODIFICATION METHOD:”;
Apply all String modification methods  to the value of s.

4.Write a java program to check whether the String  is palindrome or not.

5.Display the following output :
S1=hello
S2=Java world
S3=world
S4=hello
Input:
Char carry[]={‘J’,’a’,’v’,’a’,’w’,’o’,’r’,’l’,’d’};
Apply any string operations.

6.Display the following output using arraycopy method.
0:2001
1:2002
2:2003
3:5
4:7
5:11
6:13

Input:
Int[] first={2,3,5,7,11,13};
Int[] second={2001,2002,2003,2004,2005,2006,2007};

7.Develop a program to sorting the following array elements:
a){45,65,12,32,1,23,39,4,8,3}
b){46,78,34,43,23,22,11,55,23,111}

8.Write a java program  to convert  the value “Programmer” in the string variable convert to “Programming”.
9.Write a java program to search for a number in the given array using binary search.
10.Diaplay the output for the following program:
public class StringsDemo3 {
 
        public static void main(String[] args) {
               String str1 = "My name is bob";
               char str2[] = new char[str1.length()];
               String str3 = "bob";
               String str4 = "cob";
               String str5 = "BoB";
               String str6 = "bob";
               System.out.println("Length of the String str1 : " + str1.length());
               System.out.println("Character at position 3 is : "
                               + str1.charAt(3));
               str1.getChars(0, str1.length(), str2, 0);
               System.out.print("The String str2 is : ");
               for (int i = 0; i < str2.length; i++) {
                       System.out.print(str2[i]);
               }
               System.out.println();
               System.out.print("Comparision Test : ");
               if (str3.compareTo(str4) < 0) {
                       System.out.print(str3 + " < " + str4);
               } else if (str3.compareTo(str4) > 0) {
                       System.out.print(str3 + " > " + str4);
               } else {
                       System.out.print(str3 + " equals " + str4);
               }
               System.out.println();
               System.out.print("Equals Test");
               System.out.println("str3.equalsIgnoreCase(5) : "
                               + str3.equalsIgnoreCase(str5));
               System.out.println("str3.equals(6) : " + str3.equals(str6));
               System.out.println("str1.equals(3) : " + str1.equals(str3));
               str5.toUpperCase(); //Strings are immutable
               System.out.println("str5 : " + str5);
               String temp = str5.toUpperCase();
               System.out.println("str5 Uppercase: " + temp);
               temp = str1.toLowerCase();
               System.out.println("str1 Lowercase: " + str1);
               System.out.println("str1.concat(str4): " + str1.concat(str4));
               String str7temp = "  \t\n Now for some Search and Replace Examples    ";
               String str7 = str7temp.trim();
               System.out.println("str7 : " + str7);
               String newStr = str7.replace('s', 'T');
               System.out.println("newStr : " + newStr);
               System.out.println("indexof Operations on Strings");
               System.out.println("Index of p in " + str7 + " : "
                               + str7.indexOf('p'));
               System.out.println("Index of for in " + str7 + " : "
                               + str7.indexOf("for"));
               System.out.println("str7.indexOf(for, 30) : "
                               + str7.indexOf("for", 30));
               System.out.println("str7.indexOf('p', 30) : "
                               + str7.indexOf('p', 30));
               System.out.println("str7.lastIndexOf('p') : "
                               + str7.lastIndexOf('p'));
               System.out.println("str7.lastIndexOf('p', 4) : "
                               + str7.lastIndexOf('p', 4));
               System.out.print("SubString Operations on Strings");
               String str8 = "SubString Example";
               String sub5 = str8.substring(5); // "ring Example"
               String sub3_6 = str8.substring(3, 6); // "Str"
               System.out.println("str8 : " + str8);
               System.out.println("str8.substring(5) : " + sub5);
               System.out.println("str8.substring(3,6) : " + sub3_6);
        }
}

11.Try the programs for all array methods.(Try Example Program for every methods)
12.Try the programs for StrinBuilder class methods. _________________________________________________________________________________
Note:
1.Finish all bending lab programs.
2.Finish  all class work examples.
3.Study for VIVA.

Sunday 10 July 2011

TOMORROW LAB PROGRAMS


ARRAYS EXAMPLE PROGRAMS:

1)Display the output for the following programs:
a)
class aa
{
public static void main(String args[])
{
int i;
int a[]=new int[10];
for(i=0;i<a.length;i++)
{
a[i]=i+1;
}
System.out.println(""+a[i]+"");
}}

b)
public class all
{
  public static void main(String[] args)
  {

int[][] a2 = new int[10][5];

 for (int i=0; i<a2.length; i++) {
     for (int j=0; j<a2[i].length; j++) {
a2[i][j]=i;
         System.out.print(" " + a2[i][j]);
     }
     System.out.println("");
 }

  }
}
c)
class anarry {
     public static void main(String[] args) {
          int[] anArray={900,1000};      // allocates memory for 10 integers
          anArray[0] = 900;
         anArray[1] = 1000;

          System.out.println("Element at index 1: " + anArray[0]);
         System.out.println("Element at index 2: " + anArray[1]);
     }
}
d) class arr
{
public static void main(String args[])
{

String months[] =
         {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
          "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
      
          for (int i = 0; i < months.length; i++ ) {
          System.out.println("month: " +months[i]);

}
}
}

e)
class arry
{
int num[];
arry()
{
num=new int[10];
for(int i=0;i<num.length;i++)
for(int j=0;j<num.length;j++)
{

num[i]=i+5;
}}
void print()
{

System.out.println("Numbersare");
for(int i=0;i<num.length;i++)
{
System.out.println(num[i]);
}
}
public static void main(String args[])
{
arry s=new arry();
s.print();
}
}

f)
class arrys
{
int num[][];
arrys()
{
num=new int[2][2];
for(int i=0;i<num.length;i++)
for(int j=0;j<num.length;j++)

num[i][j]=(i+1)*(j+1);
}
void print()
{
System.out.println("Numbersare");
for(int i=0;i<num.length;i++)
{
for(int j=0;j<num.length;j++)


System.out.println(""+num[i][j]+"");
}
}
public static void main(String args[])
{
arrys s=new arrys();
s.print();
}
}

g)
import java.util.*;
class b
{
int a[]=new int[5];
int b[]=new int[5];
int sum;
Scanner ac=new Scanner(System.in);
void dis()
{
System.out.println("Enter the a values");
System.out.println("Enter the b values");

for(int i=0;i<=4;i++)
{

a[i]=ac.nextInt();
b[i]=ac.nextInt();
}
}

void pus()
{
for(int i=0;i<=4;i++)
{
sum=a[i]+b[i];
System.out.println("Numbers are"+sum);
}

}
public static void main(String args[])
{
b aaa=new b();
aaa.dis();
aaa.pus();
}
}

h)
public class copy{
  public static void main(String[] args){
 char[] copyfrom = {'a','b','c','d','e','f','g','h','i','j'};
  char[] copyTo = new char[6];
  System.arraycopy(copyfrom, 2, copyTo, 0, 6);
  System.out.println(new String (copyTo));
  }
}

i)
class multi {
    public static void main(String[] args) {
        String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
                            {"Smith", "Jones"}};
        System.out.println(names[0][0] + names[1][0]); //Mr. Smith
        System.out.println(names[0][2] + names[1][1]); //Ms. Jones
   
System.out.println(names.length);}
}

j)
class sam
{
int num[];
sam()
{
num=new int[5];
int n[]={1,2,3,4,5};
num=n;
}
void print()
{
System.out.println("NUMBERS ARE");
for(int i=0;i<num.length;i++)
System.out.println(num[i]);
}
public static void main(String args[])
{
sam s=new sam();
s.print();
}
}

k)
public class Sum
{
  public static void main(String[] args)
  {
  int[] x = new int [10];
  for (int i = 0; i<x.length; i++ )
  x[i] = i;
  int sum = 0;
  for(int i = 0; i<x.length; i++)
  sum += x[i];
  System.out.println(sum);
  }
}

l)
import java.util.*;
class name
{
int no[]=new int[2];
String names[]=new String[2];
Scanner ac=new Scanner(System.in);
void getdata()
{
System.out.println("Enter the Name and Rollno values");
for(int i=0;i<2;i++)
{
no[i]=ac.nextInt();
names[i]=ac.next();
}
}
void putdata()
{
System.out.println("Roll nO\t Name\n");
for(int i=0;i<2;i++)
{
System.out.println(no[i]+"\t"+names[i]+"\n");

}}
public static void main(String args[])
{
name n=new name();
n.getdata();
n.putdata();
}
}

m)
public class CopyArray {
  public static void main(String[] args) {
  int array1[]= {2,3,4,5,8,9};
  int array2[] = new int[6];
  System.out.println("array:");
  System.out.print("[");
  for (int i=0; i<array1.length; i++){
  System.out.print(" "+array1[i]);
  }
  System.out.print("]");
  System.out.println("\narray1:");
  System.out.print("[");
  for(int j=0; j<array1.length; j++){
  array2[j] = array1[j];
  System.out.print(" "+ array2[j]);
  }
  System.out.print("]");
  }
}


n)
public class ArrayAverage{
  public static void main(String[] args) {
  int nums[]={5,4,3,2,1};
  int result=0;
  int i=0;
  for(i=0; i < nums.length; i++){
  result=result + nums[i];
  }
  System.out.println("Average is =" + result/nums.length);
  }
}

o)
class MatrixExample{
  public static void main(String[] args)  {
  int array[][]= {{1,3,5},{2,4,6}};
  System.out.println("Row size= " + array.length);
  System.out.println("Column size = " + array[1].length);
  outputArray(array);
  }
  
 public static void outputArray(int[][] array) {
 int rowSize = array.length;
 int columnSize = array[0].length;
 for(int i = 0; i <= 1; i++) {
 System.out.print("[");
 for(int j = 0; j <= 2; j++) {
 System.out.print(" " + array[i][j]);
 }
 System.out.println(" ]");
 }
 System.out.println();
 }
}

2.Develop a java program to perform Matrix addition  using java arrays.
  Tips:
[we are going to calculate the sum of two matrix and containing its rows and columns. See below for better understanding to this.
In this program we are going to calculate the sum of two matrix. To make this program, we need to declare two dimensional array of type integer. Firstly it calculates the length of the both the arrays. Now we need to make a matrix out of it. To make the matrix we will use the for loop. By making use of the for loop the rows and column will get divide. This process will be performed again for creating the second matrix.
After getting both the matrix with us, we need to sum both the matrix. The both matrix will be added by using the for loop with array[i][j]+array1[i][j]. The output will be displayed by using the println() method.]

3.Develop a java program to perform Matrix Subtraction using java arrays.
4.Develop a java program to perform Matrix Multiplication  using java arrays.
5.Deveop a  java program for implementing the two dimensional array program and its square.
[Tips:
We are going to display the square of two matrix. Firstly, we have to define a class "SquareMatrix".  Then we take an integer type array that contains integer type values. After this, we use two 'for' loop that denotes rows and columns of a matrix. After getting both the matrix with us we need to square both matrix. When we go to square this array then we use "square[i][j] =square[i][j] * square[i][j]". So, the output will be displayed on the screen command prompt by using the println() method.]


Tuesday 5 July 2011

JAVA SOFTWARE DOWNLOAD

JAVA SOFTWARE DOWNLOADS

Dear students you can download the java software with  following web sites.
Download and install the java.. And do the programs in your home.
just click once.


http://www.java.com/en/download/

http://www.oracle.com/technetwork/java/javase/downloads/index.html..

http://www.oracle.com/technetwork/indexes/downloads/index.html

Labels: 

JAVA POLYMORPHISM


Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Any java object that can pass more than on IS-A test is considered to be polymorphic. In Java, all java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared the type of a reference variable cannot be changed.
The reference variable can be reassigned to other objects provided that it is not declared final. The type of the reference variable would determine the methods that it can invoke on the object.
A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type.

Example:

Let us look at an example.
public interface Vegetarian{}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}
Now the Deer class is considered to be polymorphic since this has multiple inheritance. Following are true for the above example:
  • A Deer IS-A aAnimal
  • A Deer IS-A Vegetarian
  • A Deer IS-A Deer
  • A Deer IS-A Object
When we apply the reference variable facts to a Deer object reference, the following declarations are legal:
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;
All the reference variables d,a,v,o refer to the same Deer object in the heap.

Virtual Methods:

In this section, I will show you how the behavior of overridden methods in Java allows you to take advantage of polymorphism when designing your classes.
We already have discussed method overriding, where a child class can override a method in its parent. An overridden method is essentially hidden in the parent class, and is not invoked unless the child class uses the super keyword within the overriding method.
/* File name : Employee.java */
public class Employee
{
   private String name;
   private String address;
   private int number;
   public Employee(String name, String address, int number)
   {
      System.out.println("Constructing an Employee");
      this.name = name;
      this.address = address;
      this.number = number;
   }
   public void mailCheck()
   {
      System.out.println("Mailing a check to " + this.name
       + " " + this.address);
   }
   public String toString()
   {
      return name + " " + address + " " + number;
   }
   public String getName()
   {
      return name;
   }
   public String getAddress()
   {
      return address;
   }
   public void setAddress(String newAddress)
  {
      address = newAddress;
  }
  public int getNumber()
  {
     return number;
  }
}
Now suppose we extend Employee class as follows:
/* File name : Salary.java */
public class Salary extends Employee
{
   private double salary; //Annual salary
   public Salary(String name, String address, int number, double
      salary)
   {
       super(name, address, number);
       setSalary(salary);
   }
   public void mailCheck()
  {
       System.out.println("Within mailCheck of Salary class ");
       System.out.println("Mailing check to " + getName()
       + " with salary " + salary);
   }
   public double getSalary()
   {
       return salary;
   }
   public void setSalary(double newSalary)
   {
       if(newSalary >= 0.0)
       {
          salary = newSalary;
       }
   }
   public double computePay()
   {
      System.out.println("Computing salary pay for " + getName());
      return salary/52;
   }
}
Now you study the following program carefully and try to determine its output:
/* File name : VirtualDemo.java */
public class VirtualDemo
{
   public static void main(String [] args)
   {
      Salary s = new Salary("Mohd Mohtashim", "Ambehta,  UP",
                                 3, 3600.00);
      Employee e = new Salary("John Adams", "Boston, MA",
                                 2, 2400.00);
      System.out.println("Call mailCheck using 
                                   Salary reference --");
      s.mailCheck();
      System.out.println("\n Call mailCheck using 
                                   Employee reference--");
      e.mailCheck();
    }
}
This would produce following result:
Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary 3600.0

Call mailCheck using Employee reference--
Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.0
Here we instantiate two Salary objects . one using a Salary reference s, and the other using an Employee reference e.
While invoking s.mailCheck() the compiler sees mailCheck() in the Salary class at compile time, and the JVM invokes mailCheck() in the Salary class at run time.
Invoking mailCheck() on e is quite different because e is an Employee reference. When the compiler seese.mailCheck(), the compiler sees the mailCheck() method in the Employee class.
Here, at compile time, the compiler used mailCheck() in Employee to validate this statement. At run time, however, the JVM invokes mailCheck() in the Salary class.
This behavior is referred to as virtual method invocation, and the methods are referred to as virtual methods. All methods in Java behave in this manner, whereby an overridden method is invoked at run time, no matter what data type the reference is that was used in the source code at compile time.

Java Inheritance


Inheritance can be defined as the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.
When we talk about inheritance the most commonly used keyword would be extends andimplements. These words would determine whether one object IS-A type of another. By using these keywords we can make one object acquire the properties of another object.

IS-A Relationship:

IS-A is a way of saying : This object is a type of that object. Let us see how the extendskeyword is used to achieve inheritance.
public class Animal{
}

public class Mammal extends Animal{
}

public class Reptile extends Animal{
}

public class Dog extends Mammal{
}
Now based on the above example, In Object Oriented terms following are true:
  • Animal is the superclass of Mammal class.
  • Animal is the superclass of Reptile class.
  • Mammal and Reptile are sub classes of Animal class.
  • Dog is the subclass of both Mammal and Animal classes.
Now if we consider the IS-A relationship we can say:
  • Mammal IS-A Animal
  • Reptile IS-A Animal
  • Dog IS-A Mammal
  • Hence : Dog IS-A Animal as well
With use of the extends keyword the subclasses will be able to inherit all the properties of the superclass except for the private properties of the superclass.
We can assure that Mammal is actually an Animal with the use of the instance operator.

Example:

public class Dog extends Mammal{
   public static void main(String args[]){

      Animal a = new Animal();
      Mammal m = new Mammal();
      Dog d = new Dog();

      System.out.println(m instanceof Animal);
      System.out.println(d instanceof Mammal);
      System.out.println(d instanceof Animal);
   }
}
This would produce following result:
true
true
true
Since we have a good understanding of the extends keyword let us look into how the implements keyword is used to get the IS-A relationship.
The implements keyword is used by classes by inherit from interfaces. Interfaces can never be extended.

Example:

public interface Animal {}

public class Mammal implements Animal{
}

public class Dog extends Mammal{
}

The instanceof Keyword:

Let us use the instanceof operator to check determine whether Mammal is actually an Animal, and dog is actually an Animal
interface Animal{}

class Mammal implements Animal{}

class Dog extends Mammal{
   public static void main(String args[]){

      Mammal m = new Mammal();
      Dog d = new Dog();

      System.out.println(m instanceof Animal);
      System.out.println(d instanceof Mammal);
      System.out.println(d instanceof Animal);
   }
} 
This would produce following result:
true
true
true

HAS-A relationship:

These relationships are mainly based on the usage. This determines whether a certain classHAS-A certain thing. This relationship helps to reduce duplication of code as well as bugs.
Lets us look into an example:
public class Vehicle{}
public class Speed{}
public class Van extends Vehicle{
 private Speed sp;
} 
This shows that class Van HAS-A Speed. By having a separate class for Speed we do not have to put the entire code that belongs to speed inside the Van class., which makes it possible to reuse the Speed class in multiple applications.
In Object Oriented feature the users do not need to bother about which object is doing the real work. To achieve this, the Van class hides the implementation details from the users of the Van class. SO basically what happens is the users would ask the Van class to do a certain action and the Vann class will either do the work by itself or ask another class to perform the action.
A very important fact to remember is that Java only supports only single inheritance. This means that a class cannot extend more than one class. Therefore following is illegal:
public class extends Animal, Mammal{} 
However a class can implement one or more interfaces. This has made Java get rid of the impossibility of multiple inheritance