Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Sunday 15 July 2012

JAVA PROGRAMS

       Java with Mysql Connectivity simple example


  1. Observe the interactions involved in the process of booking a railway ticket. Identify the various objects involved and the interactions between the objects in order to solve a problem of booking a railway ticket.
  2. Write a simple java application to print a pyramid with 5 lines. The first line has one character, 2nd line  as two characters and so on. The character to be used in the pyramid is taken as a command line argument.
  3. Write a Java application which takes several command line arguments, which are supposed to be names of students and prints output as given below:(Suppose we enter 3 names then output should be as follows):Number of arguments = 31: First Student Name is =Tom2: Second Student Name is =Dick3: Third Student Name is =HarryHint: An array may be used for converting from numeric values from 1 to 20 into String.
  4. Write a class, with main method, which declares floating point variables and observe the output of dividing the floating point values by a 0, also observe the effect of assigning a high integer value (8 digits and above) to a float and casting it back to int and printing.
  5. Write a class called Statistics, which has a static method called average, which takes a onedimensional array for double type, as parameter, and prints the average for the values in the array. Now write a class with the main method, which creates a two-dimensional array for the four weeks of a month, containing minimum temperatures for the days of the week(an array of 4 by 7), and uses the average method of the Statistics class to compute and print the average temperatures for the four weeks.
  6. Define a class called Product, each product has a name, a product code and manufacturer name.Define variables, methods and constructors, for the Product class. 
  7. Write a class called Test Product, with the main method to test the methods and constructors of the Product class. Define a class called Cartesian Point, which has two instance variables, x and y. Provide the methods get X() and get Y() to return the values of the x and y values respectively, a method called move() which would take two integers as parameters and change the values of x and y respectively, a method called display() which would display the current values of x and y. Now overload the method move() to work with single parameter, which would set both x and y to the same values, .Provide constructors with two parameters and overload to work with one parameter as well. Now define a class called Test Cartesian Point, with the main method to test the various methods in the Cartesian Point class.
  8. Define a class called Triangle, which has constructor with three parameters, which are of type Cartesian Point, defined in the exercise 7. Provide methods to find the area and the perimeter of the Triangle, a method display() to display the three Cartesian Points separated by ':' character, a method move() to move the first Cartesian Point to the specified x, y location, the move should take care of relatively moving the other points as well, a method called rotate, which takes two arguments, one is the Cartesian Point and other is the angle in clockwise direction. Overload the move method to work with Cartesian Point as a parameter. Now define a class called Test Triangle to test the various methods defined in the Triangle class. Similarly also define a class called Rectangle which has four Cartesian Point.
  9. Override the to String, equals and the hash Code methods of the classes Triangle and Rectangle defined in exercises 7 and 8 above, in appropriate manner, and also redefine the display methods to use the to String method.
  10. Define an abstract class called Polygon. Provide a constructor which takes an array of Cartesian Point as parameter. Also provide method called perimeter, which calculates and returns the perimeter of the Polygon. Declare abstract method area for this class. Also define a method called move, which takes two parameters x and y to specify the destination for the first point of the Polygon, and overload to make it work for Cartesian Point as a parameter. Now update the classes Triangle and Rectangle in the exercise 8 above, to be a subclass of the Polygon class. Write appropriate class with main method to test the polymorphism in the area method.
  11. Make the class Cartesian Point, belong to a package called edu. gtu. geometry, the classes Polygon, Triangle and Rectangle belong to the package edu. gtu. geometry. shapes and the classes Test Cartesian Point, Test Triangle, Test Rectangle and Test Polygon belong to the package edu. gtu.test. Use appropriate access specifiers for the classes and the members of the classes defined in the earlier exercises. Now onwards all the classes must be defined in a package.
  12. Update the classes Triangle and Rectangle, to throw an exception if the Cartesian Point instances passed as parameter does not specify an appropriate Triangle or Rectangle. e.g. In case of Triangle, if the three points are in a straight line, or in case of Rectangle, if the lines when connected cross each other.
  13. Define a class called Polygon Manager, which manages a number of Polygon instances. Provide methods to add, remove and list the Polygon instances managed by it. Test the methods of Polygon Manager by writing appropriate class with main method.
  14. Define a class called Statistical Data which manages a number of readings of type int. Provide a method to set Data available in the form of an array, a method add to individually add data of type int, a method to reset the entire data, and methods to return the following statistics:1. mean2. median3. mode4. variance5. standard deviation6. specified percentile (between 0 – 100) the method should have a parameter.7. specified quartile (between 1 - 3) the method should have a parameter.8. interquartile rangeUse this class to calculate the given statistics for the data regarding the total of marks obtained by all the students in your class in the previous exams. The data may be accepted one by one for each student from the keyboard.
  15. Update the class Statistical Data, and define a method called load From CSV, which takes as parameter an Input Stream, where numeric data is available in an ASCII format, in a comma separated form. Overload this method to take a File instance as parameter. Test the new methods using appropriate data. 
  16. A college maintains the information about the marks of the students of a class in a text file with fixed record length. Each line in the file contains data of one student. The first 25 characters have the name of the student, next 12 characters have marks in the four subjects, each subject has 3 characters. Create a class called Student Marks, which has student Name, and marks for four subjects. Provide appropriate getter methods and constructors, for this class. Write an application class to load the file into an array of Student Marks. Use the Statistical Data class to compute the statistics mean, median, mode, standard deviation and itnerquartile range for each of the subjects in the class.
  17. In the above exercise, use multithreading, to compute the statistics, after loading the Student Marks from the file, for marks information available for different classes available from files placed in a directory. Create at least five files in a directory with fixed record length to test your code.
  18. In the class Statistics defined earlier add method to find the percentile for a given data value. Use this to find your percentile ranking your class, during the last exam.
  19. A Frequency Distribution is a tabular summary of data showing the number (frequency) of items in each of several nonoverlapping classes. These classes may represent qualitative or quantitative data. Create a class called Frequency Distribution, which provides a method called load From File, to load data Strings from a file, and compute the Frequency Distribution for the various kinds of Strings available from the file. The class should provide a method to print the frequency distribution, and another to print the percent frequency distribution in a tabular manner. Test this class by using the sample data available regarding purchases of best selling paperback business book available from a file containing the abbreviated names of the book separated by white spaces. 
  20. Create subclass of the Frequency Distribution class called Frequency Distribution Quantitative, which provides method called add Class Range, which takes two numbers as parameter. The class should maintain a number of class ranges, and should ensure that the class ranges are nonoverlapping. The load From File method should be overridden to compute frequency distribution of numeric data loaded from the file, it should discard the data which does not fall in any of the class ranges. Test this class by using the sample data available regarding the waiting times of patient arriving at a clinic of a doctor for emergency service.
  21. Update the FrequencyDistributionQuantitative class defined in exercise 19, and adds a method to reset the data and the class ranges. Also add a method called set Range Interval, which specifies the size of each class Range and the starting value, this should be used at building the nonoverlapping class ranges as the data is loaded, now only the data less than the starting value may be discarded while loading numeric data from the file. Test this class by using the data regarding the salary of marketing executives available in a file. Set the starting value as 5,000 and have intervals of 500.
  22. Create a class called Stem and Leaf, which should have a provision to maintain the stem size and the number of intervals between the two stems. It should also have a method to load the data from the file and a method to display the stem-and-leaf diagram for the loaded data. Test the class to display data regarding the marks of students of your class in the previous exam. 
  23. Create a class called Pie Canvas, which subclasses from the java. awt. Canvas class, and overrides the paint method to display the Pie Chart for the data available from an instance of Frequency Distribution. Provide a method to set the instance of Frequency Distribution, on the Canvas. Test this class by adding it into a Frame and using the Frequency Distribution instance created for data regarding the top selling books prepared for exercise 18.
  24. Create a class called Statistical Data, which has capability of maintaining data regarding multiple variables. It should have a method to specify the variable names as String array and the method to load values from a file regarding the variables. eg. We consider two variables as percentage of marks in GCET exam and percentage of marks in 1st year of MCA, Provide methods in the class to compute the correlation coefficient between any two variables, specified in the parameter. Test the class by computing the correlation coefficient between the marks of GCET and marks of 1st year MCA for students of your class.
  25. Simple random sampling uses a sample of size n from a population fo size N to obtain data that can be used to make inferences about the characteristics of a population. Suppose that, from a population of 50 bank accounts, we want to take a random sample of four accounts in order to learn about the population. How many different random samples of four accounts are possible. Write a Java class which can compute the number of random samples for size n from a population of N, Also provide method to display all possible samples, and also a method to return a sample, derived from the possible samples. (Hint use random method of the Math class to select the random sample).
  26. Dasher is an information-efficient text-entry interface, driven by natural continuous pointing gestures. Dasher is a competitive text-entry system wherever a full-size keyboard cannot be used. Dasher is a zooming interface. You point where you want to go, and the display zooms in wherever you point. The world into which you are zooming is painted with letters, so that any point you zoom in on corresponds to a piece of text. The more you zoom in, the longer the piece of text you have written. You choose what you write by choosing where to zoom. To make the interface efficient, we use the predictions of a language model to determine how much of the world is devoted to each piece of text. Probable pieces of text are given more space, so they are quick and easy to select. Improbable pieces of text (for example, text with spelling mistakes) are given less space, so they are harder to write. The language model learns all the time: if you use a novel word once, it is easier to write next time. Write a class in Java which models the probabilities for the alphabets in a language as the starting alphabet and then the probatilities for the subsequent alphabets which may follow the alphabet and so on leading to the end of word. The probabilities may be built by reading a text file containing lots of texts in the given language. (Hint: create a class to represent a node, which has an alphabet and its probability and each node has capability of maintaining a list of subsequent nodes). (Advanced question)
  27. Write a class in Java to plot a Histogram for the data available from the instance of Statistical Data class defined in exercise 24 earlier. Make a provision for selecting the variables for which Histogram is to be plotted. 
  28. Write a class in Java to plot a Scatter diagram for the data available from the instance of Statistical Data class defined in exercise 24 earlier. Make a provision for selecting the variables for which scatter diagram is to be plotted.
  29. Define a class which implements the Table Model for displaying the data available from the Statistical Data instance of exercise 24, in a tabular form. Use instance of this model to create an instance of J Table and display the data of exercise 24 in tabular format. 
  30. Create an applet which has a Text Field to accept a URL string, and displays the document of the URL string in a new browser window.

Contact Us

Pankaj Sharma ( 8460479175 )
Chavda Vijay (8460420769 ) 
Students
JVIMS, Jamangar


Email Us @ gtumasterlogic.gmail.com

You can comment us as Anonymous by selecting as "Anonymous" in comment as field while commenting

Thank You

Define a class called Statistical Data which manages a number of readings of type int. Provide a method to set Data available in the form of an array, a method add to individually add data of type int, a method to reset the entire data and methods to return mean, median, mode, variance, standard deviation, percentile, quartile, inter quartile range


[ Statistical_data.java ]
import java.io.*;
class Statistical_data
{
   int data[];
   Statistical_data()   
{
    data=new int[1];
   }
   void setdata(int value[]) 
{
    data=new int[value.length];      
    for(int i=0;i<data.length;i++)         
{
     data[i]=value[i];
    }
   }
   int add()  
{
    int sum=0,i;
    for(i=0;i<data.length;i++)        
{
     sum=sum+data[i];
    }
    return sum;
   }
   void reset()    
{
    for(int i=0;i<data.length;i++)         
{
     data[i]=0;
    }
    data=new int[1];
   }
   double mean()   
{
    return (double)this.add()/data.length;
   }
   double median() 
{
    int size,n=0;
    double median;
    size=data.length;
    this.sort();
   
    if (size%2 == 0)         
{        
     n=size/2 - 1 ;
     median=((double)data[n] + data[n+1])/2;
    }
    else      
{
     System.out.println("ODD" + size);
     n=(size)/2;
     median=data[n];
    }        
    return median;
   }
   double mode()  
{
    double mode;
    mode= ( (double)3*median() ) - ( 2*mean() );
    return mode;
   }
   double variance()    
{
    int size=data.length;
    double mean=this.mean(),sum=0;
    for(int i=0;i<size;i++)
    {
     sum=sum+Math.pow((data[i]-mean),2);
    }
    return sum/(size-1);
   }
   double standard_deviation()    
{
    return Math.sqrt(this.variance());
   }
   double percentile(int no)
{
    double perc=0;
    int size=data.length, temp;
   
    if(no<0 && no>100)      
{   
     System.out.println("Percentile should be more than 0 and less than 100..");
     perc=-1;
    }
    else     
{
     temp=(no*size)/100;
     if( temp == (double)(no*size)/100)             
{
          perc=(double)(data[temp]+data[temp-1])/2;
     }
     else          
{
          perc=(double)data[temp];
     }
    }
    return perc;
   }
   double quartile(int no)  
{
    double quart=0;
    int size=data.length, temp;
   
    if(no<1 && no>3)        
{   
     System.out.println("Quartile should be more than 1 and less than 3..");
     quart=-1;
    }
    else      
{
     if(no==1)
          no=25;
     else if(no==2)
          no=50;
     else
          no=75;
     temp=(no*size)/100;
     if( temp == (double)(no*size)/100)            
  {
          quart=(double)(data[temp]+data[temp-1])/2;
     }
     else          
{
          quart=(double)data[temp];
     }
    }
    return quart;
   }
   double IQR()   
{
    return this.quartile(3)-this.quartile(1);
   }
   void sort() 
{
    int temp,size;
    size=data.length;
    for(int i=0;i<size;i++)
    {
     for(int j=0;j<size;j++)
     {
          if(data[i]<data[j])
          {
              temp=data[i];
              data[i]=data[j];
              data[j]=temp;
          }
     }
    }
   }
}
class student{
   int rollno,marks[];
   String name;
   student()
   {
    marks=new int[6];
   }
   void getdata()  throws IOException
   {
    name=new String();
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter RollNo");
    rollno=Integer.parseInt(br.readLine());
    System.out.println("Enter Name");
    name=br.readLine();
    System.out.println("Enter Marks");
    for(int i=0;i<6;i++)
    {
     System.out.println("Subject " + (i+1) + " : ");
     marks[i]=Integer.parseInt(br.readLine());
    }
   }
   int gettotalmarks()
   {
    int total=0;
    for(int i=0;i<6;i++)
    {
     total=total+marks[i];
    }
    return total;
   }
}
class teststats{
   public static void main(String args[]) throws IOException
   {
    int total[],no;
     student stud[];
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("Enter Number of Students:");
     no=Integer.parseInt(br.readLine());
     stud=new student[no];
     total=new int[no];
     for(int i=0;i<no;i++)
     {
      stud[i]=new student();
      stud[i].getdata();
     }
     for(int i=0;i<no;i++)
     {
      total[i]=stud[i].gettotalmarks();
     }       
    total=new int[args.length];      
    for(int i=0;i<total.length;i++)
    {
     total[i]=Integer.parseInt(args[i]);
    }   
    Statistical_data stats=new Statistical_data();
    stats.setdata(total);
    System.out.println("MEAN:"+stats.mean());
    System.out.println("MEDIAN:"+stats.median());
    System.out.println("MODE:"+stats.mode());      System.out.println("PERCENTILE:"+stats.percentile(25));
    System.out.println("VARIANCE:"+stats.variance());
    System.out.println("STANDARD DEVIATION:"+stats.standard_deviation());
    System.out.println("Q3:"+stats.quartile(3));
    System.out.println("Q2:"+stats.quartile(2));
    System.out.println("Q1:"+stats.quartile(1));
    System.out.println("INTERQUARTILE DEVIATION:"+stats.IQR());
   }
}

Write a class in Java to plot a Scatter diagram for the data available from the instance of Statistical Data class defined in exercise 24 earlier. Make a provision for selecting the variables for which scatter diagram is to be plotted.


[ scatter_dig.java ]
import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="scatter_dig" width=500 height=400>
</applet>
*/
public class scatter_dig extends Applet
{
   int no[],size;
   int h,w;
   public void init()
   {
    int i=0;
    no=new int[100];
    String line;
    try
    {
     File f=new File("E:\\SCATTER\\abc.txt");
     BufferedReader br=new BufferedReader(new FileReader(f));
     while( (line=br.readLine())!=null)
     {
          no[i]=Integer.parseInt(line);
          i++;
     }
     size=i;
    }
    catch(Exception e)
    {
    }
   }
   public void paint(Graphics g)
   {
    h=getHeight();
    w=getWidth();
    drawPlane(g);
    drawScatter(g);
    g.setFont(new Font(Font.SERIF,Font.BOLD,27));
    g.drawString("SCATTER DIAGRAM",w/2-100,20);
    g.setFont(new Font(Font.SERIF,Font.PLAIN,18));
    g.drawString("--Pankaj Sharma",w/2,40);
    showStatus("pankaj Sharma");
   }
   public void drawPlane(Graphics g)
   {
    int linex=50,liney=h-50;
    int diff=((h-100)/10);
    g.drawLine(50,50,50,h-50);
    g.drawLine(50,h-50,w-50,h-50);
    for(int i=0;i<=10;i++)
    {
     g.drawLine(linex-10,liney,linex,liney);
     g.drawString(i*10+"",linex-20,liney);
     liney=liney-diff;
    }
    for(int i=0;i<size+5;i++)
    {
     g.drawLine(linex,h-50,linex,h-40);
     g.drawString(i+"",linex-5,h-25);
     linex=linex+20;
    }
   }
   public void drawScatter(Graphics g)
   {
    int pointx=50;
    for(int i=0;i<size-1;i++)
    {
     g.drawLine(pointx,h-50-((h-100)*no[i])/100,pointx+20,h-50-((h-100)*no[i+1])/100);
     pointx=pointx+20;
    }
   }
}

Create an applet which has a Text Field to accept a URL string, and displays the document of the URL string in a new browser window.


[ suftApplet.java ]
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
/*
<applet code="surfApplet" height="500" width="500">
</applet>
*/
public class surfApplet extends Applet implements ActionListener
{
TextField tfURL;
Button bShowURL;
public void init()
{
tfURL = new TextField (30);
bShowURL = new Button ("Show Web");
bShowURL.addActionListener (this);
add (tfURL);
add(bShowURL);
}
public void actionPerformed (ActionEvent ae)
{
try
{
   AppletContext ac = getAppletContext();
  URL link=null;
  link = new URL ( tfURL.getText() );
    ac.showDocument (link, "_blank");
  showStatus("By Pankaj Sharma");
}
catch (Exception ex) {}
}
public void paint(Graphics g)
{
showStatus("www.master-gtu.blogspot.com");
}
}

Create a class called Stem and Leaf, which should have a provision to maintain the stem size and the number of intervals between the two stems. It should also have a method to load the data from the file and a method to display the stem-and-leaf diagram for the loaded data. Test the class to display data regarding the marks of students of your class in the previous exam.


[ stemleaf.java ]
import java.io.*;
class stemleaf
{
public static void main(String args[]) throws IOException
{
int i=0;
int no[]=new int[100];
String line;
File f=new File(args[0]);
/*
// WRITING DATA TO FILE
System.out.println("WRITING DATA TO FILE");
BufferedWriter br=new BufferedWriter(new FileWriter(f));
while(i<10)
{
line=""+i;
br.write(line,0,line.length());
br.newLine();
i++;
}
br.close();
*/
System.out.println("READING FROM FILE");
BufferedReader bf=new BufferedReader(new FileReader(f));
int j=0,temp;
while(  (line=bf.readLine())!=null)
{
temp=Integer.parseInt(line);
if(temp<1000 && temp>=10)
{
no[j]=Integer.parseInt(line);
j++;
}
}
int leaf,stem;
sort(no,j);
System.out.println("===========================");
System.out.println("STEM \t | \t LEAF");
System.out.println("===========================");
for(i=0; i<j;)
{
stem=no[i]/10;
System.out.print(stem + "\t | \t" );
while(no[i]/10==stem)
{
leaf=no[i]%10;
System.out.print(leaf);
if (no[i+1]/10==stem)
{
System.out.print(",");
}
i++;
}
System.out.println();
}
System.out.println("===========================");
}
static void sort(int arr[],int size)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(arr[i]<arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
}

Update the classes Triangle and Rectangle, to throw an exception if the Cartesian Point instances passed as parameter does not specify an appropriate Triangle or Rectangle. e.g. In case of Triangle, if the three points are in a straight line, or in case of Rectangle, if the lines when connected cross each other.


testtriangle .java ]

import graphics.*; //TO IMPORT CARTESIANPOINT CLASS
import java.io.*;
import java.lang.*;

class ClassNotMatchException extends Throwable
{
void display()
{
System.out.println("Class Not Math Exception Found");
}
}
class triangle
{
CartesianPoint p1,p2,p3;
triangle(CartesianPoint p1,CartesianPoint p2,CartesianPoint p3)
{
this.p1=p1;
this.p2=p2;
this.p3=p3;
}
public void display()
{
System.out.println("\nPOINT 1 : ( " + this.p1.getx() + " : " + this.p1.gety() + " )");
System.out.println("\nPOINT 2 : ( " + this.p2.getx() + " : " + this.p2.gety() + " )");
System.out.println("\nPOINT 3 : ( " + this.p3.getx() + " : " + this.p3.gety() + " )");
}
public void move() throws IOException
{
double newx,newy,diffx,diffy;
System.out.println("Enter New Cordinates of Point 1");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter x-Cordinates :");
newx=Integer.parseInt(br.readLine());
System.out.println("Enter y-Cordinates :");
newy=Integer.parseInt(br.readLine());

diffx=newx-p1.getx();
diffy=newy-p1.gety();

p1.move(newx,newy);
p2.move(p2.getx() + diffx,p2.gety() + diffy);
p3.move(p3.getx() + diffx,p3.gety() + diffy);
}
public void move(Object p1) throws IOException
{
try
{
if( !(p1 instanceof CartesianPoint))
{
ClassNotMatchException obj;
obj=new ClassNotMatchException();
throw obj;
}
else
{
double newx,newy,a,b,c;
System.out.println("Enter New Cordinates of Point 1");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter x-Cordinates :");
newx=Integer.parseInt(br.readLine());
System.out.println("Enter y-Cordinates :");
newy=Integer.parseInt(br.readLine());
// p1.move(newx,newy);
this.p1.move(newx,newy);
}
}
catch(ClassNotMatchException obj)
{
obj.display();
}
}
public void area()
{
double a,b,c,s,perimeter,area;

a=Math.sqrt(Math.pow((p1.getx()-p2.getx()),2)+Math.pow((p1.gety()-p2.gety()),2));
b=Math.sqrt(Math.pow((p1.getx()-p3.getx()),2)+Math.pow((p1.gety()-p3.gety()),2));
c=Math.sqrt(Math.pow((p2.getx()-p3.getx()),2)+Math.pow((p2.gety()-p3.gety()),2));
s=(a+b+c)/2;

area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("Area of Triangle is : " + area);
}
public void perimeter()
{
double a,b,c,perimeter;

a=Math.sqrt(Math.pow((p1.getx()-p2.getx()),2)+Math.pow((p1.gety()-p2.gety()),2));
b=Math.sqrt(Math.pow((p1.getx()-p3.getx()),2)+Math.pow((p1.gety()-p3.gety()),2));
c=Math.sqrt(Math.pow((p2.getx()-p3.getx()),2)+Math.pow((p2.gety()-p3.gety()),2));

perimeter=a+b+c;
System.out.println("Perimeter of Triangle is : " + perimeter);
}
public void rotate(CartesianPoint p1,double x)
{
double newx,newy;
newx=(p1.getx() * Math.cos(x)) - (p1.gety() * Math.sin(x));
newy=(p1.getx() * Math.sin(x)) + (p1.gety() * Math.cos(x));
p1.move(newx,newy);
}
public String toString()
{
return "p1(x):"+p1.getx()+" ; p1(y):"+p1.gety()+" ; p2(x):"+p2.getx()+" ; p2(y):"+p2.gety()+" ; p3(x):"+p3.getx()+" ; p3(y):"+p3.gety();
}
public boolean equals(Object tempobj)
{
if(!(tempobj instanceof triangle))
return false;
if(this.p1.getx() != ((triangle)tempobj).p1.getx() && this.p1.gety() != ((triangle)tempobj).p1.gety())
return false;
if(this.p2.getx() != ((triangle)tempobj).p2.getx() && this.p2.gety() != ((triangle)tempobj).p2.gety())
return false;
if(this.p3.getx() != ((triangle)tempobj).p3.getx() && this.p3.gety() != ((triangle)tempobj).p3.gety())
return false;
return true;
}
}
class rectangle
{
CartesianPoint p1,p2,p3,p4;
rectangle(CartesianPoint p1,CartesianPoint p2,CartesianPoint p3,CartesianPoint p4)
{
double a,b,c,d;
this.p1=p1;
this.p2=p2;
this.p3=p3;
this.p4=p4;
a=Math.sqrt(Math.pow((p1.getx()-p2.getx()),2)+Math.pow((p1.gety()-p2.gety()),2));
b=Math.sqrt(Math.pow((p1.getx()-p4.getx()),2)+Math.pow((p1.gety()-p4.gety()),2));
c=Math.sqrt(Math.pow((p2.getx()-p3.getx()),2)+Math.pow((p2.gety()-p3.gety()),2));
d=Math.sqrt(Math.pow((p3.getx()-p4.getx()),2)+Math.pow((p3.gety()-p4.gety()),2));
if ( !((a==c && b==d) || (a==b && c==d) || (a==d && b==c) ))
{
System.out.println("IT IS NOT A RECTANGLE....");
}
}
public void display()
{
System.out.println("\nPOINT 1 : ( " + this.p1.getx() + " : " + this.p1.gety() + " )");
System.out.println("\nPOINT 2 : ( " + this.p2.getx() + " : " + this.p2.gety() + " )");
System.out.println("\nPOINT 3 : ( " + this.p3.getx() + " : " + this.p3.gety() + " )");
System.out.println("\nPOINT 4 : ( " + this.p4.getx() + " : " + this.p4.gety() + " )");
}
public void move() throws IOException
{
double newx,newy,diffx,diffy;
System.out.println("Enter New Cordinates of Point 1");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter x-Cordinates :");
newx=Integer.parseInt(br.readLine());
System.out.println("Enter y-Cordinates :");
newy=Integer.parseInt(br.readLine());

diffx=newx-p1.getx();
diffy=newy-p1.gety();

p1.move(newx,newy);
p2.move(p2.getx() + diffx,p2.gety() + diffy);
p3.move(p3.getx() + diffx,p3.gety() + diffy);
p4.move(p4.getx() + diffx,p4.gety() + diffy);
}
public void move(CartesianPoint p1) throws IOException
{

try
{
if( !(p1 instanceof CartesianPoint))
{
ClassNotMatchException obj;
obj=new ClassNotMatchException();
throw obj;
}
else
{
double newx,newy;
System.out.println("Enter New Cordinates of Point 1");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter x-Cordinates :");
newx=Integer.parseInt(br.readLine());
System.out.println("Enter y-Cordinates :");
newy=Integer.parseInt(br.readLine());
// p1.move(newx,newy);
this.p1.move(newx,newy);
}
}
catch(ClassNotMatchException obj)
{
obj.display();
}

}
public void area()
{
double a,b,c,d,s,perimeter,area=0;

a=Math.sqrt(Math.pow((p1.getx()-p2.getx()),2)+Math.pow((p1.gety()-p2.gety()),2));
b=Math.sqrt(Math.pow((p1.getx()-p4.getx()),2)+Math.pow((p1.gety()-p4.gety()),2));
c=Math.sqrt(Math.pow((p2.getx()-p3.getx()),2)+Math.pow((p2.gety()-p3.gety()),2));
d=Math.sqrt(Math.pow((p3.getx()-p4.getx()),2)+Math.pow((p3.gety()-p4.gety()),2));
if (a==c && b==d)
{
area=a*b;
}
else if( a==b && c==d)
{
area=a*c;
}
else if( a==d && b==c)
{
area=a*b;
}
System.out.println("Area of rectangle is : " + area);
}
public void perimeter()
{
double a,b,c,d,perimeter=0;
a=Math.sqrt(Math.pow((p1.getx()-p2.getx()),2)+Math.pow((p1.gety()-p2.gety()),2));
b=Math.sqrt(Math.pow((p1.getx()-p4.getx()),2)+Math.pow((p1.gety()-p4.gety()),2));
c=Math.sqrt(Math.pow((p2.getx()-p3.getx()),2)+Math.pow((p2.gety()-p3.gety()),2));
d=Math.sqrt(Math.pow((p3.getx()-p4.getx()),2)+Math.pow((p3.gety()-p4.gety()),2));
if (a==c && b==d)
{
perimeter=2*(a+b);
}
else if( a==b && c==d)
{
perimeter=2*(a+c);
}
else if( a==d && b==c)
{
perimeter=2*(a+b);
}
System.out.println("Perimeter of rectangle is : " + perimeter);
}
public void rotate(CartesianPoint p1,double x)
{
double newx,newy;
newx=(p1.getx() * Math.cos(x)) - (p1.gety() * Math.sin(x));
newy=(p1.getx() * Math.sin(x)) + (p1.gety() * Math.cos(x));
p1.move(newx,newy);
}
public String toString()
{
return "p1(x):"+p1.getx()+" ; p1(y):"+p1.gety()+" ; p2(x):"+p2.getx()+" ; p2(y):"+p2.gety()+" ; p3(x):"+p3.getx()+" ; p3(y):"+p3.gety()+" ; p4(x):"+p4.getx()+" ; p4(y):"+p4.gety();
}
public boolean equals(Object tempobj)
{
if(!(tempobj instanceof rectangle))
return false;
if(this.p1.getx() != ((rectangle)tempobj).p1.getx() && this.p1.gety() != ((rectangle)tempobj).p1.gety())
return false;
if(this.p2.getx() != ((rectangle)tempobj).p2.getx() && this.p2.gety() != ((rectangle)tempobj).p2.gety())
return false;
if(this.p3.getx() != ((rectangle)tempobj).p3.getx() && this.p3.gety() != ((rectangle)tempobj).p3.gety())
return false;
if(this.p4.getx() != ((rectangle)tempobj).p4.getx() && this.p4.gety() != ((rectangle)tempobj).p4.gety())
return false;
return true;
}
}
class testtriangle
{
public static void main(String args[]) throws IOException
{
CartesianPoint p1,p2,p3,p4;
triangle triangle_obj;
rectangle rectangle_obj;


p1=new CartesianPoint(5,5);
p2=new CartesianPoint(10,5);
p3=new CartesianPoint(10,10);
p4=new CartesianPoint(5,10);


rectangle_obj=new rectangle(p1,p2,p3,p4);
triangle_obj=new triangle(p1,p2,p3);
triangle_obj.display();
triangle_obj.perimeter();
triangle_obj.area();

triangle_obj.move(rectangle_obj);
System.out.println("After Moving of Triangle");
triangle_obj.display();
triangle_obj.perimeter();
triangle_obj.area();
/*
triangle_obj.rotate(p1,90);
triangle_obj.rotate(p2,90);
triangle_obj.rotate(p3,90);
triangle_obj.perimeter();
triangle_obj.area();

// rectangle
*/

rectangle_obj.display();
rectangle_obj.perimeter();
rectangle_obj.area();

/*triangle_obj.move();
System.out.println("After Moving of rectangle");
rectangle_obj.display();
rectangle_obj.perimeter();
rectangle_obj.area();*/

rectangle_obj.rotate(p1,90);
rectangle_obj.rotate(p2,90);
rectangle_obj.rotate(p3,90);
rectangle_obj.perimeter();
rectangle_obj.area();

System.out.println("hasCode of triangle :"+ triangle_obj.hashCode());
System.out.println("hasCode of rectangle :"+ rectangle_obj.hashCode());
if(triangle_obj.equals(rectangle_obj))
System.out.println("BOTH OBJECTS ARE EQUAL");
else
System.out.println("BOTH OBJECTS ARE NOT EQUAL");
}
}

[ graphics.java ] 
(save it in graphics folder)

package graphics;
public class CartesianPoint
{
double x,y;
public CartesianPoint()
{
}
public CartesianPoint(double x)
{
System.out.println("CONSTRUCTOR WTIH ONE PARAMETER");
this.x=x;
this.y=x;
}
public CartesianPoint(double x,double y)
{
System.out.println("CONSTRUCTOR WTIH TWO PARAMETER");
this.x=x;
this.y=y;
}
public double getx()
{
return x;
}
public double gety()
{
return y;
}
public void move(double x,double y)
{
System.out.println("MOVE METHOD WTIH ONE PARAMETER");
this.x=x;
this.y=y;
}
public void move(double x)
{
System.out.println("MOVE METHOD WTIH TWO PARAMETER");
this.x=x;
this.y=x;
}
public void display()
{
System.out.println("Value of x : " + x);
System.out.println("Value of y : " + y);
}
}