In: Computer Science
MODIFY your code to:
Add 2 new methods to the Point3d class, each accepting 3 values as input.
pAdd(a1, a2, a3) will ADD a1 to the x coordinate, add a2 to the Y coordinate and add a3 to the z coordinate.
Point3D
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);
}
}
--------
PointTester
package com.java24hours;
import java.awt.*;
class PointTester {
public static void main(String[] arguments) {
Point location1 = new Point(11,22);
Point3D 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, 13)");
location1.move(4,13);
System.out.println("The 2D point is now at (" + location1.x
+ ", " + location1.y + ")");
System.out.println("It's being moved -10 units on both the x
"
+ "and y axes");
location1.translate(-10,-10);
System.out.println("The 2D point ends up at (" + location1.x
+ ", " + location1.y + ")\n");
System.out.println("The 3D point is at (" + location2.x
+ ", " + location2.y + ", " + location2.z + ")");
System.out.println("It's being moved to (10, 22, 71)");
170 HOUR 12: Making the Most of Existing Objects
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 being moved -20 units on the x, y "
+ "and z axes");
location2.translate(-20,-20,-20);
System.out.println("The 3D point ends up at (" + location2.x
+ ", " + location2.y + ", " + location2.z + ")");
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//Point3D.java
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);
}
// method to add a1 to the x coordinate, add a2 to the Y coordinate and add
// a3 to the z coordinate.
public void pAdd(int a1, int a2, int a3) {
x += a1;
y += a2;
z += a3;
}
// method to subtract a1 from the x coordinate, a2 from the Y coordinate and
// a3 from the z coordinate.
public void pSubtract(int a1, int a2, int a3) {
x -= a1;
y -= a2;
z -= a3;
}
}
//PointTester.java
package com.java24hours;
import java.awt.*;
import java.util.Scanner;
class PointTester {
public static void main(String[] arguments) {
Point location1 = new Point(11, 22);
Point3D 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, 13)");
location1.move(4, 13);
System.out.println("The 2D point is now at (" + location1.x + ", "
+ location1.y + ")");
System.out.println("It's being moved -10 units on both the x "
+ "and y axes");
location1.translate(-10, -10);
System.out.println("The 2D point ends up at (" + location1.x + ", "
+ location1.y + ")\n");
System.out.println("The 3D point is at (" + location2.x + ", "
+ location2.y + ", " + location2.z + ")");
System.out.println("It's being moved to (10, 22, 71)");
// 170 HOUR 12: Making the Most of Existing Objects
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 being moved -20 units on the x, y "
+ "and z axes");
location2.translate(-20, -20, -20);
System.out.println("The 3D point ends up at (" + location2.x + ", "
+ location2.y + ", " + location2.z + ")");
// defining a scanner to read input
Scanner sc = new Scanner(System.in);
// asking and reading x, y and z values for a new point
System.out.print("\nEnter x, y and z values for a new point: ");
// creating a Point3D object using the next 3 integer inputs from
// keyboard
Point3D locationx = new Point3D(sc.nextInt(), sc.nextInt(),
sc.nextInt());
// printing new point
System.out.println("The new 3D point is at (" + locationx.x + ", "
+ locationx.y + ", " + locationx.z + ")");
System.out.println("Adding (1,2,3) to this new point");
// testing pAdd()
locationx.pAdd(1, 2, 3);
System.out.println("The 3D point is now at (" + locationx.x + ", "
+ locationx.y + ", " + locationx.z + ")");
System.out.println("Subtracting (1,2,3) from this new point");
// testing pSubtract()
locationx.pSubtract(1, 2, 3);
// the point should be back at original location
System.out.println("The 3D point is now at (" + locationx.x + ", "
+ locationx.y + ", " + locationx.z + ")");
}
}
/*OUTPUT*/
The 2D point is 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 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)
Enter x, y and z values for a new point: 5 8 15
The new 3D point is at (5, 8, 15)
Adding (1,2,3) to this new point
The 3D point is now at (6, 10, 18)
Subtracting (1,2,3) from this new point
The 3D point is now at (5, 8, 15)