473,395 Members | 1,745 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,395 software developers and data experts.

Java homework assignment - Struggling

1: A Geographic Distance Calculator
Write an application to calculate a table of geographic distances. The input of your application is a file containing
the coordinates of cities in latitude / longitude pairs, where latitude and longitude are given in degrees and minutes,
as in:1

Edinburgh 55;57N 03;13W
London 51;30N 0;30W
NorthWalsham 52;50N 01;22E
Norwich 52;38N 01;18E
Notice that 1 degree = 60 minutes. The output should be a text fille containing a table of geographic distances in
kilometres:

Edinburgh 0.00 520.90 452.43 467.11
London 520.90 0.00 194.50 175.33
NorthWalsham 452.43 194.50 0.00 22.57
Norwich 467.11 175.33 22.57 0.00

The application should take the names of the input fille (giving the latitude / longitude coordinates) and the output
fille (into which the distance table is written) from the first and second command line parameter. For example, if
your project is called geodist and your input file is cities.txt, the command line

java -jar geodist.jar cities.txt distances.txt

should write the distance table into distances.txt.

The procedure for calculating distances in kilometres is: One degree of latitude corresponds to 111 kilometres. One
degree of longitude corresponds to 111 . cos(') kilometres, where ' is the latitude.2 For example, the coordinates
of Norwich in kilometres are (111 . 1:3 . cos(52:63 degrees) = 87:57 kilometres east of Greenwich's longitude, 111 .
52:63 = 5842:28 kilometres north of the equator), those of North Walsham are (91:64, 5864:48).
The Euclidean distance between these two is
Square.route(87:57- 91:64)^2 + (5842:28- 5864:48)^2 = square.route(-4:07)^2 + (-22:2)^2 = 22:57

Your application should comprise a class that adequately represents the geographic location of a city
and provides good support for carrying out this calculation.
Feb 8 '07 #1
16 5114
Ganon11
3,652 Expert 2GB
What have you accomplished on this problem alone? What code do you have? What ideas do you have?
Feb 8 '07 #2
I have nothing, kind of hoping some Really nice people on here could give me some ideas and help really.
Feb 9 '07 #3
RedSon
5,000 Expert 4TB
I have nothing, kind of hoping some Really nice people on here could give me some ideas and help really.
Well first you need to do a design. What kind of design would you need to accomplish your task? What does the flow of information look like. At a high level what do you have to do?

- Read a file
- Tokenize and parse the input
- Do some calculations on it
- Then what?
Feb 9 '07 #4
abctech
157 100+
1: A Geographic Distance Calculator
Write an application to calculate a table of geographic distances. The input of your application is a file containing
the coordinates of cities in latitude / longitude pairs, where latitude and longitude are given in degrees and minutes,
as in:1

Edinburgh 55;57N 03;13W
London 51;30N 0;30W
NorthWalsham 52;50N 01;22E
Norwich 52;38N 01;18E
Notice that 1 degree = 60 minutes. The output should be a text fille containing a table of geographic distances in
kilometres:

Edinburgh 0.00 520.90 452.43 467.11
London 520.90 0.00 194.50 175.33
NorthWalsham 452.43 194.50 0.00 22.57
Norwich 467.11 175.33 22.57 0.00

The application should take the names of the input fille (giving the latitude / longitude coordinates) and the output
fille (into which the distance table is written) from the first and second command line parameter. For example, if
your project is called geodist and your input file is cities.txt, the command line

java -jar geodist.jar cities.txt distances.txt

should write the distance table into distances.txt.

The procedure for calculating distances in kilometres is: One degree of latitude corresponds to 111 kilometres. One
degree of longitude corresponds to 111 . cos(') kilometres, where ' is the latitude.2 For example, the coordinates
of Norwich in kilometres are (111 . 1:3 . cos(52:63 degrees) = 87:57 kilometres east of Greenwich's longitude, 111 .
52:63 = 5842:28 kilometres north of the equator), those of North Walsham are (91:64, 5864:48).
The Euclidean distance between these two is
Square.route(87:57- 91:64)^2 + (5842:28- 5864:48)^2 = square.route(-4:07)^2 + (-22:2)^2 = 22:57

Your application should comprise a class that adequately represents the geographic location of a city
and provides good support for carrying out this calculation.
If you are clueless about a particular program try thinking if you were to do it manually instead of writing an application then how would you go about it.Jot those points down as an algorithm and then get started.Might help generate some ideas!
Feb 10 '07 #5
r035198x
13,262 8TB
You better learn how to make jar files too.
Feb 12 '07 #6
I am using Netbeans 5.0 and my code so far is:
It should read in the file Geo do some stuff to see if the file exsists and then split the bits of the text file up into parts of an arraylist.
Is this the right sort of thing.
And for some reason Netbeans doesnt like some of my code, it says
"
Cannot find symbol
symbol: class Geo
location: class calculator.Main
"

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. package calculator;
  4. import java.io.*;
  5.  
  6.  
  7. /**
  8.  *
  9.  * @author Matthew
  10.  */
  11. public class Main {
  12.  
  13.     /** Creates a new instance of Main */
  14.     public Main() {
  15.     }
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     public static void main(String[] args) {
  21.         BufferedReader in  = getReader("Geo.txt");
  22.  
  23.         Geo geo = readGeo(in);
  24.         while (geo != null)
  25.         {
  26.             String msg = geo.location;
  27.         }
  28.     }
  29.  
  30.  
  31.  
  32. private static BufferedReader getReader(String name)
  33. {
  34.     BufferedReader in = null;
  35.     try
  36.     {
  37.         File file = new file(name);
  38.         in = new BufferedReader(
  39.                 new FileReader(file) );
  40.     }
  41.     catch (FileNotFoundException e)
  42.     {
  43.         System.out.println("The File does not exsist");
  44.         System.exit(0);
  45.     }
  46.     catch (IOException e)
  47.     {
  48.         System.out.println("I/O Error");
  49.         System.exit(0);
  50.     }
  51.     return in;
  52. }
  53.  
  54. private static Geo readGeo(BufferedReader in)
  55. {
  56.     String location;
  57.     String line = "";
  58.     String[] data;
  59.     String[] data2;
  60.  
  61.     try
  62.     {
  63.         line = in.readLine();
  64.     }
  65.     catch (IOException e)
  66.     {
  67.         System.out.println("I/O Error");
  68.         System.exit(0);        
  69.     }
  70.  
  71.     if (line == null)
  72.         return null;
  73.     else 
  74.     {
  75.         data = line.split ("\t");
  76.         location = data[0];
  77.         data2 = line.split (";");
  78.  
  79.  
  80.  
  81.     }
  82. }
  83.  
  84.  
  85.  
Feb 13 '07 #7
r035198x
13,262 8TB
I am using Netbeans 5.0 and my code so far is:
It should read in the file Geo do some stuff to see if the file exsists and then split the bits of the text file up into parts of an arraylist.
Is this the right sort of thing.
And for some reason Netbeans doesnt like some of my code, it says
"
Cannot find symbol
symbol: class Geo
location: class calculator.Main
"

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. package calculator;
  4. import java.io.*;
  5.  
  6.  
  7. /**
  8. *
  9. * @author Matthew
  10. */
  11. public class Main {
  12.  
  13. /** Creates a new instance of Main */
  14. public Main() {
  15. }
  16.  
  17. /**
  18. * @param args the command line arguments
  19. */
  20. public static void main(String[] args) {
  21. BufferedReader in = getReader("Geo.txt");
  22.  
  23. Geo geo = readGeo(in);
  24. while (geo != null)
  25. {
  26. String msg = geo.location;
  27. }
  28. }
  29.  
  30.  
  31.  
  32. private static BufferedReader getReader(String name)
  33. {
  34. BufferedReader in = null;
  35. try
  36. {
  37. File file = new file(name);
  38. in = new BufferedReader(
  39. new FileReader(file) );
  40. }
  41. catch (FileNotFoundException e)
  42. {
  43. System.out.println("The File does not exsist");
  44. System.exit(0);
  45. }
  46. catch (IOException e)
  47. {
  48. System.out.println("I/O Error");
  49. System.exit(0);
  50. }
  51. return in;
  52. }
  53.  
  54. private static Geo readGeo(BufferedReader in)
  55. {
  56. String location;
  57. String line = "";
  58. String[] data;
  59. String[] data2;
  60.  
  61. try
  62. {
  63. line = in.readLine();
  64. }
  65. catch (IOException e)
  66. {
  67. System.out.println("I/O Error");
  68. System.exit(0); 
  69. }
  70.  
  71. if (line == null)
  72. return null;
  73. else 
  74. {
  75. data = line.split ("\t");
  76. location = data[0];
  77. data2 = line.split (";");
  78.  
  79.  
  80.  
  81. }
  82. }
  83.  
  84.  
  85.  
Well you don't have a class called Geo (or have not compiled it yet) but you are trying to create an object of type Geo.

What do you want the readGeo method to do exactly.
Feb 13 '07 #8
RedSon
5,000 Expert 4TB
I am using Netbeans 5.0 and my code so far is:
It should read in the file Geo do some stuff to see if the file exsists and then split the bits of the text file up into parts of an arraylist.
Is this the right sort of thing.
And for some reason Netbeans doesnt like some of my code, it says
"
Cannot find symbol
symbol: class Geo
location: class calculator.Main
"

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. package calculator;
  4. import java.io.*;
  5.  
  6.  
  7. /**
  8.  *
  9.  * @author Matthew
  10.  */
  11. public class Main {
  12.  
  13.     /** Creates a new instance of Main */
  14.     public Main() {
  15.     }
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     public static void main(String[] args) {
  21.         BufferedReader in  = getReader("Geo.txt");
  22.  
  23.         Geo geo = readGeo(in);
  24.         while (geo != null)
  25.         {
  26.             String msg = geo.location;
  27.         }
  28.     }
  29.  
  30.  
  31.  
  32. private static BufferedReader getReader(String name)
  33. {
  34.     BufferedReader in = null;
  35.     try
  36.     {
  37.         File file = new file(name);
  38.         in = new BufferedReader(
  39.                 new FileReader(file) );
  40.     }
  41.     catch (FileNotFoundException e)
  42.     {
  43.         System.out.println("The File does not exsist");
  44.         System.exit(0);
  45.     }
  46.     catch (IOException e)
  47.     {
  48.         System.out.println("I/O Error");
  49.         System.exit(0);
  50.     }
  51.     return in;
  52. }
  53.  
  54. private static Geo readGeo(BufferedReader in)
  55. {
  56.     String location;
  57.     String line = "";
  58.     String[] data;
  59.     String[] data2;
  60.  
  61.     try
  62.     {
  63.         line = in.readLine();
  64.     }
  65.     catch (IOException e)
  66.     {
  67.         System.out.println("I/O Error");
  68.         System.exit(0);        
  69.     }
  70.  
  71.     if (line == null)
  72.         return null;
  73.     else 
  74.     {
  75.         data = line.split ("\t");
  76.         location = data[0];
  77.         data2 = line.split (";");
  78.  
  79.  
  80.  
  81.     }
  82. }
  83.  
  84.  
  85.  
You might want to search the APIs for a tokenize method or string tokenizer. The way that you are reading in your file is not very object oriented.
Feb 13 '07 #9
Well you don't have a class called Geo (or have not compiled it yet) but you are trying to create an object of type Geo.

What do you want the readGeo method to do exactly.
The readGeo should read in the file that the buffered reader has, which should be geo.text and then split bits of the text file up into array parts so that i can then do some calculations on them.

If i create a new class, Geo, how do i get the main part of the code to refer to it. Netbeans is annoying!!!
Feb 13 '07 #10
You might want to search the APIs for a tokenize method or string tokenizer. The way that you are reading in your file is not very object oriented.
I dont think a tokenizer will help me with this to be honest.
Feb 13 '07 #11
RedSon
5,000 Expert 4TB
The readGeo should read in the file that the buffered reader has, which should be geo.text and then split bits of the text file up into array parts so that i can then do some calculations on them.

If i create a new class, Geo, how do i get the main part of the code to refer to it. Netbeans is annoying!!!

If you create the class Geo, you need to make sure that it is on your CLASSPATH. I don't think you need to import it but you could do that also.
Feb 13 '07 #12
r035198x
13,262 8TB
The readGeo should read in the file that the buffered reader has, which should be geo.text and then split bits of the text file up into array parts so that i can then do some calculations on them.

If i create a new class, Geo, how do i get the main part of the code to refer to it. Netbeans is annoying!!!
Netbeans is quite alright. I wouldn't advice beginners to use IDEs however ...

Now you don't need another class. Just a method readGeo is fine.
You want to create arrays using data that is in a file. How is the data arranged in the file and how do you want it to be stored in the arrays.
Feb 13 '07 #13
Netbeans is quite alright. I wouldn't advice beginners to use IDEs however ...

Now you don't need another class. Just a method readGeo is fine.
You want to create arrays using data that is in a file. How is the data arranged in the file and how do you want it to be stored in the arrays.
In the text file, Geo.text, the data is arranged as follows:

Edinburgh 55;57N 03;13W
London 51;30N 0;30W
NorthWalsham 52;50N 01;22E
Norwich 52;38N 01;18E

In the code before i am attempting to split each line up, like:
Edingburgh in data[0] and 55 in data[1] and so on. then i can use these to carry out some calculations for the answer.
Feb 13 '07 #14
r035198x
13,262 8TB
In the text file, Geo.text, the data is arranged as follows:

Edinburgh 55;57N 03;13W
London 51;30N 0;30W
NorthWalsham 52;50N 01;22E
Norwich 52;38N 01;18E

In the code before i am attempting to split each line up, like:
Edingburgh in data[0] and 55 in data[1] and so on. then i can use these to carry out some calculations for the answer.
in the readGeo
do
Expand|Select|Wrap|Line Numbers
  1.  FileReader file = new FileReader("fileName.txt"); 
  2. BufferedReader input = new BufferedReader(file);
  3. String line  = input.readLine();
  4.  
and now you have the first line in the string line
Feb 13 '07 #15
in the readGeo
do
Expand|Select|Wrap|Line Numbers
  1.  FileReader file = new FileReader("fileName.txt"); 
  2. BufferedReader input = new BufferedReader(file);
  3. String line  = input.readLine();
  4.  
and now you have the first line in the string line
Would I have to do some sort of loop until all of the lines are read in?
Feb 16 '07 #16
r035198x
13,262 8TB
Would I have to do some sort of loop until all of the lines are read in?
Yes. You would need a while loop for it.
Feb 17 '07 #17

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

Similar topics

4
by: Wendy S | last post by:
We've been provided a Java app to use for a homework assignment on databases. It's my own fault for writing inefficient queries, but it keeps taking 100% of my CPU and it takes several minutes to...
1
by: lsprtt | last post by:
I'm trying to create this program and I need some help It includes using JAVA, XHTML and Servlet Create a Web application for dynamic FAQs. The application should obtain the information to...
2
by: Mondo Dynamo | last post by:
A friend of mine is learning java as part of a Business Studies Degree and is finding it very hard to get to grips with. She has an assignment coming up soon and needs some help, she is offering...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
11
by: spawn | last post by:
but I've been struggling with this for far too long and I'm about to start beating my head against the wall. My assignment seemed simple: create a program that will cacluate the running total of...
13
by: spicster | last post by:
I need help with finding an entire inventory value for a java assignment. This is what I have so far. I am new to Java and am struggling. In need of help, thanks import...
2
by: DonE | last post by:
Help me please. I am having problems with my homework assignment. We are to create a product class that hold the item number, the name of the product, the number of units in stock, and the price of...
29
by: s0suk3 | last post by:
Hello, I was hoping to get some opinions on a subject. I've been programming Python for almost two years now. Recently I learned Perl, but frankly I'm not very comfortable with it. Now I want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
tracyyun
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...

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.