Staredit Network

Staredit Network -> Computers and Technical -> java
Report, edit, etc...Posted by Mp)7-7 on 2006-09-29 at 16:31:41
Ok, in my java class that I just started. I havent had any past experience with java before. If anyone could give me some help that would be fine.

This is the question I got and the sooner someone could answer this for me it would help.

Write an application that reads the (x, y) coordinates and computes the distance between the two points.


I dont know if it wants me to have user input to select the coordinate or what. Can anyone help me with this?
Report, edit, etc...Posted by Vibrator on 2006-09-29 at 18:33:26
Err well if you can do the input on your own I can help you with the rest but unless you tell me what type of input you need its kinda difficult.

for the part that I can help with you just need to use the distance formula

in java it would be Math.sqrt(Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2))
Report, edit, etc...Posted by Mp)7-7 on 2006-09-29 at 21:44:19
I want it to be, user input to have them type two different numbers then have it do the x, y thing and ya do what the question asks. Does that help?
Report, edit, etc...Posted by Vibrator on 2006-09-29 at 22:15:43
CODE

import System.io.*
public class Blah{
 public static void main(String args[]){
   InputStreamReader isr = new InputStreamReader ( System.in );
   BufferedReader stdin = new BufferedReader ( isr );
   String coord = stdin.readLine()
 }
}


That code reads a line from the console, do whatever parsing you want and use that formula i gave above for determining distance. For converting strings to integers use int = Integer.parseInt(string);
Report, edit, etc...Posted by Mp)7-7 on 2006-09-30 at 15:19:44
This is how I have it set up

CODE

import java.io.*;

public class Coordinates
{  
   public static void main(String args[])
   {            
           InputStreamReader isr = new InputStreamReader ( System.in );
           BufferedReader stdin = new BufferedReader ( isr );
           String coord = stdin.readLine();
   }
}


I am getting an error for

CODE

String coord = stdin.readLine();


error = unreported exception java.io.IOException; must be caught or declared to be thrown
Report, edit, etc...Posted by Vibrator on 2006-09-30 at 16:18:37
QUOTE(Mp)7-7 @ Sep 30 2006, 03:19 PM)
This is how I have it set up

CODE

import java.io.*;

public class Coordinates
{  
   public static void main(String args[])
   {            
           InputStreamReader isr = new InputStreamReader ( System.in );
           BufferedReader stdin = new BufferedReader ( isr );
           String coord = stdin.readLine();
   }
}


I am getting an error for

CODE

String coord = stdin.readLine();


error = unreported exception java.io.IOException; must be caught or declared to be thrown
[right][snapback]570237[/snapback][/right]



Your going to need something like this

CODE

import java.io.*;

public class Coordinates
{  
   public static void main(String args[]) throws IOException
   {            
           InputStreamReader isr = new InputStreamReader ( System.in );
           BufferedReader stdin = new BufferedReader ( isr );
           String coord = stdin.readLine();
   }
}
Report, edit, etc...Posted by Mp)7-7 on 2006-09-30 at 16:32:12
I did this, and there are no erros, but it doesnt do anything. Is their something I am missing?

QUOTE

import java.io.*;

public class Coordinates

  public static void main(String args[]) throws IOException
  {           
          InputStreamReader isr = new InputStreamReader ( System.in );
          BufferedReader stdin = new BufferedReader ( isr );
          String coord = stdin.readLine();
  }
}

Report, edit, etc...Posted by Vibrator on 2006-09-30 at 16:46:43
All that peice of code does is read a line off the console, it doesnt output anything
Report, edit, etc...Posted by Mp)7-7 on 2006-09-30 at 17:07:39
The console would be?
Report, edit, etc...Posted by Vibrator on 2006-09-30 at 17:09:43
You have done simple programs in java right? Command Line?
Report, edit, etc...Posted by Mp)7-7 on 2006-09-30 at 17:17:39
Ya, really simple ones.

But what do I do now. I am kinda lost, cause I am taking a java coarse and a xhtml/CSS class at the same time. I sometimes get confused. So right now I dont know what else I have to do.
Report, edit, etc...Posted by Vibrator on 2006-09-30 at 17:22:39
I'm not sure how your compiling and running your java stuff but the console is what java runs in. If your compiling it by yourself with javac.exe then the console is what u start in order to compile and run it.
Report, edit, etc...Posted by Mp)7-7 on 2006-09-30 at 17:34:27
I am using Bluej, have you ever heard of it? It kind of like saving a notepad to .html so you can see what it does. It runs off java, reads the code, tells you if it is right and does whatever you tell the code to do.

Ex. I made a program that took a number that you put in (3) and multiplies whatever that number is by .6 to see how many kilometers is in miles.

CODE

import java.util.Scanner;

public class KtoM
{
   public static void main(String[] args)
   {
       Scanner scan = new Scanner (System.in);
             
       double kilometers;
       double miles;
       
       System.out.println ("Enter number of kilometers:");
       kilometers = scan.nextDouble();
       
       miles = kilometers * .6;
       

       System.out.println (kilometers + " kilometers is equal to " + miles + " miles");

   }
}


It converts kilometers to miles.
Report, edit, etc...Posted by Vibrator on 2006-09-30 at 17:45:37
The code I gave you reads strings tha you enter, and I gave you the forumla to calculate distance with coordinates (x1, y1) (x2, y2) from that you should be able to do the rest.
Report, edit, etc...Posted by Mp)7-7 on 2006-10-01 at 10:18:51
So would I do the same thing that I did for the Kilometers to Miles and ask for two coordinates, and then the code you gave me now wil compute the distance between the two and what not.

Edit:
I have tried for a while now and I havent figured it out yet. I can't figure it out. Could you please tell me what I have to do now?
Report, edit, etc...Posted by Vibrator on 2006-10-01 at 18:47:40
CODE

import java.io.*;

public class Coordinates
{
 public static void main(String args[]) throws IOException
 {          
         InputStreamReader isr = new InputStreamReader ( System.in );
         BufferedReader stdin = new BufferedReader ( isr );
         System.out.println("Enter coordinates\n");
         String coord = stdin.readLine();
         int x1, x2, y1, y2;
         x1 = Integer.parseInt((coord.split(" ")[0]).split(",")[0]);
         y1 = Integer.parseInt((coord.split(" ")[0]).split(",")[1]);
         x2 = Integer.parseInt((coord.split(" ")[1]).split(",")[0]);
         y2 = Integer.parseInt((coord.split(" ")[1]).split(",")[1]);
         System.out.println("Distance: " + Math.sqrt(Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2)));
 }
}


You enter coordinates like this x,y x,y
Report, edit, etc...Posted by Mp)7-7 on 2006-10-02 at 23:12:24
Thanks, but I already figured it out while SEN was down, I had some free time.

CODE

import java.util.Scanner;

public class Coordinates
{
public static void main (String[] args)

{
Scanner scan = new Scanner(System.in);
double x1, x2, y1, y2, x, y, dist;

System.out.println("Enter an integer for x1");
x1 = scan.nextDouble();

System.out.println("Enter an integer for y1");
y1 = scan.nextDouble();

System.out.println("Enter an integer for x2");
x2 = scan.nextDouble();

System.out.println("Enter an integer for y2");
y2 = scan.nextDouble();

x = Math.pow(x2-x1,2);
y = Math.pow(y2-y1,2);
dist = Math.sqrt(x+y);

System.out.println("The distance between (" + x1 +"," + y1 +") "+
"and (" + x2 +"," + y2 + ") is" + dist);
}
}


There is no more reason for this to be open. Thank you for all your help vibrator!

Locked
Next Page (1)