-
public class Point
-
{
-
-
private int x;
-
-
private int y;
-
-
private int Number;
-
-
-
public Point(int X, int Y)
-
{
-
-
this.x =X;
-
this.y = Y;
-
this.Number=0;
-
}
-
-
-
-
public void assign(int clustNo)
-
{
-
-
this.Number = clustNo;
-
-
}
-
-
-
-
public int getNumber()
-
{
-
-
return this.Number;
-
-
}
-
-
-
public int getX()
-
{
-
-
return this.x;
-
-
}
-
-
-
-
public int getY()
-
{
-
-
return this.y;
-
-
}
-
-
-
public static double distance(Point dp1, Point dp2)
-
{
-
-
double result = 0;
-
double resultX = dp1.getX() - dp2.getX();
-
double resultY = dp1.getY() - dp2.getY();
-
result = Math.sqrt(resultX*resultX + resultY*resultY);
-
return result;
-
-
}
-
-
-
-
public String toString()
-
{
-
return "("+ this.x + " ," +this.y + ")" + " belongs to g" + this.Number ;
-
-
-
}
-
-
-
-
}
-
-
import java.io.*;
-
import java.util.*;
-
-
-
-
public class trial
-
{
-
-
-
private int k;
-
-
-
private grouping[] group;
-
-
-
-
private int nIterations;
-
-
-
-
private Vector inputdata;
-
-
-
-
private String inputFileName;
-
-
-
-
-
public trial (int k, String inputFileName)
-
{
-
-
this.k = k;
-
this.inputFileName = inputFileName;
-
this.group= new grouping[this.k];
-
this.nIterations = 0;
-
this.inputdata = new Vector();
-
-
}
-
-
-
-
-
public trial(int k, List inputdata)
-
-
{
-
-
this.k = k;
-
this.inputFileName = inputFileName;
-
this.group= new grouping[this.k];
-
this.nIterations = 0;
-
this.inputdata=new Vector(inputdata);
-
-
}
-
-
-
-
-
-
public void readData() throws IOException
-
-
{
-
-
BufferedReader in = new BufferedReader(new FileReader(this.inputFileName));
-
String line = "";
-
while ((line = in.readLine()) != null )
-
{
-
-
StringTokenizer st = new StringTokenizer(line, " \t\n\r\f,");
-
if (st.countTokens() == 2)
-
{
-
-
Point dp = new Point(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
-
dp.assign(0);
-
this.inputdata.add(dp);
-
-
}
-
-
}
-
-
-
-
-
in.close();
-
-
}
-
-
-
public void runs()
-
{
-
-
// Select k points as initial means
-
for (int i=0; i < k; i++)
-
{
-
-
this.clusters[i] = new clustering(i);
-
this.clusters[i].setMean((Point)(this.inputdata.get((int)(Math.random() * this.inputdata.size()))));
-
-
}
-
-
-
-
-
do
-
{
-
-
Iterator i = this.inputdata.iterator();
-
while (i.hasNext())
-
-
this.assign((Point)(i.next()));
-
-
this.nIterations++;
-
-
}while (this.updateMeans());
-
-
-
}
-
-
-
-
-
private void clusterassign(Point dp)
-
{
-
-
int currentCluster = dp.getClusterNumber();
-
-
double minDistance = Point.distance(dp, this.clusters[currentCluster].getMean());;
-
-
for (int i=0; i <this.k; i++)
-
if (DataPoint.distance(dp, this.clusters[i].getMean()) < minDistance)
-
{
-
-
minDistance = DataPoint.distance(dp, this.clusters[i].getMean());
-
currentCluster = i;
-
-
}
-
-
dp.clusterassign(currentCluster);
-
-
}
-
-
-
-
-
private boolean updateMeans()
-
{
-
-
boolean reply = false;
-
-
int[] x = new int[this.k];
-
int[] y = new int[this.k];
-
int[] size = new int[this.k];
-
Point[] pastMeans = new Point[this.k];
-
-
for (int i=0; i<this.k; i++)
-
{
-
-
x[i] = 0;
-
y[i] = 0;
-
size[i] = 0;
-
pastMeans[i] = this.clusters[i].getMean();
-
-
}
-
-
Iterator i = this.inputdata.iterator();
-
while (i.hasNext())
-
{
-
-
-
Point dp = (Point)(i.next());
-
int currentCluster = dp.getClusterNumber();
-
-
x[currentCluster] += dp.getX();
-
y[currentCluster] += dp.getY();
-
size[currentCluster]++;
-
-
}
-
-
for (int j=0; j < this.k; j++ )
-
if(size[j] != 0) {
-
-
x[j] /= size[j];
-
y[j] /= size[j];
-
Point temp = new Point(x[j], y[j]);
-
temp.clusterassign(j);
-
this.clusters[j].setMean(temp);
-
if (Point.distance(pastMeans[j], this.clusters[j].getMean()) !=0 )
-
reply = true;
-
-
}
-
-
return reply;
-
-
}
-
-
public int getK() {
-
-
return this.k;
-
-
}
-
-
-
public clustering getCluster(int index) {
-
-
-
-
return this.clusters[index];
-
-
}
-
-
-
-
public String toString()
-
{
-
-
return this.inputdata.toString();
-
-
-
-
-
}
-
-
-
-
public Vector getPoints() {
-
-
return this.inputdata ;
-
-
}
-
-
-
-
-
-
-
public static void main(String[] args) {
-
-
-
System.out.println("Please Enter the Number ");
-
Scanner input1 = new Scanner(System.in);
-
int n=input1.nextInt();
-
System.out.println(" Enter the file name");
-
Scanner input2 = new Scanner(System.in);
-
String name = input2.next();
-
File file = new File(name);
-
-
trial km = new trial(n, name);
-
-
try {
-
km.readData();
-
} catch (Exception e) {
-
System.err.println(e);
-
System.exit(-1);
-
}
-
-
-
km.runs();
-
-
-
System.out.println(" \n" +km);
-
-
-
}
-
-
}
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>jav a 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
6 1477
Help with what? What is the problem with the program? There is no need for us to try and guess what the problem is.
i have given that it is not giving the output in the format how i need. plz go threw my post
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.
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
Yep ArrayList, like Vector is one dimensional. That is why I said to use an ArrayList of ArrayLists.
JosAH 11,448
Recognized Expert MVP @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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 and it should be known only to one more link on that
page and not to other links.
I cant have that window reference in that global file, since iam
declaring it initially as Null and modifying its value in that one
particular link. Since all links...
|
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 1
I WANT TO BE TO HAVE THIS IN ONE RECORD IE REPEAT THE COULUMNS WAGETYPE
AND UNIT(APPEND THEM TO THE END OF UNIT)
|
by: Tamir Khason |
last post by:
Follwing the struct:
public struct TpSomeMsgRep
{
public uint SomeId;
|
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 money laundering.My father MR.
D.S.P.ALAMIEYESEIGHA was accussed of embezzling the <The sum of FOURTY FIVE
MILLION DOLLARS>.
Since my father"s arrest i have been able to set aside the sum of nine
million dollars.This money is meant to be used to...
|
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 scripts. each when
clicked i am able to run the script. so HOW and what do i do to call
and run the .sql
Thanks
| |
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 the socket is missing the
last character (line feed). When the same text is sent without the urgent
flag set, all of the characters are read.
I'm reading the data using the blocking read call of the network stream
class. The .NET...
|
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 changed or updated the
server in any way.
"There is no row at position 42"
at System.Data.RBTree`1.GetNodeByIndex(int32 userIndex)
at System.Data.DataTable.Clear(Boolean clearAll)
at system.Data.DataTable.Clear()
|
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 generating the XML
document. ---System.InvalidOperationException: The type Car List was not
expected. Use the XmlInclude or SoapInclude attribute to specify types that
are not known statically.
at...
|
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 name="Asha" id="1">
<details>456</details>
|
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 error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |