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
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
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
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, waitField 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 + "\n");
}
}
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
----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.
Screenshot
Program
Point.java
/**
* Create a class Point with 2 coordinates as attributes
* @author deept
*
*/
public class Point {
//Attributes
private int x;
private int y;
// Default constructor generate origin
public Point() {
x = 0;
y = 0;
}
//Getters
public int getX() {
return x;
}
public int getY() {
return y;
}
// point initialized from parameters
public Point(int x, int y) {
this.x = x;
this.y = y;
}
//Add 2 points and return new point
public Point add(Point other) {
return new
Point(x+other.x,y+other.y);
}
// Euclidean distance between this point and
other point
public double distance(Point other) {
double dx = this.x -
other.x;
double dy = this.y -
other.y;
return Math.sqrt(dx*dx +
dy*dy);
}
//Check 2 points coordinates are equal then
return true otherwise false
public boolean equals(Point other) {
return x==other.x &&
y==other.y;
}
//Create a Point as mirror image of point
public Point originReflection() {
return new Point(-1*x,-1*y);
}
// return a string representation of this
point
public String toString() {
return "(" + x + ", " +
y + ")";
}
//Return quardinate according to the x an y
coordnate placed
public int quadrant() {
if(x>0 && y>0)
{
return 1;
}
else if(x>0 && y<0)
{
return 2;
}
else if(x<0 && y<0)
{
return 3;
}
else if(x<0 && y>0)
{
return 4;
}
else {
return 0;
}
}
//Return a point as a mirror reflection of x
coordinate
public Point xReflection() {
return new Point(x,-1*y);
}
//Return a point as a mirror reflection of y coordinate
public Point yReflection() {
return new Point(-1*x,y);
}
//Use file for point creation
//here file read scanner as input
parameter
//Return new point
public static Point read(java.util.Scanner sc)
{
int x,y;
x=sc.nextInt();
y=sc.nextInt();
return new Point(x,y);
}
}
PointApp.java
/**
* Test class For Point class
*/
import java.io.*;
import java.util.*;
public class PointApp{
public static void main(String[] args) throws
Exception{
//For file read
Scanner scanner = new Scanner(new
File("points.txt"));
//Loop unti end of file
while(scanner.hasNextInt()){
Point
p1=Point.read(scanner);
//Read first
Point
int x1=
p1.getX();
int y1=
p1.getY();
//Read second
point
Point
p2=Point.read(scanner);
int x2=
p2.getX();
int y2=
p2.getY();
//Get
coordinates
int k =
p1.quadrant();
int l
=p2.quadrant();
int sum
=(p1.add(p2)).quadrant();
//Display points
and coordinates
System.out.println("p1: ("+x1+", " +y1+") (quadrant "+k+")"+" / p2:
("+x2+", " +y2+") (quadrant "+k+")");
System.out.println("p1+p2: ("+(x1+x2)+", "+(y1+y2)+")(quadrant
"+sum+")");
//Different checks
if(p1.xReflection().equals(p2))
System.out.println("p1 and p2 are reflections
across the x-axis");
if(p2.yReflection().equals(p2))
System.out.println("p1 and p2 are reflections
across the y-axis");
if(p1.originReflection().equals(p2))
System.out.println("p1 and p2 are reflections
through the origin");
if(!p1.equals(p2))
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 =p1.distance(p2);
System.out.println("The distance between ("+x1+", " +y1+") and
("+x2+", " +y2+") is "+d + "\n");
}
}
}
-------------------------------------------------
Output
p1: (0, 0) (quadrant 0) / p2: (1, 1) (quadrant 0)
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 0)
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 1)
p1+p2: (0, 2)(quadrant 0)
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 1)
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: (1, 1) (quadrant 1) / p2: (1, 1) (quadrant 1)
p1+p2: (2, 2)(quadrant 1)
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
INSTRUCTIONS: I NEED TO CREATE A PointApp PROGRAM THAT USES THE FOLLOWING API DOCUMENTATION. Below the...
JAVA - Please follow ALL instructions, thank you!! Here is the API, for a Point class representing a 2-dimensional point. https://docs.oracle.com/javase/7/docs/api/java/awt/Point.html Code an application class: 1.) (i.e., a class containing a main method), 2.) named PointApp that reads point data from the file points.text. This data is then used to create pairs of Point objects which are then used to flex (i.e, illustrate) the methods of the class. The format of the points.text file is: x1 y1 x2 y2 …...
Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...
This is my code that i need to finish. In BoxRegion.java I have no idea how to create the constructor. I tried to use super(x,y) but It is hard to apply. And Also In BoxRegionHashTable, I don't know how to create displayAnnotation BoxRegion.java ------------------------------------------------ public final class BoxRegion { final Point2D p1; final Point2D p2; /** * Create a new 3D point with given x, y and z values * * @param x1, y1 are the x,y coordinates for point...
The output should be "The distance is 0" (The expected). ==================== YOUR OUTPUT ===================== 0001: Enter~the~x~coordinate~for~point~1:~0 0002: Enter~the~y~coordinate~for~point~1:~0 0003: Enter~the~x~coordinate~for~point~2:~0 0004: Enter~the~y~coordinate~for~point~2:~0 0005: The~distance~is~-nan =================== MISMATCH FOUND ON LINE 0005: =================== ACTUAL : The~distance~is~-nan EXPECTED: The~distance~is~0 ====================================================== #include <iostream> #include <iomanip> #include <string> using namespace std; float squareRoot(float s) { float xn; xn = s / 2.0; int counter = 1; while (counter <= 10) { xn = (xn + (s/xn))/2.0; counter = counter + 1; } return xn; } int...
It is done in python, can you please explain what does the part of the code in bold do? print("Point should be in format (x,y)") p1 = input("Enter point 1: ") p2 = input("Enter point 2: ") x1 = int(f(p1).split(",")[0]) y1 = int(f(p1).split(",")[1]) x2 = int(f(p2).split(",")[0]) y2 = int(f(p2).split(",")[1]) slope = (y2-y1)/(x2-x1) print("Slope =",slope)
Implement the Point class (code for this up to the isHigher method was discussed in the lecture). The class has the following instance variables: • x coordinate (an int) • y coordinate (an int) and the following methods: • Constructor that sets the x and y coordinates • Get and set methods • Method equals if this point is equal to another point object (if the x and y coordinates are the same). • Method isHigher if this point is...
C++ code for CIS054 Create a program that uses a function to determine the length of a line by inputting the X,Y coordinates of the line endpoints. Show the result with a precision of four decimal places. The function prototype is to be specified as follows: double LengthOfLine (double X1, double Y1, double X2, double Y2); // returns length of line by using this code: #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main(int argc, char* argv[])...
Create a UML diagram with 3 lines per class/interface including all constructors. public class Point { public double X, Y; public Point() { this(0, 0); } public Point(double newX, double newY) { X = newX; Y = newY; } public static double distance(Point A, Point B) { return Math.sqrt(Math.pow(A.X-B.X, 2) + Math.pow(A.Y-B.Y, 2)); } } public interface Polygon { public int getNumberOfSides(); public double getPerimeter(); public double getArea(); } public abstract class Simple_polygon implements Polygon{ public Point...
// ====== FILE: Point.java ========= // package hw3; /** * A class to that models a 2D point. */ public class Point { private double x; private double y; /** * Construct the point (<code>x</code>, <code>y</code>). * @param x the <code>Point</code>'s x coordinate * @param y the <code>Point</code>'s y coordinate */ public Point(double x, double y) { this.x = x; this.y = y; } /** * Move the point to (<code>newX</code>, <code>newY</code>). * @param newX the new x coordinate for...
Long Question Q1: 25 points (1O points for part a, 15 points for part b). riate JavaSeript code to deline a Point class Representing a) Provide the oints in 3 dimensions with an s, y, and z coordinate. A Point object should have the following mher), Returns the Euelidean distance to another instance of Point passed in as the points rameter other : Returns a new Point object with the same x. y, and z coordinates Additionally, your code should...