| | |
school task help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2004
Posts: 14
Reputation:
Solved Threads: 0
: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
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
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.
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.
Only lemmings should jump to conclusions! :rolleyes: Please rate me!
•
•
Join Date: Jan 2007
Posts: 6
Reputation:
Solved Threads: 0
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.
•
•
•
•
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.
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
•
•
wowoooooooooo thanks!!!! so clever!!!!!!!!! so thats not hard at all yeah?? connecting SQL and java and transfering to and from
. 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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- Please help with login page (Community Introductions)
- Argument Not Optional message in VB (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: creating an instant messenger program in java
- Next Thread: LinkedList Implementation Help SOLVED!!!
| Thread Tools | Search this Thread |
android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database db defaultmethod development dice dragging draw ebook eclipse error event exception formatingtextintooltipjava fractal game givemetehcodez graphics gui hql html ide image infinite input integer invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx oracle parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows






