Question

In: Computer Science

MODIFY your code to: add a new point, locationx, that has 3 dimensions, and are set...

MODIFY your code to:

  1. add a new point, locationx, that has 3 dimensions, and are set by PASSING IN the arguments for the X and Y coordinates. The Z coordinate can be hard-coded (extra credit if it is passed in too). ADD CODE to print the new point location, and note to the user it is the new point using input arguments
  2. Add 2 new methods to the Point3d class, each accepting 3 values as input.

  3. pAdd(a1, a2, a3) will ADD a1 to the x coordinate, add a2 to the Y coordinate and add a3 to the z coordinate.

    1. example: point (1,2,3) calls 'add' with params (-1, 1, -1) results in point moved to new location (0,3,2). CALL the new add method using the new locationx point. PRINT the original location, the ADD parameters and the NEW location.
    2. pSubtract(a1, a2, a3) qill do the same, but subtract instead of add. Using the SAME example as above, the resulting location would be (2,1,4) (remember, subtracting a negative results in an addition... you do not need to 'code' that. JAVA will do the subtraction correctly).
    3. 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 + ")");
      }
      }

Solutions

Expert Solution

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)

Related Solutions

Modify the original code and add an additional function of your choice. The function should be...
Modify the original code and add an additional function of your choice. The function should be unique and something you created for this assignment. Support your experimentation with screen captures of executing the new code. Prepare a test table with at least 3 distinct test cases listing input and expected output for your unique function. #include <stdio.h> void printHelp () { printf ("\n"); printf ("a: a(x) = x*x\n"); printf ("b: b(x) = x*x*x\n"); printf ("c: c(x) = x^2 + 2*x...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality for multiplication (*) Adding JUnit tests Add one appropriately-named method to test some valid values for tryParseInt You will use an assertEquals You'll check that tryParseInt returns the expected value The values to test: "-2" "-1" "0" "1" "2" Hint: You will need to cast the return value from tryParseInt to an int e.g., (int) ValidationHelper.tryParseInt("1") Add one appropriately-named method to test some invalid...
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to...
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to call these functions instead of the inline code in your main(). In addition, add error checking for the user input. If the user inputs an incorrect value prompt the user to re-enter the number. The flow of the code should look something like: /** Calculate the mean of a vector of floating point numbers **/ float mean(vector& values) /** Calculate the standard deviation from...
The following is the original code, (add, t1, 3, 5) (assign, a, t1) (add, t2, 3,...
The following is the original code, (add, t1, 3, 5) (assign, a, t1) (add, t2, 3, 5) (sub, t3, t2, z) (assign, b, t3) Constant folding is applied to give the following, (assign, t1, 8) (assign, a, t1) (assign, t2, 8) (sub, t3, t2, z) (assign, b, t3) After the constant folding is applied, the (assign, t2, 8) becomes redundant. What optimizations should take place in order to remove this redundancy in the constant folded code? Also show the optimized...
A. Add code (see below for details) to the methods "set" and "get" in the following...
A. Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index]...
A. Add code (see below for details) to the methods "set" and "get" in the following...
A. Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index]...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be sure to include start() and stop() methods to start and stop the clock, respectively.Then write a program that lets the user control the clock with the start and stop buttons.
Take the following code and modify the if(args >= 3) statement to work with a dynamic...
Take the following code and modify the if(args >= 3) statement to work with a dynamic amount of inputs. Example: ./cat file1 file2 file3 file4 filen-1 filen should output a text file that concatenates the files file1 to filen into one file #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) {    char ch;    if (argc ==1)    {        while((ch = fgetc(stdin)) != EOF) fputc(ch, stdout);    }    if (argc == 2)    {   ...
Add code (see below for details) to the methods "set" and "get" in the following class,...
Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index] =...
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT