Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

INSTRUCTIONS: I NEED TO CREATE A PointApp PROGRAM THAT USES THE FOLLOWING API DOCUMENTATION. Below the API Documentation is the code I submitted. However, the

INSTRUCTIONS:

I NEED TO CREATE A PointApp PROGRAM THAT USES THE FOLLOWING API DOCUMENTATION. Below the API Documentation is the code I submitted. However, the output is different for what they are asking. I am looking for someone to fix the code to print out the correct output and to add comments so I can have an idea in how the code works. PLEASE AND THANK YOU.

API DOCUMENTATION:

  • public class Point extends java.lang.Object

    The Point class is used to create objects meant to represent points in a two-dimensional plane. Each point has an x-coordinate and a y-coordinate (ints). The class provides some methods for calculations relating to points.

    • Field Summary

      Fields
      Modifier and Type Field and Description
      static Point ORIGIN

      ORIGIN is a Point object representing the origin of the two- dimesional plane; in other words, ORIGIN represents (0, 0).

    • Constructor Summary

      Constructors
      Constructor and Description
      Point()

      A no-arg constructor.

      Point(int x, int y)

      Creates a new Point object; assigns the value of the first parameter to the x-coordinate and that of the second paramter to the y-coordinate.

    • Method Summary

      All MethodsStatic MethodsInstance MethodsConcrete Methods
      Modifier and Type Method and Description
      Point add(Point other)

      "Adds" two Point objects together.

      double distance(Point other)

      Computes and returns the distance between this Point and another Point.

      boolean equals(Point other)

      Determines whether two Point objects are "equal," that is, it determines whether they have the same x-coordinte and the same y-coordinate.

      Point originReflection()

      Returns a new Point object that represents the reflection of this Point across the origin (the center of the two-coordinate plane).

      int quadrant()

      Returns an int representing the quadrant (1, 2, 3, or 4) of the two-dimensional plane in which this Point lies.

      static Point read(java.util.Scanner sc)

      A static method that reads data from a file and creates and returns a new Point object based on that data.

      java.lang.String toString()

      Returns a String representing this Point object in the form "(x-coordinate, y-coordinate)".

      Point xReflection()

      Returns a new Point object that represents the reflection of this Point across the x-axis.

      Point yReflection()

      Returns a new Point object that represents the reflection of this Point across the y-axis.

      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • ORIGIN

        public static final Point ORIGIN

        ORIGIN is a Point object representing the origin of the two- dimesional plane; in other words, ORIGIN represents (0, 0).

    • Constructor Detail

      • Point

        public Point()

        A no-arg constructor. Creates a default Point object, which represents the point (0, 0).

      • Point

        public Point(int x, int y)

        Creates a new Point object; assigns the value of the first parameter to the x-coordinate and that of the second paramter to the y-coordinate.

        Parameters:

        x - The desired x-coordinate for this Point object

        y - The desired y-coordinate for this Point object

    • Method Detail

      • add

        public Point add(Point other)

        "Adds" two Point objects together. That is, it creates a new Point object that has: an x-coordinate that's the sum of this Point object's x-coordinate and other's x-coordinate, and a y-coordinate that's the sum of this Point object's y- coordinate and other's y-coordinate. Then it returns a reference to the new Point object. For example, if p1 represents (1, 1) and p2 represents (2, 1), and we say Point p3 = p1.add(p2);, p3 will represent (3, 2).

        Parameters:

        other - The Point object that we want to "add together with" this Point object

        Returns:

        A reference to a new Point object that's the result of "adding together" the two Point objects

      • distance

        public double distance(Point other)

        Computes and returns the distance between this Point and another Point.

        Parameters:

        other - The Point we want to find the distance to

        Returns:

        The distance (a double) from this Point to other

      • equals

        public boolean equals(Point other)

        Determines whether two Point objects are "equal," that is, it determines whether they have the same x-coordinte and the same y-coordinate. It compares this Point's x- and y- coordinates with those of other's (the parameter).

        Parameters:

        other - The Point object that we want to compare to this Point object

        Returns:

        True if the two Point objects are "equal," false if they are not

      • originReflection

        public Point originReflection()

        Returns a new Point object that represents the reflection of this Point across the origin (the center of the two-coordinate plane). For example, if p represents (2, 1), then p.originReflect() returns a new Point object representing (-2, -1).

        Returns:

        A new Point representing the reflection of the current Point across the origin

      • toString

        public java.lang.String toString()

        Returns a String representing this Point object in the form "(x-coordinate, y-coordinate)". For example, if the x- coordinate of this object is 3 and the y-coordinate of this object is 4, this method will return the string "(3, 4)".

        Overrides:

        toString in class java.lang.Object

        Returns:

        A String representation of this Point object

      • quadrant

        public int quadrant()

        Returns an int representing the quadrant (1, 2, 3, or 4) of the two-dimensional plane in which this Point lies. For example, if p represents (1, 3), which lies in the first quadrant, p.quadrant() will return 1. Note: if a point lies on the x-axis or the y-axis (or on both) it technically does not lie in a quadrant; in such a case, this method will return 0.

        Returns:

        An int (1, 2, 3, or 4) representing the quadrant in which this Point lies, or 0 if it's on the x-axis or y-axis (or on both)

      • xReflection

        public Point xReflection()

        Returns a new Point object that represents the reflection of this Point across the x-axis. For example, if p represents (2, 1), then p.xReflection() returns a new Point object representing (2, -1).

        Returns:

        A new Point representing the reflection of the current Point across the x-axis

      • yReflection

        public Point yReflection()

        Returns a new Point object that represents the reflection of this Point across the y-axis. For example, if p represents (2, 1), then p.yReflection() returns a new Point object representing (-2, 1).

        Returns:

        A new Point representing the reflection of the current Point across the y-axis

      • read

        public static Point read(java.util.Scanner sc)

        A static method that reads data from a file and creates and returns a new Point object based on that data. In detail, the method reads from a file associated with a Scanner (which it receives as a parameter. If there is no more data to be read from the file, the method returns null. Otherwise, it reads in two ints from the file and then creates a new Point object. The x-coordinate of the new object is the first int that was read from the file, and the y-coordinate is the second int that it read. Finally, the method returns a reference to the newly-created Point object.

        Parameters:

        sc - A Scanner object which should already be associated with a file; this method will read from that file using this Scanner

        Returns:

        A new Point object based on the data read from the file

    • THE CODE I SUBMITTED:

    • import java.io.*;

      import java.util.*;

      import java.lang.*;

      public class PointApp{

      public static void main(String[] args) throws Exception

      {

      Scanner scanner = new Scanner(new File("points.text"));

      int [] tall = new int [100];

      int i = 0;

      while(scanner.hasNextInt())

      {

      int x1= scanner.nextInt();

      int y1= scanner.nextInt();

      int x2= scanner.nextInt();

      int y2= scanner.nextInt();

      int k = cord(x1,y1);

      int l = cord(x2,y2);

      int sum = cord(x1+x2,y1+y2);

      System.out.println("p1: ("+x1+", " +y1+") (quadrant "+k+")"+" / p2: ("+x2+", " +y2+") (quadrant "+k+")");

      System.out.println("p1+p2: ("+(x1+x2)+", "+(y1+y2)+")(quadrant "+sum+")");

      if(x1==x2)

      System.out.println("p1 and p2 are reflections across the x-axis");

      if(y1==y2)

      System.out.println("p1 and p2 are reflections across the y-axis");

      if(x1==(-1*x2) && y1==(-1*y2))

      System.out.println("p1 and p2 are reflections through the origin");

      if(x1!=x2 && y1!=y2)

      if(Math.abs(x1-0)==Math.abs(x2-0) && Math.abs(x1-0)==Math.abs(x2-0))

      System.out.println("p1 and p2 are equidistant from the origin");

      double d = dist(x1,y1,x2,y2);

      System.out.println("The distance between ("+x1+", " +y1+") and ("+x2+", " +y2+") is "+d + " ");

      }

      }

      public static int cord(int x, int y){

      int k=0;

      if(x>=0){

      if(y>=0)

      return 1;

      else

      return 4;

      }

      if(x<0){

      if(y>=0)

      return 2;

      else

      return 3;

      }

      return -1;}

      public static double dist(int x1, int y1, int x2, int y2){

      double i = Math.pow(x2-x1,2);

      double j = Math.pow(y2-y1,2);

      double v = Math.sqrt((i+j));

      return v;

      }

      }

    • THE OUTPUT THAT I NEED:

    • p1: (0, 0) (quadrant 0) / p2: (1, 1) (quadrant 1) p1+p2: (1, 1) (quadrant 1) The distance between (0, 0) and (1, 1) is 1.4142135623730951 p1: (1, 1) (quadrant 1) / p2: (1, -1) (quadrant 4) p1+p2: (2, 0) (quadrant 4) p1 and p2 are reflections across the x-axis p1 and p2 are equidistant from the origin The distance between (1, 1) and (1, -1) is 2.0 p1: (1, 1) (quadrant 1) / p2: (-1, 1) (quadrant 2) p1+p2: (0, 2) (quadrant 0) p1 and p2 are reflections across the y-axis p1 and p2 are equidistant from the origin The distance between (1, 1) and (-1, 1) is 2.0 p1: (1, 1) (quadrant 1) / p2: (-1, -1) (quadrant 3) p1+p2: (0, 0) (quadrant 0) p1 and p2 are reflections through the origin p1 and p2 are equidistant from the origin The distance between (1, 1) and (-1, -1) is 2.8284271247461903 p1: (0, 0) (quadrant 0) / p2: (0, 0) (quadrant 0) p1+p2: (0, 0) (quadrant 0) p1 and p2 are reflections across the x-axis p1 and p2 are reflections across the y-axis p1 and p2 are reflections through the origin p1 and p2 are equidistant from the origin The distance between (0, 0) and (0, 0) is 0.0 p1: (1, 1) (quadrant 1) / p2: (1, 1) (quadrant 1) p1+p2: (2, 2) (quadrant 1) p1 and p2 are equidistant from the origin The distance between (1, 1) and (1, 1) is 0.0 p1: (1, 1) (quadrant 1) / p2: (-2, -2) (quadrant 3) p1+p2: (-1, -1) (quadrant 3) The distance between (1, 1) and (-2, -2) is 4.242640687119285
    • THE OUTPUT THAT I GET:
    • ----jGRASP exec: java PointApp2 p1: (0, 0) (quadrant 1) / p2: (1, 1) (quadrant 1) p1+p2: (1, 1)(quadrant 1) The distance between (0, 0) and (1, 1) is 1.4142135623730951

      p1: (1, 1) (quadrant 1) / p2: (1, -1) (quadrant 1) p1+p2: (2, 0)(quadrant 1) p1 and p2 are reflections across the x-axis The distance between (1, 1) and (1, -1) is 2.0

      p1: (1, 1) (quadrant 1) / p2: (-1, 1) (quadrant 1) p1+p2: (0, 2)(quadrant 1) p1 and p2 are reflections across the y-axis The distance between (1, 1) and (-1, 1) is 2.0

      p1: (1, 1) (quadrant 1) / p2: (-1, -1) (quadrant 1) p1+p2: (0, 0)(quadrant 1) p1 and p2 are reflections through the origin p1 and p2 are equidistant from the origin The distance between (1, 1) and (-1, -1) is 2.8284271247461903

      p1: (0, 0) (quadrant 1) / p2: (0, 0) (quadrant 1) p1+p2: (0, 0)(quadrant 1) p1 and p2 are reflections across the x-axis p1 and p2 are reflections across the y-axis p1 and p2 are reflections through the origin The distance between (0, 0) and (0, 0) is 0.0

      p1: (1, 1) (quadrant 1) / p2: (1, 1) (quadrant 1) p1+p2: (2, 2)(quadrant 1) p1 and p2 are reflections across the x-axis p1 and p2 are reflections across the y-axis The distance between (1, 1) and (1, 1) is 0.0

      p1: (1, 1) (quadrant 1) / p2: (-2, -2) (quadrant 1) p1+p2: (-1, -1)(quadrant 3) The distance between (1, 1) and (-2, -2) is 4.242640687119285

      ----jGRASP: operation complete. I APOLOGIZE FOR THE LONG QUESTION BUT GIVEN THAT A FEW PEOPLE ARE ASKING ABOUT THE CODE AND IT HAS THE WRONG OUTPUT I FIGURED TO BE SPECIFIC.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions