Saturday, 22 March 2014

Assignment 2

Student info
import java.io.*;
class StudentInfo
{
    int rno;
    String name,course;
    float fees;
    BufferedReader br;
    StudentInfo()
    {
        br=new BufferedReader(new InputStreamReader(System.in));
        try
        {
            System.out.println("Eneter roll no");
            rno=Integer.parseInt(br.readLine());
            System.out.println("Eneter name");
            name=br.readLine();
            System.out.println("Eneter course");
            course=br.readLine();
            System.out.println("Eneter fees");
            fees=Float.parseFloat(br.readLine());
        }
        catch(Exception ex)
        {
            System.out.println("Error in input");
        }
    }

    public void show()
    {
        System.out.print("\nRoll no-  "+rno+"\nName-  "+name+"\nCourse-  "+course+"\nFees-  "+fees);
    }
    public static void main(String args[])
    {
        StudentInfo si=new StudentInfo();
        si.show();
    }
}

Account Details
import java.io.*;
class AccountDetails
{
    int acno;
    String name,type;
    float bal;
    BufferedReader br;
    AccountDetails()
    {
        br=new BufferedReader(new InputStreamReader(System.in));
        try
        {
            System.out.println("Enter account no");
            acno=Integer.parseInt(br.readLine());
            System.out.println("Enter account type");
            type=br.readLine();
            System.out.println("Enter name");
            name=br.readLine();
            System.out.println("Enter balance");
            bal=Float.parseFloat(br.readLine());
        }
        catch(Exception ex)
        {
            System.out.println("Error in input");
        }
    }

    public void deposit(float amt)
    {
        bal=bal+amt;
    }
   
    public void withdraw(float amt)
    {
        if(bal<500&&(bal-amt)<500)
        {
            System.out.println("Cannot withdraw ammount ");
            System.exit(1);
        }
        else
        {
            bal=bal-amt;
        }
    }

    public void showBal()
    {
        System.out.print("\nAccount no-  "+acno+"\nAccount type-  "+type+"\nName-  "+name+"\nBalance available-  "+bal);
    }
    public static void main(String args[])
    {
        AccountDetails ad=new AccountDetails();
        ad.showBal();
        ad.deposit(1500F);
        ad.showBal();
        ad.withdraw(500F);
        ad.showBal();
    }
}

 
String sort
class StringSort
{
    public static void main(String[] args)
    {
        String names[] = {"paul" , "john", "smith", "avery"};
        String t;
        int n = names.length;
        int i,j,c;
        for (i=0; i<n-1; i++)
        {
            for (j=i+1; j<n; j++)
            {
                c = names[i].compareTo(names[j]);
                if (c >0)
                {
                    t = names[i];
                    names[i] = names[j];
                    names[j] = t;       
                }   
            }
        }
        for (i=0; i<n ;i++)
        {
            System.out.println(names[i]);   
        }
    }
}

 
Max Min
 import java.io.*;
public class MaxMin
{
    int arr[];
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    MaxMin()
    {
        arr=new int[5];
        for(int i=0;i<5;i++)
            arr[i]=0;
    }

    public void accept()
    {
        try
        {
            System.out.println("Enter 5 elements :");
            for(int i=0;i<5;i++)
                arr[i]=Integer.parseInt(br.readLine());
        }catch(IOException ex){}
    }

    public void find()
    {
        int max,min;
        max=min=arr[0];
        for(int i=1;i<5;i++)
        {
            if(max<arr[i])
                max=arr[i];
            else if(min>arr[i])
                min=arr[i];
        }
        System.out.println("Maximum number amongst entered array is "+max);
        System.out.println("Minimum number amongst entered array is "+min);
    }

    public static void main(String args[])
    {
        MaxMin mm=new MaxMin();
        mm.accept();
        mm.find();
    }
}

Assignment 1

Perfect no

import java.io.*;
public class PerfectNo {
    public boolean isPerfectNumber(int number){
        int temp = 0;
        for(int i=1;i<=number/2;i++){
            if(number%i == 0){
                temp += i;
            }
        }
        if(temp == number){
           
            return true;
        } else {
           
            return false;
        }
    }
    
    public static void main(String a[]){
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PerfectNo ipn = new PerfectNo();
        try{
        System.out.println("Enter The Number:   ");
        int i =  Integer.parseInt(br.readLine());
        System.out.println("Is perfect number: "+ipn.isPerfectNumber(i));}
        catch(Exception e){}
    }
}



Prime No

import java.io.*;public class PrimeNo
{
    public static void main(String[] args)
    {
             try {
                int starting_number;
                int ending_number ;
                long totals = 0;
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                System.out.println(" Enter Starting No: ");
                starting_number = Integer.parseInt(br.readLine());
                System.out.println(" Enter Ending No: ");
                ending_number = Integer.parseInt(br.readLine());
                System.out.println("List of prime numbers between " + starting_number + " and " + ending_number);
                for (long current = starting_number; current <= ending_number; current++)
        {
                        long sqr_root = (long) Math.sqrt(current);
                        boolean is_prime = true;
                        for (long i = 2; i <= sqr_root; i++)
            {
                            if (current % i == 0)
                {
                                    is_prime = false; // Current is not prime.
                            }
                        }
                        if (is_prime) {
                        System.out.println(current);
                        totals++;
                        }
                }
            System.out.println("There are a total of "+totals+" prime numbers between "+starting_number+" and "+ending_number);
        }    
        catch (Exception e){}
    }
}


  Fibonacci Series
import java.io.*;
import java.util.*;
public class FibonacciSeries
{
    private static int[] F = new int[41];
    public static void main(String[] args)
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=0;
        do
        {
            System.out.print("Please enter a value for n: ");
            try
            {
                n = Integer.parseInt(br.readLine());
            } catch (Exception x) {
            System.out.println("Must be a whole number!");
            System.exit(1);
            }
            if (n>40) System.out.println("Number too big! Must be 40 or less");
            if (n<1) System.out.println("Number too small! Must be 1 or more");
        } while (n>40 || n<1);

        System.out.println("first n terms in the Fibonacci Series.");
        for (int i=1; i<=n; i++)
        {
            System.out.print(fib(i)+" ");
        }
    }

    public static int fib(int n)
    {
        for (int i=1; i<=n; i++)
        {
            switch(i)
            {
                case 1 :     F[i]= 0; break;
                case 2 :
                case 3 :    F[i] = 1; break;
                default :    F[i] = F[i-2] + F[i-1];
            }
        }
        return F[n];
    }
}

 
Pattern
import java.io.*;
class pattern
{
public static void main(String args[])
{
int i,j,k,l;
for(i=1;i<=5;i++)
{
    for(j=1;j<=i;j++)
    {
        System.out.print(" ");
    }
    for(j=i;j<=5;j++)
    {
        System.out.print("$");
    }
   
System.out.print("\n");

}
}
}

50) Write a RMI application to convert dolor into rupees .Write all interfaces and required classes.

import java.rmi.*;
public interface rupees extends Remote
{
    public double convert(double rs) throws RemoteException;
}


import java.rmi.*;
import java.rmi.server.*;
public class rupees_impli extends UnicastRemoteObject implements rupees
{
    public rupees_impli() throws RemoteException
    {}

    public double convert(double rs) throws RemoteException
    {
        return (rs/65);
    }
}


import java.rmi.*;
import java.net.*;
public class rupees_server
{
    public static void main(String args[])
    {
        try
        {
        rupees_impli rsimpli=new rupees_impli();
        Naming.rebind("RmiRupee",rsimpli);
        }
        catch(Exception ex){}
    }
}


import java.io.*;
import java.rmi.*;
import java.net.*;
public class rupees_client
{
    public static void main(String args[])
    {
        try
        {
            String url="rmi://127.0.0.1/RmiRupee";
            rupees intf=(rupees)Naming.lookup(url);

            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter Rs : ");
            double rsval=Double.parseDouble(br.readLine());
            System.out.println("Conversion of "+rsval+" rupees is dollor "+intf.convert(rsval) );
        }
        catch(Exception ex){}
    }
}

49) Write a RMI application to reverse the given string .Write all interfaces and required classes.

import java.rmi.*;
public interface StrRev extends Remote
{
    public String reverse(String str) throws RemoteException;
}


import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
public class StrRev_Impli extends UnicastRemoteObject implements StrRev
{
    public StrRev_Impli() throws RemoteException
    {}

    public String reverse(String str) throws RemoteException
    {
        String str1="";
        for(int i=str.length()-1;i>=0;i--)
            str1=str1+str.charAt(i);
        return (str1);
    }
}


import java.rmi.*;
import java.net.*;
public class StrRev_Server
{
    public static void main(String args[])
    {
        try
        {
        StrRev_Impli strimpli=new StrRev_Impli();
        Naming.rebind("RmiReverse",strimpli);
        }
        catch(Exception ex){}
    }
}


import java.io.*;
import java.rmi.*;
import java.net.*;
public class StrRev_Client
{
    public static void main(String args[])
    {
        try
        {
            String url="rmi://127.0.0.1/RmiReverse";
            StrRev strf=(StrRev)Naming.lookup(url);

            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter String to reverse : ");
            String str=br.readLine();
            System.out.println("Reverse of entered string is "+strf.reverse(str));
        }
        catch(Exception ex){}
    }
}

48) Write a RMI application to check whether the given number is Prime or not . Write all interfaces and required classes.

import java.rmi.*;
public interface Prime extends Remote
{
    public int check_prime(int no) throws RemoteException;
}


import java.rmi.*;
import java.rmi.server.*;
public class Prime_Impli extends UnicastRemoteObject implements Prime
{
    public Prime_Impli() throws Exception
    {
    }

    public int check_prime(int no) throws RemoteException
    {
        int i,cnt=0;
        for(i=2;i<=no/2;i++)
            if(no%i==0)
            {
                cnt++;
                break;
            }       
            return (cnt);
    }
}


import java.rmi.*;
import java.net.*;
public class Prime_server
{
    public static void main(String args[])
    {
        try
        {
        Prime_Impli primpli=new Prime_Impli();
        Naming.rebind("RmiPrime",primpli);
        }
        catch(Exception ex){}
    }
}


import java.io.*;
import java.rmi.*;
import java.net.*;
public class Prime_client
{
    public static void main(String args[])
    {
        try
        {
            String url="rmi://127.0.0.1/RmiPrime";
            Prime intf=(Prime)Naming.lookup(url);

            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter No : ");
            int no=Integer.parseInt(br.readLine());
            int x=intf.check_prime(no);
            if(x==0)
            System.out.println(no+" is prime number");
            else
            System.out.println(no+" is not prime number");   
    }
        catch(Exception ex){}
    }
}

34) Write a progtam to show how file class is used to display information about a directory and all the files included in current directory.

import java.io.File;
import java.io.IOException;

public class Directory
{
    public static void main(String[] args)
    {
        File currentDir = new File(".");
        DirContents(currentDir);
    }

    public static void DirContents(File dir)
    {
        try
        {
            File[] files = dir.listFiles();
            for (File file : files)
            {
                System.out.println("     file:" + file.getCanonicalPath());
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

}

33) Write a program to check whether the file is readable, Writable or hidden.

import java.util.Scanner;
import java.io.File;
class FileDemo
{
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        String s=input.nextLine();
        File f1=new File(s);   
        System.out.println("File Name:"+f1.getName());
        System.out.println("This file is:"+(f1.exists()?"Exists":"Does not exists"));
        System.out.println("Is Readable:"+f1.canRead());
        System.out.println("IS Writable:"+f1.canWrite());
        System.out.println("Is Hidden:"+f1.isHidden());
    }
}