school task help

Reply

Join Date: Jul 2004
Posts: 14
Reputation: Elektro is an unknown quantity at this point 
Solved Threads: 0
Elektro Elektro is offline Offline
Newbie Poster

school task help

 
0
  #1
Jul 28th, 2004
:o does anyone know how to write this for me i have no idea i need help
Outline: Java is a powerful Object Oriented Programming (OOP) language specifically designed for use with the internet. However, this makes learning Java fairly difficult. So we will start with a simple (text-based) program.

You should be familiar with the x-y real number plane used in co-ordinate geometry:




Positions on this plane are given as (x,y) co-ordinate pairs relative to the x and y axes.

The distance between any two points P1 = (x1, y1) and P2 = (x2, y2) is given by the distance formula, derived from Pythagoras' Theorem:



What to do: You are required to design and implement a java program to calculate and display (as text) the distance between pairs of (x,y) points supplied from a text file.

The file is named data.txt and contains one pair of points per line in the format: (x1,y1)<tab>(x2,y2) where <tab> refers to a single tab (ASCII 9) character. You will need to create your own data.txt file to test your program. By default, this file must be stored inside the Build folder of your project.

The program should display a title and brief description of what it does. Each pair of points should be displayed, along with the distance between them, correct to 4 decimal places eg:

The distance between (0,0) and (1,5) is 5.0990
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 14
Reputation: Elektro is an unknown quantity at this point 
Solved Threads: 0
Elektro Elektro is offline Offline
Newbie Poster

Re: school task help

 
0
  #2
Jul 28th, 2004
(this thread was started by a friend of mine needing the same help, he just used my account to post the thread)

So yeah... any help would be appreciated
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: school task help

 
0
  #3
Jul 28th, 2004
So, what has your friend done so far with his project?

This sounds like homework. We don't do homework for people; we help people with what they're working on. Please post some code they've done on their own.
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 16
Reputation: rickste_r is an unknown quantity at this point 
Solved Threads: 0
rickste_r's Avatar
rickste_r rickste_r is offline Offline
Newbie Poster

Re: school task help

 
0
  #4
Jul 28th, 2004
Ok guys, I kinda have what you are looking for.

The only change is that my text file does not contain any brackets or commas i.e (20,20) = 20 20.

The x1 coordinate is entered then SPACE then y1 coordinate entered,
then TAB
then x2 coordinate is entered then SPACE then y2 coordinate entered.

I dont have a solution for breaking down a string to search for numbers in that string.

The text file attached looks like this (test4.txt)

-------------------------------------------
0[SPACE]0[TAB]1[SPACE]5
10[SPACE]20[TAB] 30[SPACE]35
20[SPACE]34[TAB] 35[SPACE]43
-------------------------------------------------

The class that drives the show, also attached (test4.java)
-------------------------------------------------
public static void main(String[] args)
{
StringTokenizer token;
String tokenString = null;

String x1Cord = null;
String y1Cord = null;
String x2Cord = null;
String y2Cord = null;

BufferedReader buffer = null;
DecimalFormat df = new DecimalFormat("0.0000");

try
{
buffer = new BufferedReader(new FileReader("test4.txt"));
}
catch (FileNotFoundException fnf)
{
System.err.print("\nError: Cannot find " + "\"test4.txt\"" +
" file\n");
System.exit(1);
}

//display a title
System.out.println("\n*************************************");
System.out.println("** Co-ordinate System v1.0 **********");
System.out.println("*************************************");
System.out.println("\nPROGRAM DETAILS: ");
System.out.println("\tRead in two co-ordinates from the text file ");
System.out.println("\tWork out the difference betweeen them");
System.out.println("\nThe format in the text file is: x1 SPACE y1 TAB x2 SPACE y2");
System.out.println("I did not include the brackets/commas for the co-ordinates in ");
System.out.println("the text file as it involves alot of coding to remove!!");

while (true)
{
try
{
tokenString = buffer.readLine();
if (tokenString == null)
{
break;
}
}
catch (IOException ioe)
{
System.err.print("\nError: There was problem reading the File");
break;
}

//tab & space is the delimeter
token = new StringTokenizer(tokenString, " \t");

x1Cord = token.nextToken();
y1Cord = token.nextToken();
x2Cord = token.nextToken();
y2Cord = token.nextToken();

//variables for the maths
double a;
double b;
double diff;
double x1 = Double.parseDouble(x1Cord);
double y1 = Double.parseDouble(y1Cord);
double x2 = Double.parseDouble(x2Cord);
double y2 = Double.parseDouble(y2Cord);

//do the pythagoras maths
a = y2 - y1;
b = x2 - x1;
diff = Math.sqrt( (a*a) + (b*b) );

//print out the co-ordinates
System.out.print("\nYour first point is:" + "(" + x1 + "," + y1 + ")");
System.out.println(" & Your second point is:" + "(" + x2 + "," + y2+ ")");

//This prints out the difference correct to 4 d.p. using DecimalFormat
System.out.println("Difference: " + df.format(diff));
}
}//end main
------------------------------------------------------------
The output from the above files:

*************************************
** Co-ordinate System v1.0 **********
*************************************
PROGRAM DETAILS:
Read in two co-ordinates from the text file
Work out the difference betweeen them

The format in the text file is: x1 SPACE y1 TAB x2 SPACE y2
I did not include the brackets/commas for the co-ordinates in
the text file as it involves alot of coding to remove!!

Your first point is: (0.0,0.0) & Your second point is: (1.0,5.0)
Difference: 5.0990


-------------------------------------------------------

Hope that gets you started, leave feedback! rickster.
Attached Files
File Type: java test4.java (2.6 KB, 15 views)
File Type: txt test4.txt (32 Bytes, 14 views)
Only lemmings should jump to conclusions! :rolleyes: Please rate me!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 6
Reputation: talyu is an unknown quantity at this point 
Solved Threads: 0
talyu talyu is offline Offline
Newbie Poster

Re: school task help

 
0
  #5
Jan 23rd, 2007
HI guys, your code is soooo amazing~ i am doing something similar too but kinda stuck hope you can help me pleaseeee.basically i am also calculating the distance, but instead of having all the co ordinates typed in a text file, i am having a fill in blank for user to fill in the TO and FORM then i want it to search through my SQL database to find the matching co ordinates. for example user fill in TO: SE1 FROM SW5, then it will search the database to find the co ordinates of SE1 and SW 5 then cauculate. is that possible??? please help~ i was searching all night but no clue.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,191
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: school task help

 
0
  #6
Jan 23rd, 2007
Originally Posted by talyu View Post
HI guys, your code is soooo amazing~ i am doing something similar too but kinda stuck hope you can help me pleaseeee.basically i am also calculating the distance, but instead of having all the co ordinates typed in a text file, i am having a fill in blank for user to fill in the TO and FORM then i want it to search through my SQL database to find the matching co ordinates. for example user fill in TO: SE1 FROM SW5, then it will search the database to find the co ordinates of SE1 and SW 5 then cauculate. is that possible??? please help~ i was searching all night but no clue.
Searching internet all night LOL :lol:

Common sence my friend,
1. database with table contatining destination(example E2) and values to coordinates x & y
2. From form either in swing/JSP, you didn't say which one, you read two destinations
- check if their are not same as there is zore distance and no point to access database and run calculation
3. Connect to database get coordinates for your two destinations and run calculation
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 6
Reputation: talyu is an unknown quantity at this point 
Solved Threads: 0
talyu talyu is offline Offline
Newbie Poster

Re: school task help

 
0
  #7
Jan 23rd, 2007
wowoooooooooo thanks!!!! so clever!!!!!!!!! so thats not hard at all yeah?? connecting SQL and java and transfering to and from
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,191
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: school task help

 
0
  #8
Jan 23rd, 2007
Originally Posted by talyu View Post
wowoooooooooo thanks!!!! so clever!!!!!!!!! so thats not hard at all yeah?? connecting SQL and java and transfering to and from
I don't see a problem there . They must showed you at school how to do it and query language is not so complex. Plus there is MySQL website with full documentation and lot of examples and if you look in daniweb MySQL section you get also lot of help there
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: school task help

 
0
  #9
Jan 23rd, 2007
well, you can't expect someone who responds to a post that's going on 3 years old with a poorly worded message that says how great the post is to be all that bright
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,191
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: school task help

 
0
  #10
Jan 23rd, 2007
LOL, I didn't check date
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC