Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Please help i cant figure it out - problems with Location and Line

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Please help i cant figure it out - problems with Location and Line

    package javaapplication10;
    /**
         * @param args the command line arguments
         */
     
    import javax.swing.*;
    /**
     *
     * @author mpjames
     */
    public class Stations {
     
        public static void main(String[] args) {
     
            // declairing variables
            double tempBearing;
            double tempLength;
            double distanceToClosest;
            double distanceToClosestDest;
            double tempBearingDest;
            double tempLengthDest;
     
     
            //an array of stations
            Station[] stations = new Station[16];
     
            //populate the array with stations and values
            stations[0] = new Station("windmillHill",new Location(60.0,12.0), new Line ("Docks Line"));
            stations[1] = new Station("trim bridge",new Location(63.0,6.0), new Line ("Docks Line"));
            stations[2] = new Station("piracCresent",new Location(80.0,8.0), new Line ("Gyratory Line"));
            stations[3] = new Station("easton",new Location(85.0,12.0), new Line ("Brunel Line"));
            stations[4] = new Station("parkway",new Location(102.0,9.0), new Line ("Brunel Line"));
            stations[5] = new Station("templeFields",new Location(140.0,7.0), new Line ("Gyratory Line"));
            stations[6] = new Station("stDennis",new Location(180.0,1.0), new Line ("Docks Line"));
            stations[7] = new Station("moxbridge",new Location(180.0,2.0), new Line ("Brunel Line"));
            stations[8] = new Station("shakespeareCourt",new Location(192.0,5.0), new Line ("Gyratory Line"));
            stations[9] = new Station("weston-on-shore",new Location(232.0,14.0), new Line ("Docks Line"));
            stations[10] = new Station("jacobsWell",new Location(240.0,3.0), new Line ("Docks Line"));
            stations[11] = new Station("central",new Location(243.0,5.0), new Line ("Docks Line"));
            stations[12] = new Station("newbridge",new Location(245.0,11.0), new Line ("Docks Line"));
            stations[13] = new Station("tivoli",new Location(275.0,6.0), new Line ("Brunel Line"));
            stations[14] = new Station("cliftonStreet",new Location(285.0,11.0), new Line ("Brunel Line"));
            stations[15] = new Station("stJudesHill",new Location(355.0,4.0), new Line ("Gyratory Line"));
     
            //To get users current bearings and and locations and creating objects
     
            //String temp  = JOptionPane.showInputDialog("Bearing to current location?");
            tempBearing = Double.parseDouble(JOptionPane.showInputDialog("Bearing to current location?"));
     
            //temp = JOptionPane.showInputDialog("distance from origin of current?");
            tempLength = Double.parseDouble(JOptionPane.showInputDialog("distance from origin of current?"));
     
           //String temp  = JOptionPane.showInputDialog("Bearing to current location?");
            tempBearingDest = Double.parseDouble(JOptionPane.showInputDialog("Bearing to Destination?"));
     
            //temp = JOptionPane.showInputDialog("distance from origin of current?");
            tempLengthDest = Double.parseDouble(JOptionPane.showInputDialog("distance to end Destination?"));
     
     
            Location current = new Location(tempBearing, tempLength);
     
            Station closest = stations[0];
            distanceToClosest = current.calcDistance(stations[0].location);
     
            for(int i = 1; i < stations.length; i++){
                double tempDist = current.calcDistance(stations[i].location);
     
                if(tempDist < distanceToClosest){
                    closest = stations[i];
                    distanceToClosest = tempDist;
                }//if (tempDist < distanceToClosest)
            }//for loop
     
     
            //2nd loop for end destination
            Location currentDest = new Location(tempBearingDest, tempLengthDest);
     
            Station closestDest = stations[0];
            distanceToClosestDest = currentDest.calcDistance(stations[0].location);
     
            for(int i = 1; i < stations.length; i++){
                double tempDist = currentDest.calcDistance(stations[i].location);
     
               if(tempDist < distanceToClosestDest){
                    closestDest = stations[i];
                    distanceToClosestDest = tempDist;
                }//if (tempDist < distanceToClosest)
            }//for loop
     
     
     
            JOptionPane.showMessageDialog(null, "the closest station is " + closest.name + closest.line.lname
                    + "\nIt is " + distanceToClosest +  " away.");
     
            JOptionPane.showMessageDialog(null, "the closest station to your destination is " + closestDest.name
                    + "\nIt is " + distanceToClosestDest +  " away.");
     
      }
     
    }
     
    class Station{  // a station class
     
        Location location;
        String name;
        Line line;
     
        public Station(String n, Location l, Line r){
            name = n;
            location = l;
            line = r;
        }
    class Line {
       String lname;
       public  Line (String String) {
        lname = String;
    }
     
    }//class station
     
    class Location{
        double bearing;
        double length;
     
        public Location(){
            bearing = 0.0;
            length = 0.0;
        }
     
        public Location(double b, double l){
            bearing = b;
            length = l;
        }
     
        public double calcDistance(Location l){
            double angle = Math.abs(bearing - l.bearing);//calculating the angle between the bearings
            double temp = (length*length)+(l.length*l.length)-(2*length*l.length*Math.cos(Math.toRadians(angle)));
            return Math.pow(temp, 0.5);
     
     
     
        }
     
    }
     
    }


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Please help i cant figure it out - problems with Location and Line

    Erm... What exactly is your problem? What errors are you getting? I have no idea how to help you because I don't know the problem.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. So im making a freehand Line,Rect,Oval; but problems
    By Matta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 20th, 2011, 08:25 AM
  2. Replies: 10
    Last Post: September 16th, 2011, 08:49 PM
  3. Reading a file line by line using the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 08:34 AM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 07:32 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 07:32 AM