Showing posts with label Object Oriented Programming. Show all posts
Showing posts with label Object Oriented Programming. Show all posts

Thursday, August 30, 2012

Java Program to Write Lines of Text to a File + Source Code

To learn the Basics of file manipulation in java ,the best way to start is by writing simple code to write some text to file.The following is a sample program that writes text to file using BufferedWriter Class

It uses the File Class to open the File to the BufferedWriter Class,which writes some sample text to the file.The main class used is the Writer Class intended for writing stream data to files.

Below is the complete source Code to implement a Simple File Writer in Java


 import java.io.*;  
 public class FileWriter {  
  public static void main(String[] args)throws IOException{  
  String towrite = "Hello World";  
  Writer data = null;  
  File file = new File("myfile.txtt");  
  data = new BufferedWriter(new FileWriter(file));  
  data.write(text);  
  data.close();  
  System.out.println("Text Written");   
  }  
 }  

Tuesday, May 22, 2012

Program to Implement Selection Sort in Java+Explanation



Selection sort is said to be an efficient algorithm when considering small computations involving limited memory/resource but it is quite inefficient when compared to other techniques when huge computations are required.Selection sort is said to be efficient for small list of items because of its simplicity.

The following steps Explains the working of the Selection sort Algorithm


  1. Obtain the Item with the Minimal value from a List/Collection of items
  2. Swap the item with the Item in the first position of the Collection/List
  3. Repeat the Process for the rest of the Items in the Collection


The Following is the Complete Program Source Code in Java for Implementing Selection Sort Algorithm
 import java.io.*;
import java.lang.*;
class array
{
 DataInputStream get;
 int a[];
 int i,j,n;
 void getdata()
 {
 try
  {
   get=new DataInputStream(System.in);
   System.out.println("Enter the limit");
   n=Integer.parseInt(get.readLine());
   a=new int[n];
   System.out.println("Enter the elements in the array");
   for(i=0;ia[j])
   {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
   }
  }
 }
 System.out.println("Elements in ascending order is:");
 for(i=0;i=0;i--)
 System.out.print(" "+a[i]);
 }
}
class selectionsort
{
 public static void main(String arg[])
 {
  array obj=new array();
  obj.getdata();
  obj.sorting();
 }
}

Wednesday, March 14, 2012

Java Program -1 way Client-Server Communication using TCP/IP



In java Networking is done using Sockets and ServerSockets.To get a good idea of how sockets are used in java for creating a client server model see the article. http://c-madeeasy.blogspot.com/2012/03/concept-of-sockets-and-networking-in.html

TCP/IP(Transmission Control Protocol/Internet Protocol) :
Is connection based protocol that is widely used over the internet.It is commonly referred to as IP.The following program uses TCP/IP to communicate.


After reading the above article,see the code below.You can see how socket and server sockets are used to create a one way client server program.Here a client can communicate with the server only.Both are DOS/Terminal Java Programs.

Fist you start the server program followed by client program in two separate terminals.In the client terminal type anything,you can see that appearing in the server terminal window.

The complete Java Program source code to implement 1 way client and server model is provided below as two separate programs Client.java and Server.Java

 //Server Program  
 //c-madeeasy.blogspot.com www.codeuniverse.tk  
 import java.io.*;  
 import java.net.*;  
 class server{  
 public static void main(String []args)  
 {  
 String data;  
 ServerSocket ssock;  
 Socket clientsock=null;  
  DataInputStream is;  
 try{  
 ssock=new ServerSocket(2000);  
 System.out.print("Server Started");  
 clientsock=ssock.accept();  
 is=new DataInputStream(clientsock.getInputStream());  
 System.out.println("Connection Accepted");  
 while(true)  
 {  
 data=is.readLine();  
 System.out.println(data);  
 }  
 }  
 catch(Exception e)  
 {  
 System.out.println("ERROR");  
 }  
 }  
 }  
 ---------------------------------------------------------------  
 //Client Program  
 import java.io.*;  
 import java.net.*;  
 class client{  
 public static void main(String []args)  
 {  
 String text;  
 Socket sock=null;  
  DataOutputStream dout;  
  PrintStream sender;  
  DataInputStream keyboardreader;  
  System.out.println("Connecting to Server.....");  
  try{  
   sock=new Socket("localhost",2000);  
   }  
  catch(Exception e)  
  {  
  }  
  try{  
  System.out.println("Connected");  
  keyboardreader=new DataInputStream(System.in);  
  sender=new PrintStream(sock.getOutputStream());  
  do  
  {  
  text=keyboardreader.readLine();  
  sender.println(text);  
  }while(!text.equals("quit"));  

Thursday, March 8, 2012

Hybrid Inheritance Example Program in C++ to Calculate the Marks and Percentage of a Student



Hybrid Inheritance is the combination of two or more inheritances : single, multiple,multilevel or hierarchical Inheritances. The following is a C++ Program to for Calculating the marks secured by a student.A Parent class with student identification is created and another class called marks is inherited from the main class.This class marks is further inherited by another class called sports and finally the sports class is inherited by the percentage class to calculated the percentage of marks.This is the best example for Hybrid Inheritance in c++The complete source code is provided Below 



 #include<iostream.h>  
 #include<conio.h>  
 #include<stdio.h>  
 #include<string.h>  
 class student_id  
 {  
 //c-madeeasy.blogspot.com  
 int rno;  
 char name[20];  
 public:  
 void read_id()  
 {  
 cout<<"\n\nEnter the Name of the Student : ";  
 gets(name);  
 cout<<"\n\nEnter the Roll No. : ";  
 cin>>rno;  
 }  
 void display_nr()  
 {  
 cout<<"\n\n\t\tSTUDENT REPORT\n\nNAME : ";  
 puts(name);  
 cout<<"\n\nROLL NO. : "<<rno;  
 }  
 };  
 class marks:public student_id  
 {  
 public:  
 int i,mark[3];  
 void read_m()  
 {  
 read_id();  
 for(i=0;i<3;i++)  
 {  
 cout<<"\n\nEnter the Marks Secured in SUBJECT "<<i+1<<" out of 100 : ";  
 cin>>mark[i];  
 }  
 }  
 void display_m()  
 {  
 display_nr();  
 cout<<"\n\n\tMarks Secured ";  
 for(i=0;i<3;i++)  
 cout<<"\n\nSUBJECT "<<i+1<<" : "<<mark[i];  
 }  
 };  
 class sports  
 {  
 public:  
 int sm;  
 void read_sportm()  
 {  
 cout<<"\n\nEnter the marks in SPORTS out of 10 : ";  
 cin>>sm;  
 }  
 };  
 class percentage:public marks,public sports  
 {  
 public:  
 float total,prcntge;  
 void calculate()  
 {  
 read_m();  
 read_sportm();  
 total=0;  
 for(i=0;i<3;i++)  
 {  
 total+=mark[i];  
 }  
 total+=sm;  
 prcntge=(total/310)*100;  
 }  
 void display_totp()  
 {  
 display_m();  
 cout<<"\n\nTOTAL = "<<total;  
 cout<<"\n\nPERCENTAGE = "<<prcntge;  
 }  
 };  
 void main()  
 {  
 int cont;  
 percentage pc;  
 clrscr();  
 do  
 {  
 pc.calculate();  
 clrscr();  
 pc.display_totp();  
 cout<<"\n\nDo You Want to Continue?(1-YES/0-NO)";  
 cin>>cont;  
 }while(cont==1);  
 getch();  
 }  

Wednesday, February 8, 2012

Vector Addition,Multiplication Program using Vector Class and Operator Overloadingin C++

Classes help to develop easier and effective programming style.To get a better understanding of classes and the concept of abstraction see http://c-madeeasy.blogspot.in/2011/12/what-is-abstractionthe-concept-of.html 

In this program a class named Vector is used to receive data in the form of a vector and + and * operators are overloaded to add and multiply these vectors.The complete Source Code of the C++ program to perform vector addition and multiplication using operator overloading is provided below.
 #include<iostream.h>  
 #include<conio.h>  
 class vector  
 {  
 //Visit c-madeeasy.blogspot.com  
 public:  
 int x,y,z;  
 void read()  
 {  
 cout<<"\n\nEnter the magnitude of i : ";  
 cin>>x;  
 cout<<"\n\nEnter the magnitude of j : ";  
 cin>>y;  
 cout<<"\n\nEnter the magnitude of k : ";  
 cin>>z;  
 }  
 vector operator -()  
 {  
 x=-x;  
 y=-y;  
 z=-z;  
 }  
 vector operator +(vector b)  
 {  
 vector c;  
 c.x=x+b.x;  
 c.y=y+b.y;  
 c.z=z+b.z;  
 return c;  
 }  
 vector operator *(vector b)  
 {  
 int prod;  
 vector c;  
 c.x=x*b.x;  
 c.y=y*b.y;  
 c.z=z*b.z;  
 prod=c.x+c.y+c.z;  
 cout<<"\nScalar Product = ";  
 cout<<prod;  
 vector d;  
 d.x=(y*b.z)-(z*b.y);  
 d.y=(x*b.z)-(z*b.x);  
 d.z=(x*b.y)-(y*b.x);  
 return d;  
 }  
 void display()  
 {  
 cout<<x<<"i"<<"+"<<y<<"j"<<"+"<<z<<"k";  
 }  
 };  
 void main()  
 {  
 vector v1,v2,v3;  
 int choice,cont;  
 do  
 {  
 clrscr();  
 cout<<"\n\tVECTORS\n\n1.Negation of The Vector\n\n2.Sum of Vecors\n\n3.Product of Vectors\n\n";  
 cout<<"\nEnter your choice : ";  
 cin>>choice;  
 switch(choice)  
 {  
 case 1 : cout<<"\nEnter the Vector\n";  
            v1.read();  
            -v1;  
            cout<<"Negation of the Vector : ";  
            cout<<v1.x<<"i"<<v1.y<<"j"<<v1.z<<"k";  
            break;  
 case 2 : cout<<"\n\nEnter the First Vector : ";  
            v1.read();  
            cout<<"\n\nEnter the Second Vector : ";  
            v2.read();  
            v3=v1+v2;  
            cout<<"\n\nThe Sum of Vectors : ";  
            v3.display();  
            break;  
 case 3 : cout<<"\n\nEnter the First Vector : ";  
            v1.read();  
            cout<<"\n\nEnter the Second Vector : ";  
            v2.read();  
            v3=v1*v2;  
            cout<<"\n\nVector Product = ";  
            v3.display();  
            break;  
 default : cout<<"\n\nInvalid Choice";  
 }  
 cout<<"\n\n\nDo you want to continue?(1-YES,0-NO)\n";  
 cin>>cont;  
 }while(cont==1);  
 }  

Thursday, January 12, 2012

Operator Overloading in C++ to add,substract,multiply and divide two Complex Numbers

Operator Overloading is a technique of polymorphism by which an operator(+,- etc) can be used to do different types of operations. eg:+ can be used to add two integers say a and b using sum=a+b similarly two floating point numbers say fa,fb by fs=fa+fb. In this example +,-,*,- operators are overloaded to add,subtract, multiply and divide two complex numbers. First a class named complex is created which has overloaded operators like + specified by a function 'complex operator +(complex c2)' which can used to add two complex numbers like c3=c1+c2 where c1,c2 and c3 are complex numbers.The complete source in C++ to implement operator overloading is provided below
 #include<iostream.h>  
 #include<conio.h>  
 class complex  
 {  
 int a,b;  
 public:  
 void read()  
 {  
 cout<<"\n\nEnter the REAL PART : ";  
 cin>>a;  
 cout<<"\n\nEnter the IMAGINARY PART : ";  
 cin>>b;  
 }  
 complex operator +(complex c2)  
 {  
 complex c3;  
 c3.a=a+c2.a;  
 c3.b=b+c2.b;  
 return c3;  
 }  
 complex operator -(complex c2)  
 {  
 complex c3;  
 c3.a=a-c2.a;  
 c3.b=b-c2.b;  
 return c3;  
 }  
 complex operator *(complex c2)  
 {  
 complex c3;  
 c3.a=(a*c2.a)-(b*c2.b);  
 c3.b=(b*c2.a)+(a*c2.b);  
 return c3;  
 }  
 complex operator /(complex c2)  
 {  
 complex c3;  
 c3.a=((a*c2.a)+(b*c2.b))/((c2.a*c2.a)+(c2.b*c2.b));  
 c3.b=((b*c2.a)-(a*c2.b))/((c2.a*c2.a)+(c2.b*c2.b));  
 return c3;  
 }  
 void display()  
 {  
 cout<<a<<"+"<<b<<"i";  
 }  
 };  
 void main()  
 {  
 complex c1,c2,c3;  
 int choice,cont;  
 do  
 {  
 clrscr();  
 cout<<"\t\tCOMPLEX NUMBERS\n\n1.ADDITION\n\n2.SUBTRACTION\n\n3.MULTIPLICATION\n\n4.DIVISION";  
 cout<<"\n\nEnter your choice : ";  
 cin>>choice;  
 if(choice==1||choice==2||choice==3||choice==4)  
 {  
 cout<<"\n\nEnter the First Complex Number";  
 c1.read();  
 cout<<"\n\nEnter the Second Complex Number";  
 c2.read();  
 }  
 switch(choice)  
 {  
 case 1     : c3=c1+c2;  
            cout<<"\n\nSUM = ";  
            c3.display();  
            break;  
 case 2     : c3=c1-c2;  
            cout<<"\n\nResult = ";  
            c3.display();  
            break;  
 case 3 : c3=c1*c2;  
            cout<<"\n\nPRODUCT = ";  
            c3.display();  
            break;  
 case 4     : c3=c1/c2;  
            cout<<"\n\nQOUTIENT = ";  
            c3.display();  
            break;  
 default     : cout<<"\n\nUndefined Choice";  
 }  
 cout<<"\n\nDo You Want to Continue?(1-Y,0-N)";  
 cin>>cont;  
 }while(cont==1);  
 getch();  
 }  

Which is the Best Photo Watermarking Software

Photo Theft is becoming more and more common in the web with the outburst of social websites like Facebook,Google Plus and Image sharing se...