943,769 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7159
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 28th, 2004
0

school task help

Expand Post »
: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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Elektro is offline Offline
14 posts
since Jul 2004
Jul 28th, 2004
0

Re: school task help

(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
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Elektro is offline Offline
14 posts
since Jul 2004
Jul 28th, 2004
0

Re: school task help

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.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Jul 28th, 2004
0

Re: school task help

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, 50 views)
File Type: txt test4.txt (32 Bytes, 43 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rickste_r is offline Offline
16 posts
since Jul 2004
Jan 23rd, 2007
0

Re: school task help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
talyu is offline Offline
6 posts
since Jan 2007
Jan 23rd, 2007
0

Re: school task help

Click to Expand / Collapse  Quote originally posted by talyu ...
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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Jan 23rd, 2007
0

Re: school task help

wowoooooooooo thanks!!!! so clever!!!!!!!!! so thats not hard at all yeah?? connecting SQL and java and transfering to and from
Reputation Points: 10
Solved Threads: 0
Newbie Poster
talyu is offline Offline
6 posts
since Jan 2007
Jan 23rd, 2007
0

Re: school task help

Click to Expand / Collapse  Quote originally posted by talyu ...
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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Jan 23rd, 2007
0

Re: school task help

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
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 23rd, 2007
0

Re: school task help

LOL, I didn't check date
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: creating an instant messenger program in java
Next Thread in Java Forum Timeline: LinkedList Implementation Help SOLVED!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC