Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Sunday, 5 February 2017



RPC Program for Simple Calculator





Submitted By: Shreeram

Prof.Mahendra Kanojia

RMI Program to Display Addition of Two Numbers




Submitted By - Mr. Shreeram.


Prof.Mahendra Kanojia

Remote Method Invocation







Prof.Mahendra Kanojia

Transmission Control Protocol
Study of Connection-oriented approach (TCP)
Client-Server program to disply Time



Prof.Mahendra Kanojia


User Datagram Protocol

Study of Connection-less approach using datagram-approach (UDP) 




Prof.Mahendra Kanojia

Monday, 18 February 2013


Program to input numbers from user using array and to calculate their Sum

Code:
/**
 * Program to input numbers from user using array and to calculate their Sum.
 **/
import java.io.*;
public class Arry_Input
{
    public static void main()throws IOException
    {
        try
        {
            int sum =0;
         int ary[] = new int[3];
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         for(int i=0;i<3;i++)
         {
             System.out.print("Enter number "+(i+1)+" ==> ");
             ary[i] = Integer.parseInt(br.readLine());
         }
         for(int i1=0; i1<3; i1++)
         {
            sum = sum+ ary[i1];
         }
         System.out.print("Sum of  numbers ==> "+sum);
    }
    catch(Exception z)
    {
        System.out.println(z.toString());
    }
}
}

OUTPUT
Enter number 1 ==> 10
Enter number 2 ==> 20
Enter number 3 ==> 30
Sum of numbers ==> 60
Prof.Mahendra Kanojia

Program to demonstrate Linear Search Using Arrays

Code:
/**
 * Program to demonstrate Linear Search Using Arrays.
 **/
import java.io.*;
public class Arry_Search
{
     public static void main()throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       System.out.print("Enter the number of elements you want to input ==> ");
       int size = Integer.parseInt(br.readLine());
        int arry[] = new int[size];
        int flag = 0, loc = 0;
        for(int i=0;i<arry.length;i++)
         {
             System.out.print("Enter number "+(i+1)+" ==> ");
             arry[i] = Integer.parseInt(br.readLine());
         }
        System.out.print("Enter number to be searched ==> ");
        int num = Integer.parseInt(br.readLine());
        for(int i = 0; i < arry.length ;i++ )
        {
            if (arry[i] == num)
            {
                flag = 1;
                loc = i;
                break;
            }
        }
        if (flag == 1)
        {
            System.out.println("Number "+num+" found in "+(loc+1)+" location");
        }
        else
        {
            System.out.println("Number not found ");
        }
    }
}

OUTPUT
Enter the number of elements you want to input ==> 4
Enter number 1 ==> 10
Enter number 2 ==> 20
Enter number 3 ==> 30
Enter number 4 ==> 40
Enter number to be searched ==> 30
Number 30 found in 3 location
Prof.Mahendra Kanojia

Friday, 18 January 2013


Program to Demonstrate Typecasting in Java (BlueJ)

Code:
/***** PROGRAM TO DEMONSTRATE TYPECASTING *****/
public class TypeCast
{
    public static void main()
    {
        System.out.println("***** Type Cast Demo *****");
        int i = 10;
        float f = 10.10f;
        double d = 12.12;
        char c = 'a';
        boolean b = true;
       
        int res1 = (int) f; // EXPLICIT TYPE CASTING
        System.out.println("type cast float 10.10 to int ==> "+res1);
        System.out.println("Type cast char "+ c + " to int ==> "+(int)c);
        double resd = i+f; // IMPLICIT TYPE CASTING
        System.out.println("type cast addition of "+i+ " + " + f + " = "+resd);
       
     
    }
}

OUTPUT:
***** Type Cast Demo *****
type cast float 10.10 to int ==> 10
Type cast char a to int ==> 97
type cast addition of 10 + 10.1 = 20.100000381469727
Prof.Mahendra Kanojia

Program to Demonstrate Call by Value and Call by Reference in Java (BlueJ)

A) Program to Demonstrate Call by Reference
code:
/***** PROGRAM TO DEMONSTRATE CALL BY REFERENCE *****/
public class CallByRef
{
   public static  int num1 = 100, num2 = 200;
    public static void main()
    {
         CallByRef cbr = new CallByRef();
        
        System.out.println("***** Program to demonstrate call by reference *****");
        System.out.println("Value of num1 before call to swap :"+cbr.num1);
        System.out.println("Value of num2 before call to swap :"+cbr.num2);
        System.out.println("***********************************************");
      
        swap(cbr); // calling swap method by reference
       
        System.out.println("Value of num1 after call to swap :"+cbr.num1);
        System.out.println("Value of num2 after call to swap :"+cbr.num2);
        System.out.println("***********************************************");
    }
    public static void swap (CallByRef cbr2)
    {
        int temp;
        temp = cbr2.num1;
        cbr2.num1 = cbr2.num2;
        cbr2.num2 = temp;
       
        System.out.println("Value of num1 in swap :"+cbr2.num1);
        System.out.println("Value of num2 in swap :"+cbr2.num2);
        System.out.println("***********************************************");
    }
}

OUTPUT:
***** Program to demonstrate call by value *****
Value of num1 before call to swap :100
Value of num2 before call to swap :200
***********************************************
Value of num1 in swap :200
Value of num2 in swap :100
***********************************************
Value of num1 after call to swap :200
Value of num2 after call to swap :100
***********************************************

A) Program to Demonstrate Call by Value

Code: 
/***** PROGRAM TO DEMONSTRATE CALL BY VALUE *****/
public class CallByVal
{
    public static void main()
    {
        int num1 = 100, num2 = 200;
        System.out.println("***** Program to demonstrate call by value *****");
        System.out.println("Value of num1 before call to swap :"+num1);
        System.out.println("Value of num2 before call to swap :"+num2);
       
        swap(num1, num2); // calling swap method by value
       
        System.out.println("Value of num1 after call to swap :"+num1);
        System.out.println("Value of num2 after call to swap :"+num2);
    }
    public static void swap (int num11,int num22)
    {
        int temp;
        temp = num11;
        num11 = num22;
        num22 = temp;
       
        System.out.println("Value of num1 in swap :"+num11);
        System.out.println("Value of num2 in swap :"+num22);
       
    }
   
}

OUTPUT:
***** Program to demonstrate call by value *****
Value of num1 before call to swap :100
Value of num2 before call to swap :200
Value of num1 in swap :200
Value of num2 in swap :100
Value of num1 after call to swap :100
Value of num2 after call to swap :200
Prof.Mahendra Kanojia

Program to Demonstrate Method Overloading in Java (BlueJ)

Code:
/***** PROGRAM TO DEMONSTRATE METHOD OVERLOADING *****/
public class methodOverloading
{
    public static void main()
    {
        int num1 = 10, num2 = 20;
        float num3 = 10.10f;
       
        methodOverloading call = new methodOverloading();
       
        call.add(num1, num2);
        call.add(num1, num2, 30);
        methodOverloading.add(num1, num3);
        add(30.30f, num2);
       
    }
    public static void add(int no1 , int no2)
    {
        System.out.println("Addition of "+no1+ " and "+no2+ " ==> "+(no1+no2));
    }
    public static void add(int no1 , float no2)
    {
        System.out.println("Addition of "+no1+ " and "+no2+ " ==> "+(no1+no2));
    }
    public static void add(float no1 , int no2)
    {
        System.out.println("Addition of "+no1+ " and "+no2+ " ==> "+(no1+no2));
    }
    public static void add(int no1 , int no2, int no3)
    {
        System.out.println("Addition of "+no1+ " + "+no2+ " + "+no3+" ==> "+(no1+no2+no3));
    }
   
}

OUTPUT:
Addition of 10 and 20 ==> 30
Addition of 10 + 20 + 30 ==> 60
Addition of 10 and 10.1 ==> 20.1
Addition of 30.3 and 20 ==> 50.3
Prof.Mahendra Kanojia

Program to Demonstrate Return Type, Ternary Operator , Call by Reference and Odd Even Check in Java (BlueJ)
Code:
/ ***** PROGRAM TO DEMONSTRATE RETURN TYPE, TERNARY OPERATOR , CALL BY REFERENCE AND ODD EVEN CHECK ******/

public class ReturnDemo
{
     public static int num = 101;
    
    public static void checkOddEven()
    {
         ReturnDemo rd = new ReturnDemo();// object creation
        
      String rply = rd.check(rd);
      System.out.println(rd.num+ " is a "+rply+" number");
    }
    public static String check(ReturnDemo rd)
    {
        // Using ternery operator
        String ans = (rd.num % 2 == 0)? "EVEN" : "ODD";
        return ans;
    }
}

OUTPUT:
101 is a ODD number
Prof.Mahendra Kanojia

Sunday, 22 January 2012


J2ME Program to Record Data and Print it to the Console

Code:
/*J2ME Program to Record Data and Print it to the Console*/
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

public class RecordStoreMIDlet extends MIDlet implements CommandListener
{
  private Display display;
  private Alert alert;
  private Form form;
  private Command exit, start, close;
  private RecordStore r_store;
  private Image image;
  public void startApp()
{
    try{image = Image.createImage("/error.png");}catch(Exception e){}
    display = Display.getDisplay(this);
    start = new Command("Start", Command.SCREEN, 1);
    close = new Command("Close", Command.SCREEN, 1);
    exit = new Command("Exit", Command.SCREEN, 1);
    form = new Form("Record Store");
    form.addCommand(start);
    form.addCommand(close);
    form.addCommand(exit);
    form.setCommandListener(this);
    display.setCurrent(form);
  }
  public void pauseApp(){}
  public void destroyApp(boolean unconditional){
    notifyDestroyed();
  }
  public void commandAction(Command c, Displayable displayable){
    String label = c.getLabel();
    if(label.equals("Start")){
      try
{
        r_store = RecordStore.openRecordStore("Sandeep Kumar Suman", true );
        String name = r_store.getName();
        System.out.println("Name: "+name);
      }
catch (Exception error)
{
        alert = new Alert("Exception", error.toString(), image,
        AlertType.WARNING);
        alert.setTimeout(Alert.FOREVER);
        display.setCurrent(alert);
      }          
    }
else if(label.equals("Close"))
{
      Try
      {
       r_store.closeRecordStore();
        System.out.println("Successfully Closed Record");
      }
catch (Exception error)
{
        alert = new Alert("Exception", error.toString(), image,
        AlertType.WARNING);
        alert.setTimeout(Alert.FOREVER);
        display.setCurrent(alert);
 }
    }
else if (RecordStore.listRecordStores() != null)
{
      Try
    {
        RecordStore.deleteRecordStore("Sandeep Kumar Suman");
        System.out.println("Successfully Deleted The Record");
      }
catch (Exception error)
{
        alert = new Alert("Exception", error.toString(), image,
        AlertType.WARNING);
        alert.setTimeout(Alert.FOREVER);
        display.setCurrent(alert);
      }
    }
else if(label.equals("Exit"))
   {
      destroyApp(true);
    }
  }  
 }

Sample Output






Comment bellow for Query and Feedback :)
Prof.Mahendra Kanojia

J2ME Program to Read the Contents of a File

Code:
/*J2ME Program to Read the Contents of a File*/
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ReadFile extends MIDlet implements CommandListener
{
  private Display display;
  private Form form;
  private Command read, exit;
  private Alert alert;
  public ReadFile()
  {
    display = Display.getDisplay(this);
    read = new Command("Read", Command.SCREEN, 1);
    exit = new Command("Exit", Command.EXIT, 1);  
    form = new Form("Read File");
    form.addCommand(exit);
    form.addCommand(read);
    form.setCommandListener(this);
  }

  public void startApp()
  {
    display.setCurrent(form);
  }
  public void pauseApp()
 {}
  public void destroyApp(boolean unconditional)
  {
    notifyDestroyed();
  }
  public void commandAction(Command c, Displayable s)
{
    String label = c.getLabel();
    if (label.equals("Read")){
      String string = file();
      if (string != null)
{
        alert = new Alert("Reading", string, null, null);
        alert.setTimeout(Alert.FOREVER);
        display.setCurrent(alert, form);
      }
    } else if (label.equals("Exit")){
      destroyApp(false);
    }
  }
  private String file()
{
    InputStream is = getClass().getResourceAsStream("help.txt");
    StringBuffer sb = new StringBuffer();
    try{
      int chars, i = 0;
      while ((chars = is.read()) != -1){
        sb.append((char) chars);
      }
      return sb.toString();
    }
catch (Exception e)
 {}
    return null;
}
}

Sample Output

Comment bellow for Query and Feedback :)
Prof.Mahendra Kanojia

J2ME Program to Access Data from a Specific URL. The stream connection is used to connect the application to specific URL by Airtime.

Code:
/*J2ME Program to Access Data from a Specific URL*/

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class AccessUrl extends MIDlet
{
  private Display display;
  String url = "http://localhost/AccessURL/hello.txt";
  public AccessUrl()
  {
    display = Display.getDisplay(this);
  }
  public void startApp()
{
    Try
{
      connection(url);
    }
catch (IOException e)
{
      System.out.println("IOException " + e);
      e.printStackTrace();
    }
  }

  public void pauseApp(){}
  public void destroyApp(boolean unconditional){}
  void connection(String url) throws IOException
{
    StreamConnection sc = null;
    InputStream is = null;
    StringBuffer buffer = new StringBuffer();
    TextBox access;
    Try
{
      sc = (StreamConnection)Connector.open(url);
      is = sc.openInputStream();
      int chars;
      while((chars = is.read()) != -1)
{
        buffer.append((char) chars);
      }
      System.out.println(buffer.toString());
      access = new TextBox("Access Text", buffer.toString(), 1024, 0);
    }
Finally
{
      if(is != null)
      {
        is.close();
      }
      if(sc != null)
       {
        sc.close();
      }
    }
    display.setCurrent(access);
  }
}

Sample Output:

Comment bellow for Query and Feedback :)
Prof.Mahendra Kanojia

J2ME Program to Continuously Scroll a Data Message on the Mobile Screen

Code:
/*J2ME Program to Continuously Scroll a Data Message on the Mobile Screen*/

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TickerExample extends MIDlet implements CommandListener{
  private Display display;
  private Ticker ticker;
  private Command exit;
  private Form form;
    public TickerExample()
{
    form = new Form("Ticker Example");
    display = Display.getDisplay(this);
    ticker = new Ticker ("This is an Ticker Example");
    exit = new Command("Exit", Command.SCREEN, 1);
    form.addCommand(exit);
    form.setCommandListener(this);
    form.setTicker(ticker);
  }
  public void startApp()
  {
    display.setCurrent(form);
  }
  public void pauseApp(){ }
  public void destroyApp(boolean unconditional)
  {
    notifyDestroyed();
  }
  public void commandAction(Command c, Displayable display)
    {
    String label = c.getLabel();
    if (label.equals("Exit")){
      destroyApp(false);
    }
  }}

Sample Output:

comment bellow for Query and Feedback :)
Prof.Mahendra Kanojia


Step1:-   Go to the Ktoolbar of J2ME wireless toolkit from the Start menu.
Step2:- Select the new project tab and give a project name and MIDlet Class Name

Step3:- Select the option CLDC 1.1 ka option and remove the check mark of Mobile Media API

Step4:- The Following window appears


Step5:-   Go to C drive of your Computer 


Step6:- Select the WTK22 folder in C drive


Step7:-  Select the apps folder in the WTK22 folder


Step8:- Select the folder named same as the project we created (i.e. HelloWorld).


Step9:- Select the src folder inside the HelloWorld folder.


Step10:- Copy the HelloWorld.Java file in this src folder


Step11:- Return to the Ktoolbar and now Build the Application.


Step12:- Once the project is successfully built , run the application.


Step13:- OUTPUT

Step14:- Click the Right button on mobile to launch the application 


Comment bellow for Query and Feedback :)
Prof.Mahendra Kanojia