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 8 of 8

Thread: First assignment, clueless student, involving coordinates and distances

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default First assignment, clueless student, involving coordinates and distances

    I just started my first highschool programming class this semester and it seems we are delving into Java a bit too quickly and deeply for anyone in the class to understand.
    Despite this, our teacher decided to give us an assignment that most likely seems simple to him yet is too challenging for us (we barely understand how to create basic object contructors, for/while loops, etc.).

    This is the outline of the assignment:

    Introduction
    To travel “as the crow flies” is an expression that refers to travelling between two points directly. A bird isn’t hindrered by only travelling on roads or sidewalks. Obviously this is the shortest route.

    Objective
    Create a program that accepts as input two points on a Cartesian plane. Your program will then output the distance between the points “as the crow flies” as well as the distance by travelling on on a North-South-East-West road grid. The program will also output the difference in distance. Between the two means of travel.

    If that isn’t enough, your program must also determine the equation of the line that the crow would fly from one point to the other.

    Details

    * 5 series of points will be written in a text file called “data1.txt” (provided by me)
    * All points will form a diagonal line
    * equations of lines must be in y=mx+b form
    * All points will be in quadrant 1 (no negative numbers)
    * All calculated distances must be rounded to one decimal point
    * Output must be written to a file called “outfile.txt”
    * Points will be written in input as 2 1 4 11
    o Represents (2,1) (4,11)



    Sample Input

    2 2 8 6

    o 3 4 7 12



    Sample Output

    As the crow flies distance: 7.2

    Street distance:10

    Difference: 2.8

    Crow equation: y = 2/3x + 2/3

    As the crow flies distance:8.9

    Street distance:10

    Difference: 12

    Crow equation: y = 2x - 2

    Grading & Submission

    * Due: Wednesday March 9
    * Late penalty: 2^n% per day, where n is the number of days late. To a max of 5 days.
    * Completion = 20 marks, Style = 5 marks
    * All java files must be submitted electronically & printed
    * Java files must also be sumbitted in hardcopy
    * Output file must be submitted in hardcopy
    * Additionally, your program will also be tested using another input file


    Obviously, I'm not asking anyone to do my assignment for me. We've been taught to pseudocode first, but I'm not really even sure about that, as I don't know enough about Java to be able to break up a program into plain English directions and go from there. We were told we'd be using arrays, and by talking to people who know something about computer programming, I've been advised to create a 100 x 100 array and use that to input coordinates and find the two distances, the difference, and the formula for the line.
    I haven't the faintest idea on how to begin this program, so tips, pointers and ideas are welcome!


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: First assignment, clueless student, involving coordinates and distances

    Any restrictions to what you are aloud to do? Because a lot of this can be done really easily.

    Check out the Point Class (Point (Java Platform SE 6)).

    That can deal with storing your x's and y's and it can even calculate distance for you. The slope would be easy to get (basic math). Then you just need to create the formula, which would be pretty straight forward.

    If you can use the above class, tell me and I'll help you understand the API (which is what the link I provided is) and figure out what you need.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Kerrigan (March 8th, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: First assignment, clueless student, involving coordinates and distances

    This is what I have so far:

    import java.util.Scanner;
     
    public class Crow{
     
       public static void main(String[] args) {
     
            Scanner coordinates = new Scanner (System.in);
            Double x, y, x2, y2;
            System.out.println("Enter first coordinates:");
            x = coordinates.nextDouble();
            y = coordinates.nextDouble();
            System.out.println("Enter second coordinates:");
            x2 = coordinates.nextDouble();
            y2 = coordinates.nextDouble();
     
       }
     
        public double slope (double x, double y, double x2, double y2) {
     
            double slope = 0;
            slope = (y2 - y )/(x2 - x);
            return slope;
       }
     
        public double b (double x, double y, double x2, double y2){
     
            double b = 0;
            b = (y+y2)/2;
            return b;
     
        }
     
        System.out.println("First coordinate: " + x, y);
        System.out.println("Second coordinate: " + x2, y2);
        System.out.println("Formula: " + "y=" + slope + "x+" + b);

    It's easy to read for me, but it seems like it could be significantly simplified, and I'd like to know how to use the point class to store the coordinates I can enter through this program and use them in finding the slope, distance, etc.

  5. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: First assignment, clueless student, involving coordinates and distances

    Furthermore, the lines to print out the coordinates and formula at the bottom are wrong, and I can't figure out where to put them to make them work.
    Also, tips for a cleaner, better-looking code (what to indent, where to space, etc.) would be appreciated, as we are being marked on style, and my teacher is adamant when it comes to making our code look pleasing.

  6. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: First assignment, clueless student, involving coordinates and distances

    Ok, so the coordinates your teacher gives you looks like they will be ints. That is perfect.

    To create a new Point object, something like this works:
    public static void main(String[] args) 
    {
    	Scanner coordinates = new Scanner (System.in);	
    	int x, y, x2, y2;
    	System.out.println("Enter first coordinates:");
    	x = coordinates.nextint();
    	y = coordinates.nextint();
    	System.out.println("Enter second coordinates:");
    	x2 = coordinates.nextint();
    	y2 = coordinates.nextint();
     
    	Point p1 = new Point(x,y);
    	Point p2 = new Point(x2,y2);
     
    	System.out.println("Distance Between The Two Points: "+p1.distance(p2));
     
    }

    That code is an example of how to create two point object and get the distance between the two. That should work, but I haven't compiled it. If there are any compile issues, tell me and I'll fix them. Make sure you import java.awt.Point
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Kerrigan (March 8th, 2011)

  8. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: First assignment, clueless student, involving coordinates and distances

    I changed the Doubles to ints, but the coding didn't work for nextint, and I switched back to Doubles. Will Point take Doubles as inputs instead of ints? And even though I made sure to import the Point class, the Points still didn't work.
    I was also wondering the proper way to go about finding the y-intercept for the "b" value of the y = mx + b formula. The method for finding it I have in my program was pinched off of someone else's program that was similar in function to what mine has to do, although their's is for finding distance and midpoints, not the formula of the line. I accidentally used the formula for midpoint y value instead of finding the y-intercept, a very silly and obvious mistake on my part!
    Can it be found with a simple "if" statement? Something along the lines of "if x=0, find y", just like how some sort of math textbook would read? How would I code that? Would it mess up my x value if I did put in something like this?
    So now I can input coordinates, and (hopefully) figure out how to find the distance and formula, but I still need the distance on the North-South-East-West grid (not the one "as the crow flies"). Is this where I should create 100 x 100 array lists?
    ...because then I really haven't the faintest.

    Just showing my updated code:

    import java.util.Scanner;
    import java.awt.Point;
     
     
    public class Crow{
     
       public static void main(String[] args) {
     
            Scanner coordinates = new Scanner (System.in);
            Double x, y, x2, y2;
            System.out.println("Enter first coordinates:");
            x = coordinates.nextDouble();
            y = coordinates.nextDouble();
            System.out.println("Enter second coordinates:");
            x2 = coordinates.nextDouble();
            y2 = coordinates.nextDouble();
            System.out.println("First coordinate: " + x + " " + y);
            System.out.println("Second coordinate: " + x2 + " " + y2);
            //This inputs coordinates from keyboard input.
     
            Point p1 = new Point(x, y);
            Point p2 = new Point(x2, y2);
     
            System.out.println("As the crow flies distance: " + p1.distance(p2));
            //Put coordinate integers into combined coordinate points
            //Used them to calculate distance between coordinates as the crow flies.
     
     
       }
     
        public double slope (double x, double y, double x2, double y2) {
     
            double slope = 0;
            slope = (y2 - y )/(x2 - x);
            return slope;
            //Finds slope of the line.
     
        }
     
     
        public double b (double x, double y, double x2, double y2){
     
            double b = 0;
            b = (y+y2)/2;
            return b;
            //Finds y-intercept for the formula, or "b" value....if I fix it.
     
        }
     
            System.out.println("Crow equation: " + "y= " + slope + " + " + b);
     
        }

    I'm using NetBeans, and it's telling me that "Point" in "new Point" is incorrect.
    Furthermore, the line, the println for the crow equation, doesn't work at all - did I put it in the wrong place in my code? Where should it be?
    Last edited by Kerrigan; March 8th, 2011 at 07:56 PM.

  9. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: First assignment, clueless student, involving coordinates and distances

    I went to class today and was lucky enough to have the teacher's attention for a bit and the help of a more programming-savvy student. My program has been nearly completely re-done; it doesn't take keyboard input anymore, but takes coordinates from a file like the assignment asks. It can also find almost everything it needs to now.
    All I'm have trouble with is the output to the new output file, and getting the numbers from the equations to be in the proper decimals and fractions that they need to be (fractions or whole numbers in the line formula, but decimals rounded to the nearest tenth for everything else). The coding is also unbearably messy, as the three of us were working on it at once! Help on the style and format of the code would be very useful to me, please and thank you!
    However, I also have some amazing help from an uncle, who wrote up another entire program for this assignment, which sadly I can't use (even though I'm so tempted!) because it's not my own program, but it's helping me to learn a great deal about what I'm supposed to do - though, with the little knowledge of Java I have, I don't understand very much of it.
    It's broken up into the main class and a line class, where in the line class all the equations are made, and the main class puts the numbers in the right format and inputs and outputs all the information.
    It's very helpful, but I'm still very slow at this, and need all the help I can get!

    Here's my updated code, and if anyone's willing to help me figure out how to merge my uncle's program with mine to make the improvements and tweaks that mine needs, please ask and I will put my uncle's code up on here. If not, even little pointers and helpful tidbits are still appreciated!

    package crow;
     
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.BufferedReader;
    import java.io.PrintWriter;
    import java.io.IOException;
    import java.awt.Point;
    import java.util.ArrayList;
     
    public class Crow {
     
        Point first;
        Point second;
        int rise;
        int run;
     
        public Crow(Point p1, Point p2) {
            this.first = p1;
            this.second = p2;
     
     
     
        }
     
        public String slopeAsString() {
     
            return this.rise + "/" + this.run;
     
        }
     
        public double slope() {
     
            double slope = 0;
            slope = (second.y - first.y) / (second.x - first.x);
            return slope;
     
     
        }
     
        public double b(int x, int y, int x2, int y2, double slope) {
     
            double b = 0;
            b = y - (slope * x);
            return b;
     
     
        }
     
        public static void main(String[] args) throws IOException {
     
     
            int x = 0, y = 0, x2 = 0, y2 = 0;
            BufferedReader inputStream = null;
            PrintWriter outputStream = null;
     
            try {
                inputStream =
                        new BufferedReader(new FileReader("data1.txt"));
                outputStream =
                        new PrintWriter(new FileWriter("out1.txt"));
     
                String l;
                String[] strAry;
     
                while ((l = inputStream.readLine()) != null) {
                    strAry = l.split(" ");
     
                    ArrayList<Integer> Coordinates = new ArrayList();
     
     
                    for (String s : strAry) {
                        Coordinates.add(Integer.parseInt(s));
     
                    }
     
                    x = Coordinates.get(0);
                    y = Coordinates.get(1);
                    x2 = Coordinates.get(2);
                    y2 = Coordinates.get(3);
     
                }
     
     
     
     
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
     
     
            }
     
     
     
            Crow myCrow;
     
            Point p1 = new Point(x, y);
            Point p2 = new Point(x2, y2);
     
            myCrow = new Crow(p1, p2);
     
            Double CrowDistance;
            int crowdistance;
            crowdistance = (int) p1.distance(p2) * 10;
            CrowDistance = (double) crowdistance / 10;
     
            System.out.println("As the crow flies distance: " + CrowDistance);
            System.out.println("Street distance: " + (Math.abs(x - x2) + Math.abs(y - y2)));
            System.out.println("Crow equation: " + "y = " + myCrow.slopeAsString() + "x + " + myCrow(x, y, x2, y2, myCrow.slope()));
            System.out.println("Difference: " + ((Math.abs(x2 - x) + Math.abs(y2 - y)) - CrowDistance));
     
     
     
        }
    }
     
    }

  10. #8
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: First assignment, clueless student, involving coordinates and distances

    Thanks for the help, I've got everything sorted out now!
    Handing it in tomorrow, 4% off for being late, but much less than what I would have lost if I handed it in without all the help I got!

Similar Threads

  1. [SOLVED] A little assignment involving arrays.
    By Melawe in forum What's Wrong With My Code?
    Replies: 39
    Last Post: May 1st, 2011, 10:43 PM
  2. arraylists to store coordinates for a snake applet
    By finalfantasyfreak15 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 19th, 2011, 10:21 PM
  3. Student TreeMap
    By raphytaffy in forum Algorithms & Recursion
    Replies: 6
    Last Post: March 2nd, 2010, 12:11 AM
  4. Connect 4 involving minimax.
    By mandar9589 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 21st, 2009, 10:52 AM
  5. [SOLVED] Help me with different activities in Java program
    By xyldon27 in forum Java Theory & Questions
    Replies: 10
    Last Post: June 9th, 2009, 09:42 AM