File Handling using Object Input Output Stream

import java.io.*;
import java.util.*;
class ObjectReadWrite implements Serializable, Comparable <ObjectReadWrite>
{
int no;
String name;
ObjectReadWrite(int i,String s)
{
this.no=i;
this.name=s;
}
public String toString()
{
return "TP Data : "+this.no+":"+this.name;
}
public int compareTo(ObjectReadWrite a1)
{
return this.name.compareTo(a1.name);
}
public static void main(String args[]) throws Exception
{
TreeSet<ObjectReadWrite> aw = new TreeSet<ObjectReadWrite>();
aw.add(new ObjectReadWrite(1,"ABC"));
aw.add(new ObjectReadWrite(2,"DDF"));
aw.add(new ObjectReadWrite(3,"DAF"));
aw.add(new ObjectReadWrite(4,"DCF"));

//Writing Objects From TreeSet
ObjectOutputStream os=new ObjectOutputStream(
new FileOutputStream(
new File("Test.dat")));
os.writeObject(aw);
os.close();



//Reading Objects into TreeSet
TreeSet ar = new TreeSet();
ObjectInputStream is=new ObjectInputStream(
new FileInputStream(
new File("Test.dat")));

ar=(TreeSet)is.readObject();
is.close();


Iterator ir = ar.iterator();
while(ir.hasNext())
{
System.out.println((ObjectReadWrite)ir.next());
}

}
}

Use of HashMap

import java.util.*;
class BookId implements Comparator{
public int compare(Object o1,Object o2){
return ((Book)o1).book_id-((Book)o2).book_id;
}
}
class BookName implements Comparator{
public int compare(Object o1,Object o2){
return ((Book)o1).title.compareTo(((Book)o2).title);
}
}
public class Book implements Comparable{
/**
Unique Book ID
                */
public int book_id;
protected String title;
/**
Book Price including GST
                */
private float price;
/**
Constructor
                */
public Book(int id, String title, float price){
this.book_id = id;
this.title = title;
this.price = price;
}
public int compareTo(Object o1){
return (int)(this.price-(((Book)o1).price));
}
public String toString(){
return this.book_id+"#"+this.title+"#"+this.price+"@";
}

public static String ObjToString(Book c[]){
String str = new String();
for(int i=0;i<c.length;i++){
str = str+c[i];
}
return str;
}

public static Book[] StringToObj(String str){
String[] st = str.split("@");
Book b2[] = new Book[st.length];

for(int i=0; i<st.length;i++){
String st1[] = st[i].split("#");
b2[i] = new Book(Integer.parseInt(st1[0]),st1

[1],Float.parseFloat(st1[2]));
}
return b2;
}

public static void main(String args[]){

//ArrayList a1 = new ArrayList();
//TreeSet a1 = new TreeSet();
HashMap<Integer,Book> a1 = new HashMap<Integer,Book>();

a1.put(new Integer(1),new Book(2,"ADA", 200));
a1.put(new Integer(3),new Book(1,"Java", 500));
a1.put(new Integer(2),new Book(4,"MI", 800));
//Collections.sort(a1,new BookName());
Collection<Book> a2 = a1.values();
Iterator ir = a2.iterator();

while(ir.hasNext())
{
System.out.println((Book)(ir.next()));
}


/*System.out.print(Book.ObjToString(b));
String str1 = ObjToString(b);

Book c[] = StringToObj(str1);
for(int i=0;i<c.length;i++){
System.out.print(c[i]);
}*/
}
}

Use of Collection Framework

Use of Collection Framework Classes and Interfaces

ArrayList, TreeSet, Iterator

import java.util.*;
public class Book implements Comparable{
/**
Unique Book ID
                */
public int book_id;
protected String title;
/**
Book Price including GST
                */
private float price;
/**
Constructor
                */
public Book(int id, String title, float price){
this.book_id = id;
this.title = title;
this.price = price;
}
public int compareTo(Object o1){
return this.book_id-(((Book)o1).book_id);
}
public String toString(){
return this.book_id+"#"+this.title+"#"+this.price+"@";
}

public static String ObjToString(Book c[]){
String str = new String();
for(int i=0;i<c.length;i++){
str = str+c[i];
}
return str;
}

public static Book[] StringToObj(String str){
String[] st = str.split("@");
Book b2[] = new Book[st.length];
for(int i=0; i<st.length;i++){
String st1[] = st[i].split("#");
b2[i] = new Book(Integer.parseInt(st1[0]),st1[1],Float.parseFloat(st1[2]));
}
return b2;
}

public static void main(String args[]){
Book b[] = new Book[3];
b[0] =new Book(1,"Java", 100);
b[1] = new Book(2,"ADA", 200);
b[2] = new Book(3,"SP", 300);
//ArrayList a1 = new ArrayList();
TreeSet a1 = new TreeSet();
a1.add(new Book(2,"ADA", 200));
a1.add(new Book(2,"ADA", 200));
a1.add(new Book(1,"Java", 500));
a1.add(new Book(4,"MI", 800));
// Collections.sort(a1);
Iterator ir = a1.iterator();
while(ir.hasNext())
{
System.out.println((Book)(ir.next()));
}
System.out.print(a1);
/*System.out.print(Book.ObjToString(b));
String str1 = ObjToString(b);
Book c[] = StringToObj(str1);
for(int i=0;i<c.length;i++){
System.out.print(c[i]);
}*/
}
}

Array of Object to String and String to Array of Object

class Book{
int book_id;
String title;
float price;

Book(int id, String title, float price){
this.book_id = id;
this.title = title;
this.price = price;
}

public String toString(){
return this.book_id+"#"+this.title+"#"+this.price+"@";
}

public static String ObjToString(Book c[]){
String str = new String();
for(int i=0;i<c.length;i++){
str = str+c[i];
}
return str;
}

public static Book[] StringToObj(String str){
String[] st = str.split("@");
Book b2[] = new Book[st.length];

for(int i=0; i<st.length;i++){
String st1[] = st[i].split("#");
b2[i] = new Book(Integer.parseInt(st1[0]),st1[1],Float.parseFloat(st1[2]));
}
return b2;
}

public static void main(String args[]){

Book b[] = new Book[3];

b[0] =new Book(1,"Java", 100);
b[1] = new Book(2,"ADA", 200);
b[2] = new Book(3,"SP", 300);


System.out.print(Book.ObjToString(b));
String str1 = ObjToString(b);

Book c[] = StringToObj(str1);
for(int i=0;i<c.length;i++){
System.out.print(c[i]);
}
}
}

Demo Socket Program

Server Side :
import java.net.*;
import java.io.*;
class Server
{
public static void main(String s[]) throws Exception
{
ServerSocket ss=new ServerSocket(5000);
Socket s1=ss.accept();
System.out.println(s1);
DataOutputStream d1=new DataOutputStream(s1.getOutputStream());
d1.writeUTF("Hello Client");
d1.flush();
d1.close();
}
}


Client Side:
import java.net.*;
import java.io.*;
class Client
{
public static void main(String s[]) throws Exception
{
Socket ss=new Socket("localhost",5000);
DataInputStream d1=new DataInputStream(ss.getInputStream());
System.out.println(d1.readUTF());
d1.close();
}
}

Overloading in JAVA

Method Overloading and Constructor Overloading

/* 
Create Class Student with attributes like rollno and name, where You have to implement the concept of Method Overloading and Constructor Overloading.

Constructor Overloading and Static member concept :
Create Constructor in such a way where if User provide (rollno, name) then students object should be created with same. If user provide only name then next possible rollno should be assigned to student. 

Method Overloading :
Create method exam where marks of 3 subject will be supplied. If student get <50 marks in any of the subject then result should be declared as a Fail with no of ATKT in a exam.
If students appeared in exam of only one or two subject then using appropriate method should be called with different no of parameters 
*/


class Student
{
                int rollno;
                String name;
                static int noofstud;

// Constructor Overloading
                Student()
                {
                }

                Student(int roll,String sname)
                {
                                this.rollno=roll;
                                this.name=sname;
                                noofstud=roll+1;
                }
                Student(String sname)
                {
                                this.rollno=noofstud;
                                this.name=sname;
                }

//Method Overloading
                void exam(int sub1,int sub2,int sub3)
                {
                                int fail=0;
                                if(sub1<50)
                                {
                                                fail=fail+1;
                                }

                                if(sub2<50)
                                {
                                                fail=fail+1;
                                }

                                if(sub3<50)
                                {
                                                fail=fail+1;
                                }

                                if(fail==0)
                                {
                                                System.out.println("Congratulations "+ this.name + " You have successfully clear the Exam.....!");
                                }
                                else
                                {
                                                System.out.println("Sorry "+ this.name + " You are failed in "+fail+" Subject");
                                }
                }

                void exam(int sub1,int sub2)
                {
                                exam(sub1,sub2,0);
                }

                void exam(int sub1)
                {
                                exam(sub1,0,0);
                }

                public static void main(String args[])
                {
                                Student s1=new Student(1,"Ajay");
                                Student s2=new Student("Mitesh");
                                Student s3=new Student("Mitesh");
                                                               
                                s1.exam(50,60,12);
                                s2.exam(60,70);
                                s3.exam(30);
                }

}