Reputation: 13
I've just started learning Java so sorry if this is a noob question but I've been through each line of code and I don't understand what's going wrong. The code is from the book "Java in 24 hours".
The aim of the code is to take a starting 2D and 3D point, then move them and translate them. First it asks me to make a 3D point class:
package com.java24hours;
import java.awt.*;
public class Point3D extends Point
{
public int z;
public Point3D(int x, int y, int z)
{
super(x,y);
this.z = z;
}
public void move(int x, int y, int z)
{
this.z = z;
super.move(x,y);
}
public void translate(int x, int y, int z)
{
this.z += z;
super.translate(x,y);
}
}
It then calls for a tester class which uses the Points3D to move and translate the 3D points:
package com.java24hours;
import java.awt.*;
class PointTester
{
public static void main(String[] arguments)
{
Point location1 = new Point(11,22);
Point location2 = new Point3D(7,6,64);
System.out.println("The 2D point is at ("+location1.x + "," + location1.y +")");
System.out.println("It's being moved to (4,11)");
location1.move(4,11);
System.out.println("The 2D point is now at (" + location1.x + "," + location1.y + ")");
System.out.println("It's now being moved -10 in both the x and y axis");
location1.translate(-10,-10);
System.out.println("The 2D point is now at (" + location1.x +"," + location1.y + ")\n");
System.out.println("The 3D point is at (" + location2.x + "," + location2.y + "," + location.z + ")");
System.out.println("It's being moved to (10,22,71)");
location2.move(10,22,71);
System.out.println("The 3D point is now at (" + location2.x + "," + location2.y + "," + location2.z + ")");
System.out.println("It's now going to be moved -20 units in the x y and z axis");
location2.translate(-20,-20,-20);
System.out.println("It's now at (" + location2.x + "," + location2.y + "," + location2.z + ")");
}
}
This creates the following errors on the lines featuring location2.z:
cannot find symbol method move cannot be applied to given types method translate cannot be applied to given types
Source is here: https://www.informit.com/library/content.aspx?b=STY_Java2_24hours&seqNum=140
This is roughly what it should give me (I changed some of the wording):
The 2D point is located at (11, 22)
It's being moved to (4, 13)
The 2D point is now at (4, 13)
It's being moved -10 units on both the x and y axes
The 2D point ends up at (-6, 3)
The 3D point is located at (7, 6, 64)
It's being moved to (10, 22, 71)
The 3D point is now at (10, 22, 71)
It's being moved -20 units on the x, y and z axes
The 3D point ends up at (-10, 2, 51)
I think my confusion is that I declared the z variable in the Point3D class, I created the newPoint3D which accepted three variables, and then when I try and use the third variable later on it can't find it.
Thanks
Upvotes: 1
Views: 450
Reputation: 719239
The problem is this line:
Point location2 = new Point3D(7,6,64);
You have created a Point3D
instance and assigned it to a Point
variable. The object that is currently in location2
at that point has a z
field, but since you declared the variable as a Point
... the compiler has to assume that it is a Point
instance, which doesn't have a z
field.
Hence the compiler tells you that location2.z
is a compilation error.
The fix is this:
Point3D location2 = new Point3D(7,6,64);
OK, so why is Java designed to not allow that?
Consider an example like this:
Point location2 = test ? new Point3D(7,6,64) : new Point(7,6);
System.out.println(location2.z);
If the compiler accepted that, and test
was false
, what should happen? In a dynamically typed language, that would give you a runtime error because you attempted to access a field that does not exist. But Java is statically typed by design, and that is not acceptable.
In fact, Java takes the view that when you declare a variable has a type Point
the following code can only treat its value as a Point
.
Upvotes: 0
Reputation: 1866
Either define 'x' and 'move' in Point or refer to location1 and location2 by Point3D
Upvotes: 0
Reputation: 2291
You have a syntax problem in your code because you are using location.z
instead of location2.z
Also, you are using Point
class as a reference to create an object of Point3D
. You cannot access variable z
from a Point
class due to Polymorphism concepts(I suggest you look into it) as the Point3D
inherits the Point
class and so, the variable z
is defined within the Point3D
class and not the Point
.
Concluding, you should instantiate your 3D Point as below:
Point3D location2 = new Point3D(7,6,64);
Upvotes: 1
Reputation: 1803
I'm assuming that the base Point
's move()
method accepts two parameters. However, you're calling that method with three: location2.move(10,22,71);
Sure, you know that the Point
contained in location2
is actually a Point3D
, but the compiler thinks it's just a regular Point
. Change the type of location2
to Point3D
and you should be fine.
Upvotes: 0
Reputation: 11006
This is happening because you've declared your location2
variable as a Point. Its real type is Point3D, but you're referencing it as type Point and so Point3D variable z is not visible. (This is a fundamental principal of object oriented programming called polymorphism, but don't worry about that yet as you're just starting to learn!)
You can fix this by declaring your location2
variable as a Point3D, like so:
Point location1 = new Point(11,22);
//here's the fixed line of code.
Point3D location2 = new Point3D(7,6,64);
//[...] rest of your code
Upvotes: 0