Points Class Problem
I'm supposed to simulate a cat and dog chase for class. If the cat reaches the tree, the cat wins, and if not then dog wins.
I have been looking for answers or what I should do for this problem, any help would be greatly appreciated.
I ask the user to input 5 values X,Y,Z,A, and B which will stand for the X and Y coordinates, Z is the distance that the tree is on the X coordinate. A and B are the cat and dogs speed.
My problem is trying to input what the users enters into the dogs position in the plane. X,Y.
here is my code and compiler errors:
import java.util.Scanner;
import java.awt.Point;
publicclass program3
{
publicstaticvoid main(String[] args)
{
PointCat=new Point();
PointDog=new Point();
doubleX, Y, Z, A, B;
doubleCatStepSize = 0.001, DogStepSize;
Scanner Keyboard =new Scanner(System.in);
System.out.print("Please enter 5 numbers which are between 1 and 1,000 ");
System.out.println("that will represent the variable X,Y,Z,A, and B: ");
System.out.print("X: ");
X = Keyboard.nextDouble();
System.out.print("Y: ");
Y = Keyboard.nextDouble();
System.out.print("Z: ");
Z = Keyboard.nextDouble();
System.out.print("A: ");
A = Keyboard.nextDouble();
System.out.print("B: ");
B = Keyboard.nextDouble();
Dog.setX(X);
Dog.setY(Y);
DogStepSize = CatStepSize * (B/A);
}
}
Compiler errors:
program3.java:46: cannot find symbol
symbol : method setX(double)
location: class java.awt.Point
Dog.setX(X);
^
program3.java:47: cannot find symbol
symbol : method setY(double)
location: class java.awt.Point
Dog.setY(Y);
^
[url http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Point.html]java.awt.Point[/url]Look there for the correct methods. Also, all variable names should start with a lower case letter and give a good description of their purpose.
The method you gave me worked, but now the other method that my professor gave me isn't working.Is there something else I need to import?
heres is the section that is throwing errors now.
import java.util.Scanner;
import java.awt.Point;
public class program3
{
public static void main(String[] args)
{
PointCat= new Point();
PointDog= new Point();
doublex, y, z, a, b;
doubleCatStepSize = 0.001, DogStepSize;
Scanner Keyboard = new Scanner(System.in);
System.out.print("Please enter 5 numbers which are between 1 and 1,000 ");
System.out.println("that will represent the variable x,y,z,a, and b: ");
System.out.print("x: ");
x = Keyboard.nextDouble();
System.out.print("y: ");
y = Keyboard.nextDouble();
System.out.print("z: ");
z = Keyboard.nextDouble();
System.out.print("a: ");
a = Keyboard.nextDouble();
System.out.print("b: ");
b = Keyboard.nextDouble();
Dog.setLocation(x,y);
DogStepSize = CatStepSize * (b/a);
Cat.moveInDirection(0.0, z);
Cat.moveTowardPoint(x, y, CatStepSize);
}
}
Compiler Errors:
program3.java:49: cannot find symbol
symbol : method moveInDirection(double,double)
location: class java.awt.Point
Cat.moveInDirection(0.0, z);
^
program3.java:49: cannot find symbol
symbol : method moveTowardPoint(double,double,double)
location: class java.awt.Point
Cat.moveTowardPoint(x,y,CatStepSize);
^
2 errors
None of the methods he has given me are working with my double variables, or ints when I changed them.
The methods he has given me are:
setX, setY, getX, getY, moveInDirection, moveTowardPoint, and distanceFrom.
He says we will have to use all and I know that, but none of them are working for me. Sorry if I seem completely retarded but he gave me that and no import statements, and also said we would need our variables to be doubles.
Contradicting info is driving me off the wall, so any help again would be great, thanks.
Message was edited by:
Bmiller234
>The methods he has given me are:
>setX, setY, getX, getY, moveInDirection, moveTowardPoint,
>and distanceFrom.
Not enough information. If I say to you,
"Use the method 'myMethod' in a program."
Can you do it? If not, why not? What is the minimum amount of information you have to know to be able to use myMethod?
This error:
program3.java:49: cannot find symbol
symbol : method moveInDirection(double,double)
location: class java.awt.Point
Cat.moveInDirection(0.0, z);
is telling you that Cat does not have a method called moveInDirection() that takes two doubles as arguments. What is Cat? You defined Cat to be a Point. The Point class is defined by java, and you can only call methods that are defined in the Point class. You should visit the javadocs and look at the definition of the Point class. Does the Point class define a method called moveInDirection() that takes two doubles as arguments?
I've looked through my notes and found this.
Paper says:
From now on students may use our new and fully-operational Point class. If you wish to use this new object in one of your programs you must be sure to place the file Point.java in the same folder with the program.
Point.java:
import java.util.Random;
public class Point
{
private double X, Y;
private final double PI = 3.14159265358979;
public Point()
{
X = 0.0;
Y = 0.0;
}
// end constructor
public void setX(double A)
{
X = A;
return;
}
// end public method setX
public void setY(double A)
{
Y = A;
return;
}
// end public method setY
public double getX()
{
return X;
}
// end public method getX
public double getY()
{
return Y;
}
// end public method getY
public void moveInDirection(double dir, double dist)
{
X = X + dist*Math.cos(dir*PI/180.0);
Y = Y + dist*Math.sin(dir*PI/180.0);
return;
}
// end public method moveInDirection
public void moveTowardPoint(double a, double b, double increment)
{
double distance;
distance = Math.sqrt((a-X)*(a-X) + (b-Y)*(b-Y));
X = X + (increment/distance)*(a-X);
Y = Y + (increment/distance)*(b-Y);
return;
}
// end public method moveTowardPoint
public double distanceFrom(double A, double B)
{
return Math.sqrt((X-A)*(X-A) + (Y-B)*(Y-B));
}
// end public method distanceFrom
public String toString()
{
String result;
result = "(" + X + ", " + Y +")";
return result;
}
// end public method toString
public int whichQuadrant()
{
if (X > 0.0 && Y > 0.0)
return 1;
else if (X < 0.0 && Y > 0.0)
return 2;
else if (X < 0.0 && Y < 0.0)
return 3;
else if (X > 0.0 && Y < 0.0)
return 4;
else
return 0;
// end if
}
// end public method whichQuadrant
public void randomLocation(double A, double B, double C, double D)
{
Random generator = new Random();
if (A >= B || C >= D)
{
System.out.println("Illegal parameters for method randomLocation!");
System.exit(0);
}
// end if
X = (B-A)*generator.nextDouble() + A;
Y = (D-C)*generator.nextDouble() + C;
return;
}
// end public method randomLocation
}
// end class Point
So I added this file to my cs200 folder (where all my java programs are held) and it still doesn't work. Do I need to import this somehow? or add it to my program all together? Or will this even allow me to use doubles as arguments.
Sorry for the complete confusion but I have not been told this before its getting rediculous. I know that the point class has no methods named moveTowardPoint or moveInDirection, but just is confusing when the professor has not told me anything specific...at all.
Sorry again for this stupidity but I have been doing this for probably about 2 months now.
Try commenting out the import java.awt.Point line from the source for your program. And make sure the professor's Point.java is where he told you to put it.You should not need to import it if both files are in the default package.Good luck.
BobCa at 2007-7-15 4:42:13 >

<Try commenting out the
><import java.awt.Point
><line from the source for your program.
><And make sure the professor's Point.java is where he told you to put it.
><You should not need to import it if both files are in the default package.
><Good luck.
haha wow.
Now I feel completely rediculous. Thats all that it was. Thank you BobC, and to everyone else who helped.
Always the small details.>
Well after all of that working I'm now stuck.
I believe I'm stuck in the while loop, but not sure why, well have an Idea.
I need to create a while loop that continues until the cat reaches the tree, or until the dog catches the cat. It compiles good, but will never produce output, or takes too long to.
Any help would be greatly appreciated again.
Here is the code:
import java.util.Scanner;
//import java.awt.Point;
public class program3
{
public static void main(String[] args)
{
PointCat= new Point();
PointDog= new Point();
doublex, y, z, a, b;
doubleCatStepSize = 0.001, DogStepSize;
boolean CatReachesTree = false, DogCatchesCat = false;
Scanner Keyboard = new Scanner(System.in);
System.out.print("Please enter 5 numbers which are between 1 and 1,000 ");
System.out.println("that will represent the variable x,y,z,a, and b: ");
System.out.print("x: ");
x = Keyboard.nextDouble();
System.out.print("y: ");
y = Keyboard.nextDouble();
System.out.print("z: ");
z = Keyboard.nextDouble();
System.out.print("a: ");
a = Keyboard.nextDouble();
System.out.print("b: ");
b = Keyboard.nextDouble();
Dog.setX(x);
Dog.setY(y);
DogStepSize = CatStepSize * (b/a);
Cat.moveTowardPoint(z, 0.0, CatStepSize);
Cat.getX();
Cat.getY();
Dog.moveTowardPoint((Cat.getX()),(Cat.getY()),DogStepSize);
Dog.getX();
Dog.getY();
//System.out.println((Cat.distanceFrom(0.0, z)));
while (!CatReachesTree && !DogCatchesCat);
{
Cat.moveTowardPoint(z, 0.0, CatStepSize);
Dog.moveTowardPoint((Cat.getX()), (Cat.getY()), DogStepSize);
if (Cat.getX() >= z)
CatReachesTree = true;
if (Dog.getX() >= Cat.getX())
DogCatchesCat = true;
if (CatReachesTree = true)
System.out.println("The cat made it safely to the Tree!");
if (DogCatchesCat = true)
System.out.print("The dog caught the cat!");
}
}
}
Once again a clean compile just no output. Maybe my code is a little off, but I have tested up the page to Cat.getX() and it is moving.
Message was edited by:
Bmiller234
if (CatReachesTree = true)Use ==, not =, and rename your variables!
import java.util.Scanner;
//import java.awt.Point;
public class program3
{
public static void main(String[] args)
{
Pointcat= new Point();
Pointdog= new Point();
doublex, y, z, a, b;
doublecatStepSize = 0.001, dogStepSize;
boolean catReachesTree = false, dogCatchesCat = false;
Scanner Keyboard = new Scanner(System.in);
System.out.print("Please enter 5 numbers which are between 1 and 1,000 ");
System.out.println("that will represent the variable x,y,z,a, and b: ");
System.out.print("x: ");
x = Keyboard.nextDouble();
System.out.print("y: ");
y = Keyboard.nextDouble();
System.out.print("z: ");
z = Keyboard.nextDouble();
System.out.print("a: ");
a = Keyboard.nextDouble();
System.out.print("b: ");
b = Keyboard.nextDouble();
dog.setX(x);
dog.setY(y);
dogStepSize = catStepSize * (b/a);
cat.moveTowardPoint(z, 0.0, catStepSize);
cat.getX();
cat.getY();
dog.moveTowardPoint((cat.getX()),(cat.getY()),dogStepSize);
dog.getX();
dog.getY();
//System.out.println((Cat.distanceFrom(0.0, z)));
while (!catReachesTree && !dogCatchesCat);
{
cat.moveTowardPoint(z, 0.0, catStepSize);
dog.moveTowardPoint((cat.getX()), (cat.getY()), dogStepSize);
if (cat.getX() >= z)
catReachesTree = true;
if (dog.getX() >= cat.getX())
dogCatchesCat = true;
if (catReachesTree == true)
System.out.println("The cat made it safely to the Tree!");
if (dogCatchesCat == true)
System.out.print("The dog caught the cat!");
}
}
}
Fixed the statements, and it is still locked up, no output.
I'm guessing my conditions are incorrect.
Trying to move the cat first toward z (which is the tree), then the dog is moving toward the cat through the cats movement toward the tree.
Sorry about the variables, they are all lowercase, and I am new to making variables respectful.
Sorry again for this tedious posting, but I am stuck.
The help on the other part got me through all of this and now I believe I am stuck in a loop either that or the step size is too small so it is taking to long?
Thanks again.
Another thing, shouldn't the professors Point class extend the orginal Point class in awt? All he is doing is adding funcanablitily to the original class so extending it would be logical...
>"Another thing, shouldn't the professors Point class extend the orginal Point class in awt? All he is doing is adding funcanablitily to the original class so extending it would be logical..."
I would think so, but my professors Point class did not work until I commented awt. Weird yes, but it was the only way it would work that I could see.
Theres a semi-colon after your while statement.
I guess this is what I get for staring at this screen all day, careless errors.
Okay now the loop works, but now my problem is all I get is that the dog caught the cat, even with the values that the cat should make it safely to the tree.
I'm going to work with this for a while and if I'm still stuck may attempt to post again.
Thank you for your responses and not ridiculing me of my carelessness.
Bah.
What would happen if you have a class called Point importing java.awt.Point? What would happen if Point extended Point? I'm assuming compiler hissy fits, I should try it.
Maybe it would be easier to follow if you gave your variables more meaningful names.
Well! This is all Pointless
Thank you, Vagabon, for thinking out loud.
Atleast I wasn't using caps, then I'd be thinking really loud.
> Atleast I wasn't using capsAnd millions of exclamation marks.
haha All my programs get the response, "What!? This is Stupid!".I agree haha.But hey now I'm infinately looped, or its either the cat always wins, or the dog always wins.Gears of War until I clear my head from beginner syndrome.
look back at the algorythem (sp?) for determining what moves where, see if something is amiss, I'll check it out but I can't compile java on this machine, it's the schools with not compiler (not even java to run the applets on the web).
Move the
if (catReachesTree == true)
System.out.println("The cat made it safely to the Tree!");
if (dogCatchesCat == true)
System.out.print("The dog caught the cat!");
out of the loop.
Whats happening? You need to be more specific.> Gears of War until I clear my head from beginner syndrome.Is it good? I dont have enough money right now...
Just thinking out loud:
//cat.method( Y2 value, Y1 value, distance?)
cat.moveTowardPoint(z, 0.0, catStepSize);
//dog.method(cat's x, cat's y, distance)
//Why are you useing the cat's x? nvm
dog.moveTowardPoint((cat.getX()), (cat.getY()), dogStepSize);
if (cat.getX() >= z)
catReachesTree = true;
if (dog.getX() >= cat.getX())
dogCatchesCat = true;
public void moveTowardPoint(double a, double b, double increment)
{
double distance;
//a = y2? b = y1? increment is what? how far they are moveing
//okay totalDistance - how far is moved
distance = Math.sqrt((a-X)*(a-X) + (b-Y)*(b-Y));
X = X + (increment/distance)*(a-X);
Y = Y + (increment/distance)*(b-Y);
return;
}
I think I see it, remove the cat.getY() and put 0.0
Alright I'm trying that now, I also moved out the If statements.Be back with results
What is z? What is X? What is Y? If you corrected those variables names this would be much easier to debug!
yeah sorry about that.X and Y = Dogs X and Y coordinates.Z = Trees X coordinate because Y will always be 0.0A and B are the cat and dogs speed/stepsize.
Still only produces the result that the Cat reaches tree.
Here is my code as of now, still playing with it:
import java.util.Scanner;
//import java.awt.Point;
public class program3
{
public static void main(String[] args)
{
Pointcat= new Point();
Pointdog= new Point();
doublex, y, z, a, b;
doublecatStepSize = 0.001, dogStepSize;
boolean catReachesTree = false, dogCatchesCat = false;
Scanner Keyboard = new Scanner(System.in);
System.out.print("Please enter 5 numbers which are between 1 and 1,000 ");
System.out.println("that will represent the variable x,y,z,a, and b: ");
System.out.print("x: ");
x = Keyboard.nextDouble();
System.out.print("y: ");
y = Keyboard.nextDouble();
System.out.print("z: ");
z = Keyboard.nextDouble();
System.out.print("a: ");
a = Keyboard.nextDouble();
System.out.print("b: ");
b = Keyboard.nextDouble();
dog.setX(x);
dog.setY(y);
dogStepSize = catStepSize * (b/a);
cat.moveTowardPoint(z, 0.0, catStepSize);
cat.getX();
cat.getY();
dog.moveTowardPoint((cat.getX()),0.0,dogStepSize);
dog.getX();
dog.getY();
//System.out.println((Cat.distanceFrom(0.0, z)));
while (!catReachesTree && !dogCatchesCat)
{
cat.moveTowardPoint(z, 0.0, catStepSize);
dog.moveTowardPoint((cat.getX()), (cat.getY()), dogStepSize);
if (cat.getX() >= z)
catReachesTree = true;
if (dog.getX() >= cat.getX())
dogCatchesCat = true;
}
if (catReachesTree = true)
System.out.println("The cat made it safely to the tree!");
else
System.out.println("The dog caught the cat!");
}
}
Alright I moved it out of the loop, and I know I need to use the distance method somehow.
Which the method he gave me to use is: cat.distanceFrom(double, double);
I wish I could use the methods in your code but can't against prof.
Message was edited by:
Bmiller234
made more reable (note don't have time to finish, change variable names)
import java.util.Scanner;
//import java.awt.Point;
public class program3
{
public static void main(String[] args)
{
Pointcat= new Point();
Pointdog= new Point();
doublex, y, z, a, b;
doublecatStepSize = 0.001, dogStepSize;
boolean catReachesTree = false, dogCatchesCat = false;
Scanner Keyboard = new Scanner(System.in);
System.out.print("Please enter 5 numbers which are between 1 and 1,000 ");
System.out.println("that will represent the variable x,y,z,a, and b: ");
System.out.print("x: ");
x = Keyboard.nextDouble();
System.out.print("y: ");
y = Keyboard.nextDouble();
System.out.print("z: ");
z = Keyboard.nextDouble();
System.out.print("a: ");
a = Keyboard.nextDouble();
System.out.print("b: ");
b = Keyboard.nextDouble();
dog.setX(x);
dog.setY(y);
//dogStepSize = catStepSize * (b/a);
//simplified to dogStepSize = 1 *(b/a) on to
dogStepSize = b/a;
cat.moveTowardPoint(z, 0.0, catStepSize);
//cat.getX(); not used
//cat.getY();
dog.moveTowardPoint((cat.getX()),0.0,dogStepSize);
dog.getX();
dog.getY();
//System.out.println((Cat.distanceFrom(0.0, z)));
while (!catReachesTree && !dogCatchesCat)
{
cat.moveTowardPoint(z, 0.0, catStepSize);
dog.moveTowardPoint((cat.getX()), (cat.getY()), dogStepSize);
if (cat.getX() >= z)
catReachesTree = true;
if (dog.getX() >= cat.getX())
dogCatchesCat = true;
}
if (catReachesTree = true)
System.out.println("The cat made it safely to the tree!");
else
System.out.println("The dog caught the cat!");
}
}
What a sec, are not the cat and dog starting out at the same place? No nvm that
g2g
Ok. If x and y are the Dog's x and y coordinates, shouldnt those be stored in your Point class? You can access them with Dog.getX() and Dog.getY(). Also, You are setting the x coordinates to be greater than 0, and the tree's x coordinate to be 0. Then you are saying that the cat has caught the tree if the cat's x is greater than the tree's x, which it is automatically.
And what is the point of just randomly calling Dog.getX()?
Sorry about the random calling, I was using that earlier to test, and print to make sure the right values were coming out.
Also the cat begins at the origin, while the dog begins where X and Y is inputted by the user.
I thought by using
if (cat.getX() >= z)
A print statement would be thrown if and when the cat reaches the tree or surpasses the tree. Since the cat only moves along the X-axis and the tree is at a fixed point on the X-axis.
Ex. Cat (0,0) and Tree(12,0) 12 units away from cat.
Okay I think I see my problem.
Or maybe not.
I thought a = cats speed
b = dogs speed
Also from what I understood is that the catStepSize is to be locked at 0.001 which I thought that stepsize is the speed, correct?
So I need to divide the catStepSize by b and a also?
Not sure but I'm going to fool around with it.
You set catStepSize to .001, then never change it. You then set dogStepSize to b/a. Im not sure what you are trying to do here.
Now I'm not either.I was using our professors tips and one was this: catStepSize = 0.001 , then scale dogs speed accordingly.
So, you want catStepSize to always be .001, then you should set dogStepSize to something higher than that.
Tried that also.
And changed the If-then-else also, to one of the other hints I got.
Which is, and doesn't make sense to me so throws that out as being a tip:
if (cat.getX() <= dogStepSize)
System.out.println("The dog caught the cat!");
else
System.out.println("The cat made it safely to the tree!");
Which doesn't make sense, if this was the case then the whole while loop could just be thrown out. All I would need is to divide dogStepSize by b/a and then use the if-then-else.
So that makes no sense to me.
Now stuck for good.
Thinking of just going early tomorrow up to the professor and presenting my problem.
I really, really, really hate to do this, but look at this and see if it works:
import java.util.Scanner;
import java.util.Random;
public class program3
{
public static void main(String[] args)
{
Pointcat= new Point();
Pointdog= new Point();
doublecatX, dogX, treeX;
doublecatStepSize, dogStepSize;
boolean catReachesTree = false, dogCatchesCat = false;
Scanner Keyboard = new Scanner(System.in);
System.out.println("Please enter 5 numbers which are between 1 and 1,000 ");
System.out.print("Dog's x: ");
dogX = Keyboard.nextDouble();
System.out.print("Cat's x: ");
catX = Keyboard.nextDouble();
System.out.print("Tree's x: ");
treeX = Keyboard.nextDouble();
System.out.print("Cat Step Size: ");
catStepSize = Keyboard.nextDouble();
System.out.print("Dog Step Size: ");
dogStepSize = Keyboard.nextDouble();
dog.setX(dogX);
dog.setY(0.0);
cat.setX(catX);
cat.setY(0.0);
while (!catReachesTree && !dogCatchesCat)
{
cat.moveTowardPoint(treeX, 0.0, catStepSize);
dog.moveTowardPoint((cat.getX()), (cat.getY()), dogStepSize);
if (cat.getX() >= treeX)
catReachesTree = true;
if (dog.getX() >= cat.getX())
dogCatchesCat = true;
}
if (catReachesTree = true)
System.out.println("The cat made it safely to the tree!");
else
System.out.println("The dog caught the cat!");
}
}
I also tried that a little bit ago, well not as neat because I'm jumbling everything together. But I set another double like you did with treeX.
Also only to get always: Cat reaches tree.
I know people hate giving away basically full code, but i did try that, and pretty much like that.
Thanks anyway though I'm gonna work with this until something clicks though.
I'm just going to disregard professors tips because they don't seem to be doing anything but complicating the situation.
Thanks for all the replies.
General dubug hint:
Add a
System.out.println("Dog: X:" +dog.getX() + " Y: " dog.getY()");
System.out.println("Cat: X:" +cat.getX() + " Y: " cat.getY()");
inside the while loop.
Then look at the results, so you can see what happens during a run.
You will often get a cubic *ton of text, but you will also be able to see what the numbers are doing, and say "Ahah! I forgot to (whatever)!"
Then, once it works, comment out or remove the debug lines.
BobCa at 2007-7-21 12:50:04 >

D'OH! I cant believe I didnt see that!!!
Change the end to:
if (catReachesTree == true)
System.out.println("The cat made it safely to the tree!");
else
System.out.println("The dog caught the cat!");
It works now.
I told you that back in reply 9!