I have actually lurked here quite a bit in the past to find help with issues but this time I actually felt like asking for myself here is my code so far...
import java.util.*;
import java.io.*;
class travel
{
private static class City
//defines inner class object City
{
private int x, y;// variables declared for coordinates
private String name;// variable declared for city name
public City()//Default constructor
{}
public City(int x, int y, String n)
/*creats custom constructor to allow me to pass values while creating object
**I probably don't need this but here it is
*/
{
this.x = x;
this.y = y;
this.name = n;
}
public int getX(){return x;}
//method to get x
public int getY(){return y;}
//method to get y
public String getNAME(){return name;}
//method to get name
public void setX(int x){this.x = x;}
//method sets x
public void setY(int y){this.y = y;}
//method sets y
public void setNAME(String n){this.name = n;}
//method sets name
}
public static void main(String[] args)
{
City[] Cities;//set up an array
Cities = new City[5];// limit length of array
//creat 5 instances of the City class
City C1 = new City();
City C2 = new City();
City C3 = new City();
City C4 = new City();
City C5 = new City();
//Fill the array with City objects
Cities[0] = C1;
Cities[1] = C2;
Cities[2] = C3;
Cities[3] = C4;
Cities[4] = C5;
travel startup = new travel(Cities);
}
public travel (City[] AA)
{
getCities(AA);// call method to get yser input and set objects' attributes accordingly
//Call some recursion method???
}
public void getCities(City[] AA)
{
Scanner scan = new Scanner(System.in);//set up a scanner call
int i, x, y;
String n, trash;
for (i=0; i < 5;i++)//this sets the city object name and coordinates as per user input
{
System.out.println("City name.");
System.out.println("");
n = scan.nextLine();
AA[i].setNAME(n);
System.out.println("X coordinates.");
System.out.println("");
x = scan.nextInt();
AA[i].setX(x);
trash = scan.nextLine();
System.out.println("Y coordinates.");
System.out.println("");
y = scan.nextInt();
trash = scan.nextLine();
AA[i].setY(y);
System.out.println("You entered: " + n + " Coordinates X:" + x + " Y:" + y + ".");
System.out.println("");
}
for (i=0; i < 5;i++)//This is just to test that I am setting each object correctly
{
n = AA[i].getNAME();
x = AA[i].getX();
y = AA[i].getY();
System.out.println(n + " at X:" + x + " Y:" + y);
}
}
/*********************FAIL**************
public double totalDistance(City[] AA)
{
int i =0;
double xSub = AA[i].getX() - AA[i-1].getX();
double ySub = AA[i].getY() - AA[i-1].getY();
double distance = Math.pow(xSub, 2) + Math.pow(ySub, 2);
distance = Math.round(Math.sqrt(distance));
System.out.println("Distance From " + AA[i].getNAME() + " to " + AA[i-1].getNAME() + " is " + distance + " miles.");
if (i == 1)
{
return distance;
}
else
{
return distance+totalDistance(City[] AA, i-1);
}
}*/
}

New Topic/Question
Reply




MultiQuote







|