that looks like it will work
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.
that looks like it will work
TheMatrixMage (September 18th, 2014)
Also now when I print testing I get the object label. I can change that using a toString method of some sort right? I want to get it to tell me the coordinates right back, but through printing the object instead of the coordinates individually if possible.
you will have to make your own method to tell you the coordinates. Something like
public String toString(){ return x1 +" " +x2 + " " +.... all the values. } Then in your main you can say System.out.print(testing.toString()); and it will give you the coordinates
Now how would I call upon translate from the initial code to shift all the coordinates left 3 and up 5?
You would call it like you do any other method. testing.translate();
Although you need to give the shiftX and shiftY values . Right now in your Rawr class they do not have any.
Awesome, got that part working
So now I need to be able to send the coordinates to calculate every side of the polygon through sideLength1to2 and then have them use perimeter() directly after to calculate the perimeter of the translated polygon.
Is there a way to get it to have it take the coordinates or do I need to use a similar setup as with the previous?
--- Update ---
import java.lang.Math;
public class Rawr {
private double x1;
private double x2;
private double x3;
private double x4;
private double y1;
private double y2;
private double y3;
private double y4;
private double shiftX;
private double shiftY;
private double s1;
private double s2;
private double s3;
private double s4;
public Rawr(){
x1 = 0;
x2 = 0;
x3 = 0;
x4 = 0;
y1 = 0;
y2 = 0;
y3 = 0;
y4 = 0;
}
public void setValues(double newX1, double newX2, double newX3, double newX4, double newY1, double newY2, double newY3, double newY4){
x1 = newX1;
x2 = newX2;
x3 = newX3;
x4 = newX4;
y1 = newY1;
y2 = newY2;
y3 = newY3;
y4 = newY4;
}
public void translate(double shiftX, double shiftY){
x1 = (x1 + shiftX);
x2 = (x2 + shiftX);
x3 = (x3 + shiftX);
x4 = (x4 + shiftX);
y1 = (y1 + shiftY);
y2 = (y2 + shiftY);
y3 = (y3 + shiftY);
y4 = (y4 + shiftY);
}
private void sideLength1to2(double x1, double x2, double x3, double x4, double y1, double y2, double y3, double y4){
double s1 = Math.sqrt((x2 - x1)*(x2 - x1) + (y2 - y1) * (y2 - y1));
double s2 = Math.sqrt((x3 - x2)*(x3 - x2) + (y3 - y2) * (y3 - y2));
double s3 = Math.sqrt((x4 - x3)*(x4 - x3) + (y4 - y3) * (y4 - y3));
double s4 = Math.sqrt((x1 - x4)*(x1 - x4) + (y1 - y4) * (y1 - y4));
return;
}
private void perimeter(){
double p = s1 + s2 + s3 + s4;
}
public String toString(){
return "(" + x1 + "," + y1 + ")" + "(" + x2 + "," + y2 + ")" + "(" + x3 + "," + y3 + ")" + "(" + x4 + "," + y4 + ")";
}
}
Tester
public class RawrTester {
public static void main(String[] args){
Rawr testing = new Rawr();
testing.setValues(1,2,1,4,5,2,3,4);
System.out.println(testing.toString());
testing.translate(-3,5);
System.out.println(testing.toString());
Rawr testing2 = new Rawr();
testing2.setValues(1,2,1,4,5,2,3,4);
System.out.println(testing.sideLength1to2(-2,-1,-2,1,10,7,8,9) + testing2.sideLength1to2(1,2,1,4,5,2,3,4));
}
}
From this I am told that sideLength1to2 is not visible from the type Rawr. How am I to fix this when I am required to leave sideLength1to2 private?
--- Update ---
I guess my only issue is knowing how to use the private class whilst leaving it as private.
Last edited by TheMatrixMage; September 18th, 2014 at 01:29 AM.
Please post your code correctly using code or highlight tags which are explained near the top of this link.