1. Member
Join Date
Mar 2014
Posts
9
Rep Power
0

## coordinate converter

Hi, I made these classes, one for converting cart to polar then calculating the distance between 2 points and the other for polar to cart then calculating the distance between 2 points... I am using the test class to input a list of ten coords and add up the total distance between them. I am having trouble when I create the instance of polar or cartesian I dont know what to send to it (right now I just have null) and that is giving me issues. Any help is appreciated. Thanks!

Java Code:
```public class Test
{
public static void main(String[] args)
{

//TestConversion test = new TestConversion();

System.out.println("I made it to here test");

Test.TestConversion();

}

public static double[] TestConversion()
{
System.out.println("X,Y Coordinates are:");
double[] xCoords = {86.92, 70.93, 97.74, 30.90, 51.66, 0.830, 55.91, 32.92, 65.26, 83.90};
double[] yCoords = {2.47, 27.81, 34.36, 35.14, 31.70, 21.77, 66.62, 75.23, 72.53, 4.710};
double cartesianDistance =0;
double polarDistance = 0;

Cartesian startPoint = new Cartesian(null);
Cartesian endPoint = new Cartesian(null);

Polar polar1 = new Polar(null);
Polar polar2 = new Polar(null);

for ( int row = 0; row < xCoords.length - 1; row++ )
{
// loop through columns of current row
startPoint.setX(xCoords[ row ]);
startPoint.setY(yCoords[ row ]);
endPoint.setX(xCoords[ row + 1 ]);
endPoint.setY(yCoords[ row + 1 ]);

double totalDistance = 0;
double distance = startPoint.getDistance(endPoint);

totalDistance = totalDistance + distance;
cartesianDistance = totalDistance;

}

for ( int row = 0; row < xCoords.length - 1; row++ )
{
// loop through columns of current row
startPoint.setX(xCoords[ row ]);
startPoint.setY(yCoords[ row ]);
endPoint.setX(xCoords[ row + 1 ]);
endPoint.setY(yCoords[ row + 1 ]);

double totalDistance = 0;
double distance = polar1.calculateDistancePolar(polar2);

totalDistance = totalDistance + distance;
polarDistance = totalDistance;

}
double[] bothDistances = {cartesianDistance, polarDistance};

System.out.println("Cartesian Distance test =" +cartesianDistance++);
System.out.println("Polar Distance test =" +polarDistance++);
return bothDistances;

} // end Test
}```
Java Code:
```public class Cartesian
{

// Price two Arg Constructor
public Cartesian(Polar polar)
{
this.x = polar.getRho() * Math.cos(polar.getTheta());
this.y = polar.getRho() * Math.sin(polar.getTheta());
}// End Constructor

public double getDistance(Cartesian cartesian)
{
return Math.sqrt(Math.pow((getX() - cartesian.getX()), 2.0) + Math.pow((getY() - cartesian.getY()), 2.0));
}

public double getX()
{
return this.x;
}
public void setX(double x)
{
this.x = x;
}
public double getY()
{
return this.y;
}
public void setY(double y)
{
this.y = y;
}

private double x;
private double y;
}```
Java Code:
```public class Polar
{

// Price two Arg Constructor
//
public Polar(Cartesian cartesian)
{
this.rho = Math.hypot(cartesian.getX(), cartesian.getY());
this.theta = Math.atan2(cartesian.getY(),cartesian.getX());

}// End Constructor

public double calculateDistancePolar(Polar polar)
{
return Math.sqrt(Math.pow(getRho(), 2.0) + Math.pow(polar.rho, 2.0) - 2.0 * getRho() *polar.rho * Math.cos(polar.theta - getTheta()));
}// End calculateDistancePolar Method

public double getTheta()
{
return this.theta;
}

public void setTheta(double theta)
{
this.theta = theta;
}

public double getRho()
{
return this.rho;
}

public void setRho(double rho)
{
this.rho = rho;
}

private double theta;
private double rho;```

2. ## Re: coordinate converter

when I create the instance of polar or cartesian I dont know what to send to it
Look at the definitions for the constructors for those classes and see what type of args they take.

How do you create an instance of either class without using an instance of the other class?

3. Member
Join Date
Mar 2014
Posts
9
Rep Power
0

## Re: coordinate converter

Originally Posted by Norm
Look at the definitions for the constructors for those classes and see what type of args they take.

How do you create an instance of either class without using an instance of the other class?
Ok I have this now. Im still a bit stuck
im trying to use this line to run the Polar distance calculator but it is looking for it within the cart class.
excerpt:
Java Code:
`double distance = startPoint.calculateDistancePolar(endPoint);`

Java Code:
```public class Test
{
public static void main(String[] args)
{

//TestConversion test = new TestConversion();

System.out.println("I made it to here test");

Test.TestConversion();

}

public static double[] TestConversion()
{
System.out.println("X,Y Coordinates are:");
double[] xCoords = {86.92, 70.93, 97.74, 30.90, 51.66, 0.830, 55.91, 32.92, 65.26, 83.90};
double[] yCoords = {2.47, 27.81, 34.36, 35.14, 31.70, 21.77, 66.62, 75.23, 72.53, 4.710};
double cartesianDistance =0;
double polarDistance = 0;
System.out.println("I made it to here test");

Cartesian startPoint = new Cartesian(new Polar(startPoint));
Cartesian endPoint = new Cartesian(new Polar(endPoint));

for ( int row = 0; row < xCoords.length - 1; row++ )
{
// loop through columns of current row
startPoint.setX(xCoords[ row ]);
startPoint.setY(yCoords[ row ]);
endPoint.setX(xCoords[ row + 1 ]);
endPoint.setY(yCoords[ row + 1 ]);

double totalDistance = 0;
double distance = startPoint.getDistance(endPoint);

totalDistance = totalDistance + distance;
cartesianDistance = totalDistance;

}

for ( int row = 0; row < xCoords.length - 1; row++ )
{
// loop through columns of current row
startPoint.setX(xCoords[ row ]);
startPoint.setY(yCoords[ row ]);
endPoint.setX(xCoords[ row + 1 ]);
endPoint.setY(yCoords[ row + 1 ]);

double totalDistance = 0;

double distance = startPoint.calculateDistancePolar(endPoint);

totalDistance = totalDistance + distance;
polarDistance = totalDistance;

}

double[] bothDistances = {cartesianDistance, polarDistance};

System.out.println("Cartesian Distance test =" +cartesianDistance++);
System.out.println("Polar Distance test =" +polarDistance++);
return bothDistances;

} // end Test
}```

4. ## Re: coordinate converter

use this line to run the Polar distance calculator but it is looking for it within the cart class.
Please explain why you think that. Copy the full text of any error messages and paste them here.

what class is startPoint?

5. Member
Join Date
Mar 2014
Posts
9
Rep Power
0

## Re: coordinate converter

Originally Posted by Norm
Please explain why you think that. Copy the full text of any error messages and paste them here.

what class is startPoint?
I dont really know. startPoint is cartesian. which is why its being screwed up and that makes sense.
XML Code:
```Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method calculateDistancePolar(Cartesian) is undefined for the type Cartesian

at Test.TestConversion(Test.java:60)
at Test.main(Test.java:15)
I made it to here test```
So I made this change below to create an instance of polar1 and polar2 at line 27 and 28.

And now it errors at line 24 and 25 with this error:
XML Code:
```I made it to here test
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable startPoint may not have been initialized
The local variable endPoint may not have been initialized

at Test.TestConversion(Test.java:29)
at Test.main(Test.java:15)```
Java Code:
```public class Test
{
public static void main(String[] args)
{

//TestConversion test = new TestConversion();

System.out.println("I made it to here test");

Test.TestConversion();

}

public static double[] TestConversion()
{
System.out.println("X,Y Coordinates are:");
double[] xCoords = {86.92, 70.93, 97.74, 30.90, 51.66, 0.830, 55.91, 32.92, 65.26, 83.90};
double[] yCoords = {2.47, 27.81, 34.36, 35.14, 31.70, 21.77, 66.62, 75.23, 72.53, 4.710};
double cartesianDistance =0;
double polarDistance = 0;
System.out.println("I made it to here test");

Cartesian startPoint = new Cartesian(new Polar(startPoint));
Cartesian endPoint = new Cartesian(new Polar(endPoint));

Polar polar1 = new Polar(startPoint);
Polar polar2 = new Polar(endPoint);

for ( int row = 0; row < xCoords.length - 1; row++ )
{
// loop through columns of current row
startPoint.setX(xCoords[ row ]);
startPoint.setY(yCoords[ row ]);
endPoint.setX(xCoords[ row + 1 ]);
endPoint.setY(yCoords[ row + 1 ]);

double totalDistance = 0;
double distance = startPoint.getDistance(endPoint);

totalDistance = totalDistance + distance;
cartesianDistance = totalDistance;

}

for ( int row = 0; row < xCoords.length - 1; row++ )
{
// loop through columns of current row
startPoint.setX(xCoords[ row ]);
startPoint.setY(yCoords[ row ]);
endPoint.setX(xCoords[ row + 1 ]);
endPoint.setY(yCoords[ row + 1 ]);

double totalDistance = 0;

double distance = polar1.calculateDistancePolar(polar2);

totalDistance = totalDistance + distance;
polarDistance = totalDistance;

}

double[] bothDistances = {cartesianDistance, polarDistance};

System.out.println("Cartesian Distance test =" +cartesianDistance++);
System.out.println("Polar Distance test =" +polarDistance++);
return bothDistances;

} // end Test
}```

6. ## Re: coordinate converter

That doesn't make sense. The code is using the value of the variables BEFORE they have been given any value. That is why the compiler says: startPoint may not have been initialized
The code needs to assign startPoint a value BEFORE trying to use that value.

Did you see this question I asked earlier:
How do you create an instance of either class without using an instance of the other class?

7. Member
Join Date
Mar 2014
Posts
9
Rep Power
0

## Re: coordinate converter

Originally Posted by Norm
That doesn't make sense. The code is using the value of the variables BEFORE they have been given any value. That is why the compiler says: startPoint may not have been initialized
The code needs to assign startPoint a value BEFORE trying to use that value.

Did you see this question I asked earlier:
How do you create an instance of either class without using an instance of the other class?
Im lost. How would I use an instance of the class? How do I initialize startPoint and endPoint?? I am new to this I am trying to understand how to use constructors this way by passing objects into them.

8. ## Re: coordinate converter

Both classes need constructors that do not require an instance of the other class.

Your assignment or class material must describe how to create instances of the class from numbers for example.
Those constructors can be used to create startPoint and endPoint.

9. Member
Join Date
Mar 2014
Posts
9
Rep Power
0

## Re: coordinate converter

Originally Posted by Norm
Both classes need constructors that do not require an instance of the other class.

Your assignment or class material must describe how to create instances of the class from numbers for example.
Those constructors can be used to create startPoint and endPoint.
hmm, gotcha that is how I initially had created my classes but then this is what my instructor showed us this to use. What if I just have the cartesian class takes an input of x and y doubles in the constructor and leave the polar class constructor alone. That should work, right?

10. ## Re: coordinate converter

What are the assignment's requirements?
In general I'd think both classes should have constructors that take numbers. Ask your instructor.

11. Member
Join Date
Mar 2014
Posts
9
Rep Power
0

## Re: coordinate converter

Originally Posted by Norm
What are the assignment's requirements?
In general I'd think both classes should have constructors that take numbers. Ask your instructor.
He just wants 2 classes, both to convert cart to polar and vis-versa in the constructor, then one method to calculate distance between two points. Then we got a matrix of ten cartesian points we have to use to test.

I think I have it figured out, Im having a little trouble figuring out how to write an if statement saying I if its at the 2nd to last value of the array do... line 39 of test class.

Let me know if you see anything real weird I might have done, I still have to go through and check everything but the values for the total distances for both polar and Cartesian are the same which is a good sign

Java Code:
```import java.util.Arrays;

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

//TestConversion test = new TestConversion();
Test.TestConversion();

}

public static double[] TestConversion()
{
System.out.println("X,Y Coordinates are:");
double[] xCoords = {86.92, 70.93, 97.74, 30.90, 51.66, 0.830, 55.91, 32.92, 65.26, 83.90};
double[] yCoords = {2.47, 27.81, 34.36, 35.14, 31.70, 21.77, 66.62, 75.23, 72.53, 4.710};
double[] rhoCoords = new double[10];
double[] thetaCoords = new double[10];
double cartesianDistance =0;
double polarDistance = 0;

/*
Cartesian startPoint = new Cartesian(new Polar(startPoint));
Cartesian endPoint = new Cartesian(new Polar(endPoint));

Polar polar1 = new Polar(startPoint);
Polar polar2 = new Polar(endPoint);
*/
for ( int row = 0; row < xCoords.length - 1; row++ )
{
Cartesian startPoint = new Cartesian(new Polar(xCoords[row], yCoords[row]));
Cartesian endPoint = new Cartesian(new Polar(xCoords[row+1], yCoords[row+1]));

rhoCoords [row] = new Polar(xCoords[row], yCoords[row]).getRho();
thetaCoords [row] = new Polar(xCoords[row], yCoords[row]).getTheta();

if (row.equals(xCoords.length - 1))
{
rhoCoords [row+1] = new Polar(xCoords[row+1], yCoords[row+1]).getRho();
thetaCoords [row+1] = new Polar(xCoords[row+1], yCoords[row+1]).getTheta();
}

double cartTotalDistance = 0;
double cartDistance = startPoint.getDistance(endPoint);

cartTotalDistance = cartTotalDistance + cartDistance;
cartesianDistance = cartTotalDistance;

Polar polar1 = new Polar(xCoords[row], yCoords[row]);
Polar polar2 = new Polar(xCoords[row+1], yCoords[row+1]);

double polarTotalDistance = 0;
double polarDistance1 = polar1.calculateDistancePolar(polar2);

polarTotalDistance = polarTotalDistance +polarDistance1;
polarDistance = polarTotalDistance;
}

double[] bothDistances = {cartesianDistance, polarDistance};

System.out.println(Arrays.toString(thetaCoords));
System.out.println(Arrays.toString(rhoCoords));

System.out.println("Cartesian Distance test =" +cartesianDistance++);
System.out.println("Polar Distance test =" +polarDistance++);
return bothDistances;

} // end Test
}```
Java Code:
```public class Polar
{

// Price two Arg Constructor
//
public Polar(double x, double y)
{
this.rho = Math.hypot(x, y);
this.theta = Math.atan2(y,x);

}// End Constructor

public double calculateDistancePolar(Polar polar)
{
return Math.sqrt(Math.pow(getRho(), 2.0) + Math.pow(polar.rho, 2.0) - 2.0 * getRho() *polar.rho * Math.cos(polar.theta - getTheta()));
}// End calculateDistancePolar Method

public double getTheta()
{
return this.theta;
}

public void setTheta(double theta)
{
this.theta = theta;
}

public double getRho()
{
return this.rho;
}

public void setRho(double rho)
{
this.rho = rho;
}

private double theta;
private double rho;

}```
Java Code:
```public class Cartesian
{

// Price two Arg Constructor
public Cartesian(Polar polar)
{
this.x = polar.getRho() * Math.cos(polar.getTheta());
this.y = polar.getRho() * Math.sin(polar.getTheta());
}// End Constructor

public double getDistance(Cartesian cartesian)
{
return Math.sqrt(Math.pow((getX() - cartesian.getX()), 2.0) + Math.pow((getY() - cartesian.getY()), 2.0));
}

public double getX()
{
return this.x;
}
public void setX(double x)
{
this.x = x;
}
public double getY()
{
return this.y;
}
public void setY(double y)
{
this.y = y;
}

private double x;
private double y;
}```

12. ## Re: coordinate converter

It still seems that there should be a constructor for the Cartesian class that does not require a Polar object.

Also why do the methods that get the distance have different names?

13. Member
Join Date
Mar 2014
Posts
9
Rep Power
0

## Re: coordinate converter

method question is a good point. I can change that, would make more sense. And would I create a 2nd constructor (question just for my future knowledge can you have multiple constructors?)? or do you mean just make the constructor so it take the polar coords?

14. ## Re: coordinate converter

can you have multiple constructors?
Yes, as many as you want as long as the compiler can tell the difference between one and the other by having different parameters.

Have a second constructor that takes x and y.

15. Member
Join Date
Mar 2014
Posts
9
Rep Power
0

## Re: coordinate converter

Originally Posted by Norm
Yes, as many as you want as long as the compiler can tell the difference between one and the other by having different parameters.

Have a second constructor that takes x and y.
will do thanks.

16. Member
Join Date
Mar 2014
Posts
9
Rep Power
0

## Re: coordinate converter

Awesome, thanks for your help. Its working beautifully now.

Java Code:
```public class Cartesian
{

//Cartesian two Arg Constructor
public Cartesian(double rho, double theta)
{
this.x = rho * Math.cos(theta);
this.y = rho * Math.sin(theta);
}//End Constructor

//Cartesian Polar Object Constructor
public Cartesian(Polar polar)
{
this.x = polar.getRho() * Math.cos(polar.getTheta());
this.y = polar.getRho() * Math.sin(polar.getTheta());
}// End Constructor

public double getDistance(Cartesian cartesian)
{
return Math.sqrt(Math.pow((getX() - cartesian.getX()), 2.0) + Math.pow((getY() - cartesian.getY()), 2.0));
}

public double getX()
{
return this.x;
}
public void setX(double x)
{
this.x = x;
}
public double getY()
{
return this.y;
}
public void setY(double y)
{
this.y = y;
}

private double x;
private double y;
}```
Java Code:
```public class Polar
{

// Polar two Arg Constructor
public Polar(double x, double y)
{
this.rho = Math.hypot(x, y);
this.theta = Math.atan2(y, x);
}// End Constructor

//Polar Cartesian Object Constructor
public Polar(Cartesian cartesian)
{
this.rho = Math.hypot(cartesian.getX(), cartesian.getY());
this.theta = Math.atan2(cartesian.getY(), cartesian.getX());
}// End Constructor

public double getDistance(Polar polar)
{
return Math.sqrt(Math.pow(getRho(), 2.0) + Math.pow(polar.rho, 2.0) - 2.0
* getRho() * polar.rho * Math.cos(polar.theta - getTheta()));
}// End calculateDistancePolar Method

public double getTheta()
{
return this.theta;
}

public void setTheta(double theta)
{
this.theta = theta;
}

public double getRho()
{
return this.rho;
}

public void setRho(double rho)
{
this.rho = rho;
}

private double theta;
private double rho;

}```
Java Code:
```import java.util.Arrays;

public class Test
{
public static void main(String[] args)
{
//TestConversion test = new TestConversion();
Test.TestConversion();
}

public static double[] TestConversion()
{
double[] xCoords = {86.92, 70.93, 97.74, 30.90, 51.66, 0.830, 55.91, 32.92, 65.26, 83.90};
double[] yCoords = {2.47, 27.81, 34.36, 35.14, 31.70, 21.77, 66.62, 75.23, 72.53, 4.710};
double[] rhoCoords = new double[10];
double[] thetaCoords = new double[10];
double cartesianDistance =0;
double polarDistance = 0;
double cartTotalDistance = 0;
double polarTotalDistance = 0;

for ( int row = 0; row < xCoords.length - 1; row++ )
{
Cartesian startPoint = new Cartesian(new Polar(xCoords[row], yCoords[row]));
Cartesian endPoint = new Cartesian(new Polar(xCoords[row+1], yCoords[row+1]));

rhoCoords [row] = new Polar(xCoords[row], yCoords[row]).getRho();
thetaCoords [row] = new Polar(xCoords[row], yCoords[row]).getTheta();

if (row == xCoords.length - 2)
{
rhoCoords [row+1] = new Polar(xCoords[row+1], yCoords[row+1]).getRho();
thetaCoords [row+1] = new Polar(xCoords[row+1], yCoords[row+1]).getTheta();
}

double cartDistance = startPoint.getDistance(endPoint);

cartTotalDistance = cartTotalDistance + cartDistance;
cartesianDistance = cartTotalDistance;

Polar polar1 = new Polar(xCoords[row], yCoords[row]);
Polar polar2 = new Polar(xCoords[row+1], yCoords[row+1]);

double polarDistance1 = polar1.getDistance(polar2);

polarTotalDistance = polarTotalDistance +polarDistance1;
polarDistance = polarTotalDistance;
System.out.println("Coordinate Distances added up by step:");
System.out.println("Cartesian Distance test" +row+ " =" +cartesianDistance++);
System.out.println("Polar Distance test " +row+ " =" +polarDistance++);
}

System.out.println("-----------------------------------------------------------");
double[] bothDistances = {cartesianDistance, polarDistance};
System.out.println("theta coordinates are:");
System.out.println(Arrays.toString(thetaCoords));
System.out.println("rho coordinates are:");
System.out.println(Arrays.toString(rhoCoords));

System.out.println("Cartesian Distance test =" +cartesianDistance++);
System.out.println("Polar Distance test =" +polarDistance++);
return bothDistances;

} // end Test
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•