473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

it is urgent please help me

6
Expand|Select|Wrap|Line Numbers
  1. public class Point
  2.  {
  3.  
  4.     private int x;
  5.  
  6.     private int y;
  7.  
  8.     private int Number;
  9.  
  10.  
  11.     public Point(int X, int Y)
  12.         {
  13.  
  14.         this.x =X;
  15.         this.y = Y;
  16.         this.Number=0;
  17.     } 
  18.  
  19.  
  20.  
  21.     public void assign(int clustNo) 
  22.           {
  23.  
  24.         this.Number = clustNo;
  25.  
  26.     } 
  27.  
  28.  
  29.  
  30.     public int getNumber() 
  31.         {
  32.  
  33.         return this.Number;
  34.  
  35.     } 
  36.  
  37.  
  38.     public int getX() 
  39.          {
  40.  
  41.         return this.x;
  42.  
  43.     } 
  44.  
  45.  
  46.  
  47.     public int getY() 
  48.         {
  49.  
  50.         return this.y;
  51.  
  52.     } 
  53.  
  54.  
  55.     public static double distance(Point dp1, Point dp2) 
  56.         {
  57.  
  58.         double result = 0;
  59.         double resultX = dp1.getX() - dp2.getX();
  60.         double resultY = dp1.getY() - dp2.getY();
  61.         result = Math.sqrt(resultX*resultX + resultY*resultY);
  62.         return result;
  63.  
  64.     } 
  65.  
  66.  
  67.  
  68.     public  String toString()
  69.           {   
  70.               return "("+ this.x + " ," +this.y + ")" + " belongs to g" + this.Number ; 
  71.  
  72.  
  73.           }
  74.  
  75.  
  76.  
  77.  
  78. import java.io.*;
  79. import java.util.*;
  80.  
  81.  
  82.  
  83. public class trial
  84.  {
  85.  
  86.  
  87.         private int k;
  88.  
  89.  
  90.     private grouping[] group;
  91.  
  92.  
  93.  
  94.     private int nIterations;
  95.  
  96.  
  97.  
  98.     private Vector inputdata;
  99.  
  100.  
  101.  
  102.     private String inputFileName;
  103.  
  104.  
  105.  
  106.  
  107.          public trial (int k, String inputFileName) 
  108.          {
  109.  
  110.         this.k = k;
  111.         this.inputFileName = inputFileName;
  112.         this.group= new grouping[this.k];
  113.         this.nIterations = 0;
  114.         this.inputdata = new Vector();
  115.  
  116.       } 
  117.  
  118.  
  119.  
  120.  
  121.            public trial(int k, List inputdata)
  122.  
  123.            {
  124.  
  125.         this.k = k;
  126.         this.inputFileName = inputFileName;
  127.         this.group= new grouping[this.k];
  128.         this.nIterations = 0;
  129.         this.inputdata=new Vector(inputdata);
  130.  
  131.         } 
  132.  
  133.  
  134.  
  135.  
  136.  
  137.        public void readData() throws IOException
  138.  
  139.                {
  140.  
  141.         BufferedReader in = new BufferedReader(new FileReader(this.inputFileName));
  142.         String line = "";
  143.            while ((line = in.readLine()) != null )
  144.                       {
  145.  
  146.             StringTokenizer st = new StringTokenizer(line, " \t\n\r\f,");
  147.                 if (st.countTokens() == 2) 
  148.                                   {
  149.  
  150.                     Point dp = new Point(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
  151.                     dp.assign(0);
  152.                     this.inputdata.add(dp);
  153.  
  154.                                    }
  155.  
  156.                 }
  157.  
  158.  
  159.  
  160.  
  161.         in.close();
  162.  
  163.     } 
  164.  
  165.  
  166.          public void runs() 
  167.                {
  168.  
  169.            // Select k points as initial means
  170.              for (int i=0; i < k; i++)
  171.                     {
  172.  
  173.             this.clusters[i] = new clustering(i);
  174.             this.clusters[i].setMean((Point)(this.inputdata.get((int)(Math.random() * this.inputdata.size()))));
  175.  
  176.                }
  177.  
  178.  
  179.  
  180.  
  181.           do 
  182.                    {
  183.  
  184.             Iterator i = this.inputdata.iterator();
  185.                 while (i.hasNext())
  186.  
  187.                         this.assign((Point)(i.next()));
  188.  
  189.             this.nIterations++;
  190.  
  191.         }while (this.updateMeans());
  192.  
  193.  
  194.               } 
  195.  
  196.  
  197.  
  198.  
  199.     private void clusterassign(Point dp)
  200.            {
  201.  
  202.         int currentCluster = dp.getClusterNumber();
  203.  
  204.         double minDistance = Point.distance(dp, this.clusters[currentCluster].getMean());;
  205.  
  206.         for (int i=0; i <this.k; i++)
  207.             if (DataPoint.distance(dp, this.clusters[i].getMean()) < minDistance) 
  208.                              {
  209.  
  210.                 minDistance = DataPoint.distance(dp, this.clusters[i].getMean());
  211.                 currentCluster = i;
  212.  
  213.                    }
  214.  
  215.         dp.clusterassign(currentCluster);    
  216.  
  217.     } 
  218.  
  219.  
  220.  
  221.  
  222.         private boolean updateMeans() 
  223.            {
  224.  
  225.         boolean reply = false;
  226.  
  227.         int[] x = new int[this.k];
  228.         int[] y = new int[this.k];
  229.         int[] size = new int[this.k];
  230.         Point[] pastMeans = new Point[this.k];
  231.  
  232.         for (int i=0; i<this.k; i++)
  233.                   {
  234.  
  235.             x[i] = 0;
  236.             y[i] = 0;
  237.             size[i] = 0;
  238.             pastMeans[i] = this.clusters[i].getMean();
  239.  
  240.          }
  241.  
  242.         Iterator i = this.inputdata.iterator();
  243.         while (i.hasNext()) 
  244.                 {
  245.  
  246.  
  247.             Point dp = (Point)(i.next());
  248.             int currentCluster = dp.getClusterNumber();
  249.  
  250.             x[currentCluster] += dp.getX();
  251.             y[currentCluster] += dp.getY();
  252.             size[currentCluster]++;
  253.  
  254.         }
  255.  
  256.         for (int j=0; j < this.k; j++ ) 
  257.             if(size[j] != 0) {
  258.  
  259.                 x[j] /= size[j];
  260.                 y[j] /= size[j];
  261.                 Point temp = new Point(x[j], y[j]);
  262.                 temp.clusterassign(j);
  263.                 this.clusters[j].setMean(temp);
  264.                 if (Point.distance(pastMeans[j], this.clusters[j].getMean()) !=0 )
  265.                     reply = true;
  266.  
  267.             }
  268.  
  269.         return reply;
  270.  
  271.     } 
  272.  
  273.     public int getK() {
  274.  
  275.         return this.k;
  276.  
  277.     } 
  278.  
  279.  
  280.     public clustering getCluster(int index) {
  281.  
  282.  
  283.  
  284.         return this.clusters[index];
  285.  
  286.     } 
  287.  
  288.  
  289.  
  290.     public String toString()
  291.            {        
  292.  
  293.                     return  this.inputdata.toString();
  294.  
  295.  
  296.  
  297.  
  298.     } 
  299.  
  300.  
  301.  
  302.     public Vector getPoints() {
  303.  
  304.         return this.inputdata ;
  305.  
  306.     } 
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.     public static void main(String[] args) {
  314.  
  315.  
  316.          System.out.println("Please Enter the Number ");
  317.          Scanner input1 = new Scanner(System.in);
  318.          int n=input1.nextInt();
  319.          System.out.println(" Enter the file name");
  320.          Scanner input2 = new Scanner(System.in);
  321.          String name = input2.next();
  322.          File file = new File(name);
  323.  
  324.         trial  km = new  trial(n, name);
  325.  
  326.         try {
  327.             km.readData();
  328.         } catch (Exception e) {
  329.             System.err.println(e);
  330.             System.exit(-1);
  331.         }
  332.  
  333.  
  334.         km.runs();
  335.  
  336.  
  337.                  System.out.println(" \n" +km);
  338.  
  339.  
  340.         } 
  341.  
this code works well on the data
345,2
300,2
390,2
400,3
457,3
478,3
200,1
234,1
280,1
and produces the output

C:\Program Files\Java\jdk1.6.0_03\bin>java trialPlease Enter the Number
3
Enter the file name
hi.csv

[(345 ,2) belongs to g1, (300 ,2) belongs to g1, (390 ,2) belongs to
g0, (400 ,3) belongs to g0, (457 ,3) belongs to g0, (478 ,3)
belongs to g0, (200 ,1) belongs to g2, (234 ,1) belongs to g2,
(280 ,1) belongs to g1]

but i need the output in the form in the excel sheet
group0 group1 group2
400,3 345 ,2 200,1
457,3 390 ,2 234,1
478,3 300 ,2 28,1

Please help me . it is urgent . i have to submit my project
Mar 3 '09 #1
6 1458
r035198x
13,262 8TB
Help with what? What is the problem with the program? There is no need for us to try and guess what the problem is.
Mar 3 '09 #2
thijo
6
i have given that it is not giving the output in the format how i need. plz go threw my post
Mar 3 '09 #3
r035198x
13,262 8TB
1.) Vector is old. Consider using ArrayList instead.
2.) StringTokenizer is also old. Read the API specs for it for details. The String.split method is better.
3.) Read the Java code naming conventions. Class names should start with capital letter e.t.c.
4.) What you are getting as output is what you are printing in your toString. Add those points to an ArrayList of ArrayLists. Those in group0 go into the ArrayList at position 0 in the arraylist, those in group one go into the arraylist at position 1 e.t.c.
You could then write a print method for that takes that arraylist and prints out its contents the way you want.
Mar 3 '09 #4
thijo
6
thanks for help. but i dont know how to implement arraylist. when i declared arraylist and stored my result in it, it is stored as a single dimension. then how could i implement the looping for the printing the data in the format i want. please help in coding. it will be of great help. i have to finish my project
Mar 4 '09 #5
r035198x
13,262 8TB
Yep ArrayList, like Vector is one dimensional. That is why I said to use an ArrayList of ArrayLists.
Mar 4 '09 #6
JosAH
11,448 Expert 8TB
@r035198x
Or use a Map<String, List<Point>>; the keys are the group names, the values are the Points of the group. The values can even be a Set<Point> if needed. But I don't think the OP will go that way because it's "urgent".

kind regards,

Jos
Mar 4 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: learner | last post by:
Hi, In a page, i have many links. I have some functions in a global file which is included in all linked pages. I want to have a reference to a window which is to be opened on clicking one link...
2
by: Dimitri | last post by:
PLEASE HELP,I HAVE A DATABSE WITH MULTIPLE RECORDS AS OUTLINED BELOW EMP NO LEVEL NEXTINCREASE WAGETYPE UNIT 1000 1 0 1000 1000 1 0 1002 ...
28
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
0
by: Charles Dido | last post by:
Sir, I am MR.CHARLES ALAMIEYESEIGHA. I am the son of GOVONOUR of BAYELSA STATE IN NIGERIA.Some months ago, this YEAR, my father was arrested in LONDON by the LONDON METHROPOLITAN POLICE for...
17
by: Saps | last post by:
Hi all. Can anyone help me here. I have loads of .sql files and i need a way to call these from my asp page so the user can run them from the browser. Meaning i have a page with a list of all...
3
by: N. Spiker | last post by:
I am attempting to receive a single TCP packet with some text ending with carriage return and line feed characters. When the text is send and the packet has the urgent flag set, the text read from...
4
by: Adrian Parker | last post by:
We've suddenly started getting a problem with a call to clear the contents of a DataTable. This is on a live customer site that's been working fine until yesterday. As far as we know they've not...
5
by: igotyourdotnet | last post by:
I'm creating a web service this is going to return an arraylist of items. When I try and run it Im' getting the following error message: System.InvalidOperationException: There was an error...
4
by: ashasivan | last post by:
Hi, Please help me as soon as possible for the following problem. It is very very urgent. I have an XML file as follows: <?xml version="1.0" standalone="yes"?> <Employee> <em...
1
by: psantosh12 | last post by:
Hello Frnds Please need help to resolve error.......... it is very very urgent........ The error is Runtime Error Description: An application error occurred on the server. The current custom...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.