Author Topic: Java Program to check 3 given points are in same line or not  (Read 1292 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Java Program to check 3 given points are in same line or not
« on: January 06, 2012, 10:48:15 am »
Here is the Java Program to check whether 3 given points are in same line or not.

Code: (java)
//Source Code Listing

import java.lang.*;
import java.util.*;
import java.util.List;
import java.io.*;

public class CheckIsLine {

   public static int ReadInteger()
   {
        try
        {
              InputStreamReader input = new InputStreamReader(System.in);
              BufferedReader reader = new BufferedReader(input);
              return Integer.parseInt(reader.readLine());
        }
        catch (Exception e)
        {

              e.printStackTrace();
              return 0;
        }
   }
  
   
  public static void main(String args[]) 
  {
      List<Integer> points = new ArrayList<Integer>();             
      for(int i = 0; i < 3; i++)
      {
         System.out.format("Enter x[%d]: ", i + 1);
         points.add(ReadInteger());
         System.out.format("Enter y[%d]: ", i + 1);
         points.add(ReadInteger());
      }
      
      Boolean isline = false;
      
      if(points.get(0) == points.get(2) &&
         points.get(0) == points.get(4))
         isline = true;
      if(points.get(1) == points.get(3) &&
         points.get(1) == points.get(5))
         isline = true;
     
     if(isline == true)
         System.out.println("The points you gave are in the same line");
     else
         System.out.println("The points you gave are NOT in the same line and can form a triangle");
  }
}

C:\Users\Kathir\Java>java CheckIsLine
Enter x[1]: 100
Enter y[1]: 50
Enter x[2]: 101
Enter y[2]: 51
Enter x[3]: 102
Enter y[3]: 52
The points you gave are NOT in the same line and can form a triangle

C:\Users\Kathir\Java>java CheckIsLine
Enter x[1]: 100
Enter y[1]: 51
Enter x[2]: 100
Enter y[2]: 55
Enter x[3]: 100
Enter y[3]: 200
The points you gave are in the same line

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1