Wednesday, 9 December 2015

Simple Code To Write Data in File And Read Data to File

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class File implements Serializable {
    int no;
    String Name;
    double Sal;

    public File() {

        this.no = 100;
        Name = "Amol";
        Sal = 5000;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public double getSal() {
        return Sal;
    }

    public void setSal(double sal) {
        Sal = sal;
    }

    public File(int no, String name, double sal) {
        super();
        this.no = no;
        Name = name;
        Sal = sal;
    };

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Information is : " + no + "   " + Name + "   " + Sal;
    }

    void display() {
        System.out.println("Information is : " + no + "   " + Name + "   "
                + Sal);
    }

}

class C {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Scanner s = new Scanner(System.in);
        ArrayList a = new ArrayList();

        int ch1;

        do {
            System.out.println("1) Write");
            System.out.println("2) Read");
            System.out.println("Enter Your Choice");
            ch1 = s.nextInt();

            switch (ch1) {

            case 1: {
                FileInputStream fie = new FileInputStream(
                        "D:\\Amol\\khushi\\amu.txt");
                ObjectInputStream ob = new ObjectInputStream(fie);
                ArrayList a2 = (ArrayList) ob.readObject();
                System.out.println("How Many Details You want to Enter");
                int ch = s.nextInt();
                File f1[] = new File[ch];
                for (int i = 0; i < f1.length; i++) {

                    System.out.println("Enter the id : ");
                    int aaa = s.nextInt();
                    System.out.println("Enter the Name : ");
                    String aa = s.next();
                    System.out.println("Enter The Salary : ");
                    double ss = s.nextDouble();
                    f1[i] = new File(aaa, aa, ss);
                    a2.add(f1[i]);
                    try {
                        FileOutputStream fio = new FileOutputStream(
                                "D:\\Amol\\khushi\\amu.txt");
                        ObjectOutputStream ob1 = new ObjectOutputStream(fio);
                        ob1.writeObject(a2);
                        System.out.println("Write Sucessfully");
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    f1[i].display();
                }

                break;

            }
            case 2: {
                try {
                    FileInputStream fie = new FileInputStream(
                            "D:\\Amol\\khushi\\amu.txt");
                    ObjectInputStream ob = new ObjectInputStream(fie);
                    ArrayList f2 = (ArrayList) ob.readObject();
                    Iterator it = f2.iterator();
                    while (it.hasNext()) {
                        System.out.println(it.next());
                    }

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
                break;
            }
        } while (ch1 != 10);
    }
}

Above GUI CODE.....


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class GD extends Frame implements ActionListener{
    public static void main(String argv[]){
        GD fa=new GD();
        //Setup GridLayout with 2 rows and 5 columns
        fa.setLayout(new FlowLayout());
        fa.setSize(200,450);
        fa.setVisible(true);
        fa.setLocation(75, 40);
    }
   
   
    GD() {
        Frame frame = new Frame();

        Panel p1 = new Panel();

        p1.setSize(200, 300);
        add(p1);
        p1.setBackground(Color.green);
        Button b, b1, b2, b3, b4, b5;
        b = new Button("Login");
        b1 = new Button("Create An Account");
        b2 = new Button("MAke Deposite");
        b3 = new Button("Make Withdrawl");
        b4 = new Button("Display Account");
        b5 = new Button("Logout");
        p1.setVisible(true);
        p1.setLayout(new GridLayout(6, 1));
        p1.add(b).setLocation(12, 200);
        p1.add(b1).setLocation(12, 200);
        p1.add(b2).setLocation(12, 200);
        p1.add(b3).setLocation(12, 200);
        p1.add(b4).setLocation(12, 200);
        p1.add(b5).setLocation(12, 200);
        p1.setPreferredSize(new Dimension(150, 400));
        setBounds(50,50,50,40);
       b.addActionListener(new ActionListener() {
       
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            new Design();
        }
    });
       b1.addActionListener(new ActionListener() {
       
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            new Create_An_Account();
        }
    });
       b2.addActionListener(new ActionListener() {
       
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            new Make_Deposite();
           
        }
    });
       b3.addActionListener(new ActionListener() {
       
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            new  Withdrowl();
        }
    });
       b4.addActionListener(new ActionListener() {
       
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            new Choose_Account();
        }
    });
       b5.addActionListener(new ActionListener() {
       
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
           
        }
    });
       MyWindowListener m=new MyWindowListener(this);
       this.addWindowListener(m);

   
    } // End of constructor


    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
       
    }


}  //End of Application
   
//players.setPreferredSize(new Dimension(450, 80));
//buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));



**************************************************************************


import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionListener;

public class Create_An_Account extends Frame {
    Label l,l1,l2,l3,l4,l5,l6,l7,l8,l9;
    TextField t,t1,t2,t3,t4,t5,t6,t7,t8,t9;
    Button b,b1,b2,b3;
   
    Create_An_Account()
    {
        l=new Label("Acc_Number");
        l1=new Label("First Name ");
        l2=new Label("Middel Name");
        l3=new Label("Last NAme");
        l4=new Label("Streat");
        l5=new Label("City");
        l6=new Label("Sate ");
        l7=new Label("Pin NO");
        l8=new Label("MObile NO");
        l9=new Label("Email");
        t=new TextField(10);
        t1=new TextField(10);
        t2=new TextField(10);
        t3=new TextField(10);
        t4=new TextField(10);
        t5=new TextField(10);
        t6=new TextField(10);
        t7=new TextField(10);
        t8=new TextField(10);
        t9=new TextField(10);
        b=new Button("New Custmer");
        b1=new Button("Update");
        b2=new Button("Remove");
        b3=new Button("Cancel");
        add(l);
        add(t);
        add(l1);
        add(t1);
        add(l2);
        add(t2);
        add(l3);
        add(t3);
        add(l4);
        add(t4);
        add(l5);
        add(t5);
        add(l6);
        add(t6);
        add(l7);
        add(t7);
        add(l8);
        add(t8);
        add(l9);
        add(t9);
        add(b);
        add(b1);
        add(b2);
        add(b3);
        setBounds(350,25,150,650);
        setLayout(new FlowLayout());
        setVisible(true);
        setResizable(false);
        MyWindowListener m=new MyWindowListener(this);
        this.addWindowListener(m);
    }

}
class Test1
{
    public static void main(String[] args) {
        Create_An_Account d=new Create_An_Account();
    }
}
*********************************************************************




import java.awt.*;


public class Make_Deposite extends Frame{
    Label l,l1,l2;
    TextField t,t1,t2;
    Button b,b1,b2;
    Make_Deposite()
    {
        l=new Label("Accout_Type");
        l1=new Label("Accout_Number ");
        l2=new Label("Deposite_Amount");
        b=new Button("Submit");
        b1=new Button("Reset");
        b2=new Button("Cancel");
        t=new TextField(10);
        t1=new TextField(10);
        t2=new TextField(10);
        add(l);
        add(t);
        add(l1);
        add(t1);
        add(l2);
        add(t2);
        add(b).setPreferredSize(new Dimension(80,30));
        add(b1).setPreferredSize(new Dimension(80,30));
        add(b2).setPreferredSize(new Dimension(80,30));

        setBounds(350,25,250,200);
        setLayout(new FlowLayout());
        setVisible(true);
        setResizable(false);
        MyWindowListener m=new MyWindowListener(this);
        this.addWindowListener(m);
       
    }

}
*****************************************************************************




import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;


public class Withdrowl extends Frame {
    Label l,l1,l2;
    TextField t,t1,t2;
    Button b,b1,b2;
    Withdrowl()
    {
        l=new Label("Accout_Type");
        l1=new Label("Accout_Number ");
        l2=new Label("Withdrawl_Amount");
        b=new Button("Submit");
        b1=new Button("Reset");
        b2=new Button("Cancel");
        t=new TextField(10);
        t1=new TextField(10);
        t2=new TextField(10);
        add(l);
        add(t);
        add(l1);
        add(t1);
        add(l2);
        add(t2);
        add(b).setPreferredSize(new Dimension(80,30));
        add(b1).setPreferredSize(new Dimension(80,30));
        add(b2).setPreferredSize(new Dimension(80,30));

        setBounds(350,25,250,200);
        setLayout(new FlowLayout());
        setVisible(true);
        setResizable(false);
        MyWindowListener m=new MyWindowListener(this);
        this.addWindowListener(m);
       
    }

}



**************************************************************************

import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;


public class Choose_Account extends Frame {
    Label l;
    Button b,b1,b2;
    Choose_Account()
    {
        l=new Label("Click Below To Generate the Report");
        b=new Button("Saving_Account Information");
        b1=new Button("Current_Account Information");
        b2=new Button("Other Trancation Information");
        add(l);
        add(b).setPreferredSize(new Dimension(250,80));;
        add(b1).setPreferredSize(new Dimension(250,80));;
        add(b2).setPreferredSize(new Dimension(250,80));;
        setBounds(350,25,350,400);
        setLayout(new FlowLayout());
        setVisible(true);
        setResizable(false);
        MyWindowListener m=new MyWindowListener(this);
        this.addWindowListener(m);
    }

}
*******************************************************************







1. Create a class Emp, which contains details about the employee and compile and run its
instance.


public class Emp {
    int id;
    String Name;
    double sal;
   
   
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public double getSal() {
        return sal;
    }
    public void setSal(double sal) {
        this.sal = sal;
    }//setter getter end here
   
    public Emp() {
   
        this.id = 100;
        Name = "Amol";
        this.sal = 50000;
    }
   
    public Emp(int id, String name, double sal) {
        super();
        this.id = id;
        Name = name;
        this.sal = sal;
    }
    void display()
    {
        System.out.println("Id is :"+id);
        System.out.println("Name is :"+Name);
        System.out.println("Salary is :"+sal);
    }

}
class test
{
    public static void main(String[] args) {
        Emp e=new Emp(101,"Akshay",8000);
        e.display();
        System.out.println();
        Emp e1=new Emp(101,"Akshay",8000);
        e1.display();
        System.out.println();
        Emp e2=new Emp(101,"Akshay",8000);
        e2.display();
        Emp e3=new Emp(101,"Akshay",8000);
        e3.display();
    }

}



2. Create an inner class for a manager, which contains information about the manager.
Use the appropriate interfaces. Create an anonymous inner class for Technical skills.

public class Empl {
    class Manager{
        int id;
        String Name;
        double sal;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return Name;
        }
        public void setName(String name) {
            Name = name;
        }
        public double getSal() {
            return sal;
        }
        public void setSal(double sal) {
            this.sal = sal;
        }//end here setter and getter
       
        public Manager() {
       
            this.id = 100;
            Name = "Amol";
            this.sal = 50000;
        }
       
       
        public Manager(int id, String name, double sal) {
            super();
            this.id = id;
            Name = name;
            this.sal = sal;
        }
        //constructor end here
        void display()
        {
            System.out.println("Id is :"+id);
            System.out.println("Name is :"+Name);
            System.out.println("Salary is :"+sal);
        }       
       
    }
   

}
class test1{
    public static void main(String[] args) {
        Empl e=new Empl();
        Empl.Manager m=new Empl().new Manager();
        m.display();
       
    }
}

3. Create an appropriate data structures to store employee object and use the
java.util.package properties.

import java.util.ArrayList;
import java.util.*;


public class Emp1 {
    int id;
    String Name;
    double sal;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    } // end here setter and getter

    public Emp1() {

        this.id = 100;
        Name = "Amol";
        this.sal = 50000;
    }

    public Emp1(int id, String name, double sal) {
        super();
        this.id = id;
        Name = name;
        this.sal = sal;
    }

    void display() {
        System.out.println("Id is :" + id);
        System.out.println("Name is :" + Name);
        System.out.println("Salary is :" + sal);
    }

}
class Demo
{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
       
        ArrayList a=new ArrayList();
        Emp1[] b =new Emp1[5];
        for(int i=0;i<b.length;i++)
        {
            b[i]=new Emp1();
            System.out.println("Enter the Id");
            int a1=s.nextInt();
            System.out.println("Enter the Name");
            String s1=s.next();
            System.out.println("Enter the Salary");
            double d=s.nextDouble();
            Emp1 w=new Emp1(a1,s1,d);
            a.add(b[i]);
           
        }
        for(int i=0;i<a.size();i++)
        {
            b[i].display();
        }
       
    }
}

4. Create a user defined exception to check whether your employee exist in your data
structure and using the catch and finally block. Redeem an appropriate solution.


import java.util.*;

import org.omg.CosNaming.NamingContextPackage.NotFound;


public class Emp2 implements Comparable{
    int id;
    String Name;
    double sal;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public double getSal() {
        return sal;
    }
    public void setSal(double sal) {
        this.sal = sal;
    }//end here setter and getter
   
   
   
    public Emp2() {
       
        this.id = 100;
        Name = "Amol";
        this.sal = 50000;
    }
   
   
   
    public Emp2(int id, String name, double sal) {
        super();
        this.id = id;
        Name = name;
        this.sal = sal;
    }
    void display()
    {
        System.out.println("Id is :"+id);
        System.out.println("Name is :"+Name);
        System.out.println("Salary is :"+sal);
    }

    @Override
    public int compareTo(Object arg0) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public boolean equals(Object obk) {
        // TODO Auto-generated method stub
        Emp2 e3=(Emp2)obk;
        if(this.id==e3.id)
        {
        return true;
        }
        else
        {
            return false;
        }
    }
}
 class Demo1
 {
     public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        ArrayList a1=new ArrayList();
         Emp2 e=new Emp2(101,"amol",5000);
        Emp2 e2=new Emp2(102,"Akshay",5000);
        a1.add(e);
        a1.add(e2);
        System.out.println("Enter the Id");
        int a=s.nextInt();
        System.out.println("Enter the Name");
        String b=s.next();
        System.out.println("Enter Salary");
        double d=s.nextDouble();
        Emp2 e1=new Emp2(a,b,d);
        boolean aa=a1.contains(e1);
        if(aa==true)
        {
            try{
                throw new NotFoundException();
            }catch(NotFoundException e111)
            {
                System.out.println(e111);
            }
        }
        else
        {
            a1.add(e1);
            e1.display();
            System.out.println("Records Suceefully Added");

        }
       
    }
 }




******************************************************************



public class NotFoundException extends Exception {
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Records Already Exits";
    }

}

5. Using the collection framework define an appropriate interface to user registration
application.

import java.util.ArrayList;
import java.util.Scanner;


public interface User {

     void Reg(int roll_no,String Name,String Address,int Mobile_Number);
     void display();
     public boolean equals(Object obj);
   
}
class Demo2 implements User{
static int a;
static String b;
static String c;
static int d;
public Demo2() {
this.a = 1;
this.b = "Amol";
this.c = "Baramati";
this.d = 1234567890;
}

    static Scanner s=new Scanner(System.in);

    @Override
    public void Reg(int roll_no, String Name, String Address, int Mobile_Number) {
        // TODO Auto-generated method stub
        roll_no=a;
        Name=b;
        Address=c;
        Mobile_Number=d;
    }
   
    public Demo2(int a, String b, String c, int d) {
    super();
    this.a = a;
    this.b = b;
    this.c = c;
    this.d = d;
}

    @Override
    public void display() {
        // TODO Auto-generated method stub
        System.out.println("Roll_number is a :"+a);
        System.out.println("Name is a : "+b);
        System.out.println("Address is a : "+c);
        System.out.println("Mobile Number is a :"+d);
       
       
    }
   
    public static void main(String[] args) {
        Demo2 d1=new Demo2();
        d1.display();
        System.out.println();
          System.out.println("Enter the Roll_Number");
            a=s.nextInt();
            System.out.println("Enter the Name");
            b=s.next();
            System.out.println("Enter the Adress");
            c=s.next();
            System.out.println("Enter The Mobile Number");
            d=s.nextInt();
        Demo2 d11=new Demo2(a,b,c,d);
        d11.display();
       
       
       
    }


   
}
7. Create Employee & manager classes and make Employee, manager classes objects
persistent.


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Employee implements Serializable {
    int id;
    String Name;
    double sal;

    int getId() {
        return id;
    }

    void setId(int id) {
        this.id = id;
    }

    String getName() {
        return Name;
    }

    void setName(String name) {
        Name = name;
    }

    double getSal() {
        return sal;
    }

    void setSal(double sal) {
        this.sal = sal;
    }// Setter And Getter End Here

    public Employee() {
        this.id = 100;
        Name = "Amol";
        this.sal = 90000;
    }

    public Employee(int id, String name, double sal) {
        super();
        this.id = id;
        Name = name;
        this.sal = sal;
    }

    // Construtor End Here
    void display() {
        System.out.println("Id is a :" + id);
        System.out.println("Name is a :" + Name);
        System.out.println("Salary is a :" + sal);
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Id is a :" + id+"   "+  Name +"   "+ "   " + sal;
    }

} // Employee Class Here

class Manager extends Employee {
    int target;
    int incentive;

    int getTarget() {
        return target;
    }

    void setTarget(int target) {
        this.target = target;
    }

    int getIncentive() {
        return incentive;
    }

    void setIncentive(int incentive) {
        this.incentive = incentive;
    }// end here setter and getter

    public Manager() {

        this.target = 50;
        this.incentive = 500;
    }

    public Manager(int a, String b, double p, int target, int incentive) {
        super(a, b, p);
        this.target = target;
        this.incentive = incentive;
    }

    void display() {
        super.display();
        System.out.println("Target is a : " + target);
        System.out.println("Incentive is a : " + incentive);
    }

}// MAnager Class End Here

class Dewmo {
    public static void main(String[] args) {
        ArrayList a = new ArrayList();
        Scanner s = new Scanner(System.in);
        System.out.println("Defalut Information Here ");
        Manager m = new Manager();
        m.display();
        System.out.println();
        System.out.println();
        System.out.println();
        int ch;
        do {
            System.out.println("1) Writing Data to File..!!");
            System.out.println("2) Reading Data to File..!!");
            System.out.println("Enter your Chocice");
            ch = s.nextInt();
            switch (ch) {
            case 1: {

                Manager m1 = new Manager(100, "Akshay", 80000, 60, 7800);
                Manager m2 = new Manager(200, "Amol",   90000, 70, 7896);
                Manager m3 = new Manager(100, "Akshay", 80000, 60, 7800);
                Manager m4 = new Manager(100, "Akshay", 80000, 60, 7800);
                Manager m5 = new Manager(100, "Akshay", 80000, 60, 7800);
                Manager m6 = new Manager(100, "Akshay", 80000, 60, 7800);
                Manager m7 = new Manager(100, "Akshay", 80000, 60, 7800);
                Manager m8 = new Manager(100, "Akshay", 80000, 60, 7800);
                Manager m9 = new Manager(100, "Akshay", 80000, 60, 7800);
                Manager m10 = new Manager(100, "Akshay",80000, 60, 7800);

                a.add(m1);
                a.add(m2);
                a.add(m3);
                a.add(m4);
                a.add(m5);
                a.add(m6);
                a.add(m7);
                a.add(m8);
                a.add(m9);
                a.add(m10);
                try {
                    FileOutputStream fio = new FileOutputStream(
                            "D:\\amol\\khushi\\amui.txt");
                    ObjectOutputStream o = new ObjectOutputStream(fio);
                    o.writeObject(a);
                    System.out.println("Write Suessfully...!!");
                    o.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            }
            case 2: {
                try {
                    FileInputStream fi = new FileInputStream(
                            "D:\\amol\\khushi\\amui.txt");
                    try {
                        ObjectInputStream oa = new ObjectInputStream(fi);
                        try {
                            ArrayList a1 = ((ArrayList) oa.readObject());
                            // System.out.println(a1);
                            Iterator it = a1.iterator();
                            while (it.hasNext()) {
                                System.out.println(it.next());
                            }

                            oa.close();

                        } catch (ClassNotFoundException e6) {
                            // TODO Auto-generated catch block
                            e6.printStackTrace();
                        }
                    } catch (IOException e6) {
                        // TODO Auto-generated catch block
                        e6.printStackTrace();
                    }
                } catch (FileNotFoundException e6) {
                    // TODO Auto-generated catch block
                    e6.printStackTrace();
                }
                break;
            }

            }

        } while (ch != 10);
    }

}


9. Create a new array, whose size and component type are not known until runtime, and
then modify the array's components


import java.util.Scanner;


public class Array1 {
    int id;
    String Name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }//End Here Setter And Getter
   
   
    public Array1() {
   
        this.id = 100;
        this.Name = "Amol";
    }
   
   
    public Array1(int id, String name) {
        super();
        this.id = id;
        Name = name;
    } //Constructor End Here
    void display()
    {
        System.out.println("ID is a :" +id);
        System.out.println("Number is a : "+Name);
    }

}
class demoa
{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        System.out.println("Enter the size of Array u want");
        int n=s.nextInt();
        Array1 []a=new Array1[n];
        for(int i=0;i<a.length;i++)
        {
            System.out.println("Enter The Id");
            int b=s.nextInt();
            System.out.println("Enter The Name");
            String h=s.next();
            a[i]=new Array1();
            a[i].display();
        }
   
       
    }

}

Monday, 7 December 2015

Mini Project CUI BAse

//Developed By Amol Sitaram Orape
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.*;

public class Emp implements Serializable, Comparable {
    int id;
    String name;
    double sal;
    String Deg;

    int getId() {
        return id;
    }

    void setId(int id) {
        this.id = id;
    }

    String getName() {
        return name;
    }

    void setName(String name) {
        this.name = name;
    }

    double getSal() {
        return sal;
    }

    void setSal(double sal) {
        this.sal = sal;
    }

    String getDeg() {
        return Deg;
    }

    void setDeg(String deg) {
        Deg = deg;
    }// end here setter and getter

    public Emp() {
        super();
        this.id = 100;
        this.name = "Amol";
        this.sal = 50000;
        Deg = "Manger";
    }

    public Emp(int id, String name, double sal, String deg) {
        super();
        this.id = id;
        this.name = name;
        this.sal = sal;
        Deg = deg;
    }// end here constr....

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Information is " + id + "\n" + name + "\n" + sal + "\n" + Deg;
    }

    @Override
    public int compareTo(Object o) {
        // TODO Auto-generated method stub
        Emp w = (Emp) o;
        return this.id = w.id;
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        Emp w = (Emp) obj;
        if (this.id == w.id)
            return true;
        else
            return false;

    }
    public boolean equal(Object obj) {
        // TODO Auto-generated method stub
        Emp w = (Emp) obj;
        if (this.id == w.id)
        {
            return true;
        }
        else
            return false;

    }
}

class Test {
    public static void main(String[] args) throws FileNotFoundException {

        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        int ch;
        System.out.println("**********Menu**************");
        System.out.println("1) Create Five Employee Details");
        System.out.println("2) Dispaly all Employee Details");
        System.out.println("3) Add Single Employee Details");
        System.out.println("4) Search By id");
        System.out.println("5) Upadte By id ");
        System.out.println("6) Sort  By id ");
        ArrayList a = new ArrayList();
        do {
            Scanner s = new Scanner(System.in);
            ch = s.nextInt();
            switch (ch) {

            case 1: {
                Emp e1 = new Emp(1, "Amol", 50000, "Manger");
                Emp e2 = new Emp(2, "Akshay", 50000, "Manger");
                Emp e3 = new Emp(3, "Rushikesh", 50000, "Manger");
                Emp e4 = new Emp(4, "Parasad", 50000, "Manger");
                Emp e5 = new Emp(5, "Aaish", 50000, "Manger");

                a.add(e1);
                a.add(e2);
                a.add(e3);
                a.add(e4);
                a.add(e5);
                try {

                    FileOutputStream fio = new FileOutputStream(
                            "D:\\amol\\khushi\\emp.txt");
                    try {
                        ObjectOutputStream os = new ObjectOutputStream(fio);
                        os.writeObject(a);
                        System.out.println("Write Sucessfully");
                        os.close();
                    } catch (IOException e6) {
                        // TODO Auto-generated catch block
                        e6.printStackTrace();
                    }
                } catch (FileNotFoundException e6) {
                    // TODO Auto-generated catch block
                    e6.printStackTrace();
                }
                break;
            }

            case 2: {
                try {
                    FileInputStream fi = new FileInputStream(
                            "D:\\amol\\khushi\\emp.txt");
                    try {
                        ObjectInputStream oa = new ObjectInputStream(fi);
                        try {
                            ArrayList a1 = ((ArrayList) oa.readObject());
                            // System.out.println(a1);
                            Iterator it = a.iterator();
                            while (it.hasNext()) {
                                System.out.println(it.next());
                            }

                            oa.close();

                        } catch (ClassNotFoundException e6) {
                            // TODO Auto-generated catch block
                            e6.printStackTrace();
                        }
                    } catch (IOException e6) {
                        // TODO Auto-generated catch block
                        e6.printStackTrace();
                    }
                } catch (FileNotFoundException e6) {
                    // TODO Auto-generated catch block
                    e6.printStackTrace();
                }
                break;
            }

            case 3: {

                try {
                    System.out.println("Enter the id :");
                    int nm = Integer.parseInt(br.readLine());
                    System.out.println("Enter the Name");
                    String a1 = br.readLine();
                    System.out.println("Enter the salary");
                    double aa = Double.parseDouble(br.readLine());
                    System.out.println("Enter the Degisation ");
                    String as = br.readLine();

                    Emp e11 = new Emp(nm, a1, aa, as);
                    System.out.println(e11);
                    a.add(e11);

                } catch (IOException e12) {
                    // TODO Auto-generated catch block
                    e12.printStackTrace();
                }
                break;
            }
            case 4: {
                FileInputStream fi;
                try {
                    fi = new FileInputStream("D:\\amol\\khushi\\emp.txt");

                    ObjectInputStream oa;

                    oa = new ObjectInputStream(fi);

                    ArrayList a1;

                    a1 = ((ArrayList) oa.readObject());
                    //System.out.println(a1);

                    System.out.println("Enter the id :");
                    int nm = s.nextInt();
                    System.out.println("Enter the Name");
                    String a11 = s.next();
                    System.out.println("Enter the salary");
                    double aa = s.nextDouble();
                    System.out.println("Enter the Degisation ");
                    String as = s.next();

                    Emp e11 = new Emp(nm, a11, aa, as);

                    if (a1.contains(e11)) {
                        System.out.println("Found");
                    } else {
                        System.out.println("Not Found ");
                    }
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                break;
            }
            case 5: {
                FileInputStream fi;
                try {
                    fi = new FileInputStream("D:\\amol\\khushi\\emp.txt");

                    ObjectInputStream oa;

                    oa = new ObjectInputStream(fi);

                    ArrayList a1;

                    a1 = ((ArrayList) oa.readObject());
                    //System.out.println(a1);
                    System.out.println("Enter the id :");
                    int nm = s.nextInt();
                    System.out.println("Enter the Name");
                    String a11 = s.next();
                    System.out.println("Enter the salary");
                    double aa = s.nextDouble();
                    System.out.println("Enter the Degisation ");
                    String as = s.next();

                    Emp e11 = new Emp(nm, a11, aa, as);
                    if(a1.contains(e11))
                    {
                        System.out.println("Id is Found so enter the updated information");
                       
                        System.out.println(a1);
                        System.out.println("Enter the id :");
                        int nm1 = s.nextInt();
                        System.out.println("Enter the Name");
                        String a111 = s.next();
                        System.out.println("Enter the salary");
                        double aa1 = s.nextDouble();
                        System.out.println("Enter the Degisation ");
                        String as1 = s.next();

                        Emp e111 = new Emp(nm1, a111, aa1, as1);
                        a.add(e111);
                        FileOutputStream fio = new FileOutputStream(
                                "D:\\amol\\khushi\\emp.txt");
                        ObjectOutputStream o=new ObjectOutputStream(fio);
                        o.writeObject(a);
                        o.close();
                        System.out.println("Written Sucessfully");
                        System.out.println("Devloped By AMOL SITARAM ORAPE");
                       
                       
                    }
                    else
                        System.out.println("Updated Id is Not Found..!! Please Try Agains..!! ");
                }
                    catch (IOException | ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               
                }

    break;

            }
           
            case 6: {

                System.out.println("Code in Progress.............!!amol..!");

            }

                break;
            }

        } while (ch != 10);
    }
}

Wednesday, 2 December 2015

Use Inbuilt or Core Function of Date & Math Class

import java.util.*;
class date {
    public static void main(String[] args) {
       
   
    Date date1=new Date();
    System.out.println("Full date is :"+date1);
    System.out.println("Date is : "+date1.getDate());
    System.out.println("Day is : "+date1.getDay());
    System.out.println("Hour is : "+date1.getHours());
    System.out.println("Mintiues is : " +date1.getMinutes());
    System.out.println("Second is : "+date1.getSeconds());
    System.out.println("Time is : "+date1.getTime());
    System.out.println("Year is : "+date1.getYear());
    System.out.println("Time Zone OffSet is : "+date1.getTimezoneOffset());
    }   

}

OutPut is=

Full date is :Wed Dec 02 17:52:01 IST 2015
Date is : 2
Day is : 3
Hour is : 17
Mintiues is : 52
Second is : 1
Time is : 1449058921359
Year is : 115
Time Zone OffSet is : -330








import java.lang.Math;
import java.util.*;

class Maths {
    public static void main(String[] args) {
        int a=Math.abs(-1);
        System.out.println("Abs Of Given Number is : "+a);
        double b=Math.ceil(1.2522);
        System.out.println("Ceil of  Given Number is : "+b);
        double c=Math.sqrt(25);
        System.out.println("Square root of Given Number is : "+c);
        double d=Math.E;
        System.out.println("Value Of e is : "+d);
        double f=Math.PI;
        System.out.println("Value Of PI is :"+f);
    }
   

}

Output is :
Abs Of Given Number is : 1
Ceil of  Given Number is : 2.0
Square root of Given Number is : 5.0
Value Of e is : 2.718281828459045
Value Of PI is :3.141592653589793

Friday, 27 November 2015

Number Check Exception


     Create a class called CalcAverage that has the following method:

public double avgFirstN(int N)


This method receives an integer as a parameter and calculates the average of first N natural numbers. If N is not a natural number, throw an exception IllegalArgumentException with an appropriate message.


package om;
public class Test {
    int c;
   
    public double avgFirstN(int N)
    {
        if(N<0)
        {
            try{
                throw new NumberFormatException();
            }
            catch(NumberFormatException nf){
                System.out.println("We Must To Enter The POsitive Number");
            }
        }
        else
        {
            int c=N*10;
       
        System.out.println("THe Average is a = "+c);
        }
       
        return N;   
    }
   
public static void main(String[] args) {
       
       
        Test t=new Test();
        t.avgFirstN(10);   
    }

}