First assignment, clueless student, involving coordinates and distances

• March 7th, 2011, 04:17 PM
Kerrigan
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

* 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!
• March 7th, 2011, 05:33 PM
aussiemcgr
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.
• March 7th, 2011, 06:55 PM
Kerrigan
Re: First assignment, clueless student, involving coordinates and distances
This is what I have so far:

Code Java:

```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.
• March 7th, 2011, 06:59 PM
Kerrigan
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.
• March 7th, 2011, 08:41 PM
aussiemcgr
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:
Code java:

```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
• March 8th, 2011, 04:50 PM
Kerrigan
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:

Code java:

```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?
• March 9th, 2011, 04:11 PM
Kerrigan
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!

Code java:

```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));       } }   }```
• March 10th, 2011, 03:52 PM
Kerrigan
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!