Java homework assignment - Struggling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • matchuglenn
    New Member
    • Feb 2007
    • 7

    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.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    What have you accomplished on this problem alone? What code do you have? What ideas do you have?

    Comment

    • matchuglenn
      New Member
      • Feb 2007
      • 7

      #3
      I have nothing, kind of hoping some Really nice people on here could give me some ideas and help really.

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by matchuglenn
        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?

        Comment

        • abctech
          New Member
          • Dec 2006
          • 157

          #5
          Originally posted by matchuglenn
          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!

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            You better learn how to make jar files too.

            Comment

            • matchuglenn
              New Member
              • Feb 2007
              • 7

              #7
              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
              "

              Code:
              
              package calculator;
              import java.io.*;
              
              
              /**
               *
               * @author Matthew
               */
              public class Main {
                  
                  /** Creates a new instance of Main */
                  public Main() {
                  }
                  
                  /**
                   * @param args the command line arguments
                   */
                  public static void main(String[] args) {
                      BufferedReader in  = getReader("Geo.txt");
                      
                      Geo geo = readGeo(in);
                      while (geo != null)
                      {
                          String msg = geo.location;
                      }
                  }
                  
              
              
              private static BufferedReader getReader(String name)
              {
                  BufferedReader in = null;
                  try
                  {
                      File file = new file(name);
                      in = new BufferedReader(
                              new FileReader(file) );
                  }
                  catch (FileNotFoundException e)
                  {
                      System.out.println("The File does not exsist");
                      System.exit(0);
                  }
                  catch (IOException e)
                  {
                      System.out.println("I/O Error");
                      System.exit(0);
                  }
                  return in;
              }
              
              private static Geo readGeo(BufferedReader in)
              {
                  String location;
                  String line = "";
                  String[] data;
                  String[] data2;
                  
                  try
                  {
                      line = in.readLine();
                  }
                  catch (IOException e)
                  {
                      System.out.println("I/O Error");
                      System.exit(0);        
                  }
                  
                  if (line == null)
                      return null;
                  else 
                  {
                      data = line.split ("\t");
                      location = data[0];
                      data2 = line.split (";");
                      
                      
                      
                  }
              }
              
              }

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by matchuglenn
                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
                "

                Code:
                 
                 
                package calculator;
                import java.io.*;
                 
                 
                /**
                *
                * @author Matthew
                */
                public class Main {
                 
                /** Creates a new instance of Main */
                public Main() {
                }
                 
                /**
                * @param args the command line arguments
                */
                public static void main(String[] args) {
                BufferedReader in = getReader("Geo.txt");
                 
                Geo geo = readGeo(in);
                while (geo != null)
                {
                String msg = geo.location;
                }
                }
                 
                 
                 
                private static BufferedReader getReader(String name)
                {
                BufferedReader in = null;
                try
                {
                File file = new file(name);
                in = new BufferedReader(
                new FileReader(file) );
                }
                catch (FileNotFoundException e)
                {
                System.out.println("The File does not exsist");
                System.exit(0);
                }
                catch (IOException e)
                {
                System.out.println("I/O Error");
                System.exit(0);
                }
                return in;
                }
                 
                private static Geo readGeo(BufferedReader in)
                {
                String location;
                String line = "";
                String[] data;
                String[] data2;
                 
                try
                {
                line = in.readLine();
                }
                catch (IOException e)
                {
                System.out.println("I/O Error");
                System.exit(0); 
                }
                 
                if (line == null)
                return null;
                else 
                {
                data = line.split ("\t");
                location = data[0];
                data2 = line.split (";");
                 
                 
                 
                }
                }
                 
                }
                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.

                Comment

                • RedSon
                  Recognized Expert Expert
                  • Jan 2007
                  • 4980

                  #9
                  Originally posted by matchuglenn
                  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
                  "

                  Code:
                  
                  package calculator;
                  import java.io.*;
                  
                  
                  /**
                   *
                   * @author Matthew
                   */
                  public class Main {
                      
                      /** Creates a new instance of Main */
                      public Main() {
                      }
                      
                      /**
                       * @param args the command line arguments
                       */
                      public static void main(String[] args) {
                          BufferedReader in  = getReader("Geo.txt");
                          
                          Geo geo = readGeo(in);
                          while (geo != null)
                          {
                              String msg = geo.location;
                          }
                      }
                      
                  
                  
                  private static BufferedReader getReader(String name)
                  {
                      BufferedReader in = null;
                      try
                      {
                          File file = new file(name);
                          in = new BufferedReader(
                                  new FileReader(file) );
                      }
                      catch (FileNotFoundException e)
                      {
                          System.out.println("The File does not exsist");
                          System.exit(0);
                      }
                      catch (IOException e)
                      {
                          System.out.println("I/O Error");
                          System.exit(0);
                      }
                      return in;
                  }
                  
                  private static Geo readGeo(BufferedReader in)
                  {
                      String location;
                      String line = "";
                      String[] data;
                      String[] data2;
                      
                      try
                      {
                          line = in.readLine();
                      }
                      catch (IOException e)
                      {
                          System.out.println("I/O Error");
                          System.exit(0);        
                      }
                      
                      if (line == null)
                          return null;
                      else 
                      {
                          data = line.split ("\t");
                          location = data[0];
                          data2 = line.split (";");
                          
                          
                          
                      }
                  }
                  
                  }
                  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.
                  Last edited by RedSon; Feb 13 '07, 03:46 PM. Reason: r0 answered concurrently

                  Comment

                  • matchuglenn
                    New Member
                    • Feb 2007
                    • 7

                    #10
                    Originally posted by r035198x
                    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!!!

                    Comment

                    • matchuglenn
                      New Member
                      • Feb 2007
                      • 7

                      #11
                      Originally posted by RedSon
                      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.

                      Comment

                      • RedSon
                        Recognized Expert Expert
                        • Jan 2007
                        • 4980

                        #12
                        Originally posted by matchuglenn
                        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.

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by matchuglenn
                          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.

                          Comment

                          • matchuglenn
                            New Member
                            • Feb 2007
                            • 7

                            #14
                            Originally posted by r035198x
                            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.

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by matchuglenn
                              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
                              Code:
                               FileReader file = new FileReader("fileName.txt"); 
                              BufferedReader input = new BufferedReader(file);
                              String line  = input.readLine();
                              and now you have the first line in the string line

                              Comment

                              Working...