Question

Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

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 point: retgre

Not an integer! Try again! Enter the x-coordinate for first point: 89.67

Not an integer! Try again! Enter the x-coordinate for first point: -13

ERROR! Should be positive. Enter the x-coordinate for first point: 15

Enter the y-coordinate for first point: fwgfe

Not an integer! Try again! Enter the y-coordinate for first point: 90.6

Not an integer! Try again! Enter the y-coordinate for first point: -32

ERROR! Should be positive. Enter the y-coordinate for first point: b

Not an integer! Try again! Enter the y-coordinate for first point: 23

First point (after call to set) is (15, 23)

Distance from origin for first point =  27.46

Distance from origin for second point =  14.76

Distance between first point and second point =  12.81

First point (after call to translate (5, 10)) is (20, 33)

Second point (after call to translate (15, 5)) is (22, 18)

---Call to equals: The 2 points are NOT equal.

---Calls to copy and print---

First point (after call to copy) is (20, 33)

Second point (after call to copy) is (20, 33)

---Call to equals after call to copy: The 2 points are equal.

Here is my class point

public class Point {
  
int x, y;
  
//Default Constructor
public Point() {
  
}
  
//Parameter Constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
  
//getter function for x
public int getX() {
return x;
}

//getter function for y
public int getY() {
return y;
}
  
//A method named set to set the coordinates to the parameters passed; invalid values set to 0
public void set(int x, int y) {
this.x = x;
this.y = y;
}
  
//A method named print to print each Point object as (x, y)
public void print(Point p) {
System.out.println( toString(p) );
}
  
// method toString()
public String toString(Point p) {
return "( " + p.getX() + " , " + p.getY() + " )";
}

//method named equals to compare 2 Point objects for equality
public boolean equals(Point p1, Point p2) {
if(p1.x == p2.x && p1.y == p2.y) {
return true;
}
else {
return false;
}
}

//2 methods named copy and getCopy to make a copy of a Point object into another Point object
public Point copy(Point p) {
Point copyPoint = new Point();
copyPoint.x = p.getX();
copyPoint.y = p.getY();
return copyPoint;
}
  
public Point getCopy(Point p) {
return copy(p);
}
  
  
//method named distanceFromOrigin to calculate the distance between a point and the origin at(0, 0)
public double distanceFromOrigin(Point p) {
//Using Euclidian Distance Formula d = sqrt( x^2 + y^2 )
  
double d = Math.sqrt( (p.getX() * p.getX()) + (p.getY() * p.getY()) );
return d;
}
  
// method named distance to calculate the distance from a point to a given point.
public double distance(Point p1, Point p2) {
//Using Euclidian Distance Formula d = sqrt( (x1^2 - x2^2) + (y1^2 - y2^2) )
  
double d = Math.sqrt( Math.abs((Math.pow(p1.getX(),2) - Math.pow(p2.getX(),2))) + Math.abs((Math.pow(p1.getY(),2) - Math.pow(p2.getY(),2))));
return d;
}
  
//method named translate to shift the location of a point by a given amount.
public Point translate(Point p, int amount) {
p.set( p.getX() + amount, p.getY()+amount);
return p;
  
}
  
//method named isHorizontal that returns true if any given point lines up horizontally with a given point.
public boolean isHorizontal(Point p1, Point p2) {
//if y coordinate is same then they are horizontal
return (p1.getY() == p2.getY());
}
  
//method named isVertical that returns true if any given Point object lines up vertically with a given Point object.
public boolean isVertical(Point p1, Point p2) {
//if x coordinate is same then they are vertical
return (p1.getX() == p2.getX());
}
  
//method named slope that returns the slope of the line between this Point object and a given Point object.
public double slope(Point p1, Point p2) {
double sl = (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
return sl;
}
  
  
public static void main(String[] args) {
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go this code and let me know if u need any changes in this.Thank You
_________________

// Point.java

import java.text.DecimalFormat;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Point {

   int x, y;

   // Default Constructor
   public Point() {

   }

   // Parameter Constructor
   public Point(int x, int y) {
       this.x = x;
       this.y = y;
   }

   // getter function for x
   public int getX() {
       return x;
   }

   // getter function for y
   public int getY() {
       return y;
   }

   // A method named set to set the coordinates to the parameters passed;
   // invalid values set to 0
   public void set(int x, int y) {
       this.x = x;
       this.y = y;
   }

   // A method named print to print each Point object as (x, y)
   public void print(Point p) {
       System.out.println(toString(p));
   }

   // method toString()
   public String toString(Point p) {
       return "( " + p.getX() + " , " + p.getY() + " )";
   }

   // method named equals to compare 2 Point objects for equality
   public boolean equals(Point p1, Point p2) {
       if (p1.x == p2.x && p1.y == p2.y) {
           return true;
       } else {
           return false;
       }
   }

   // 2 methods named copy and getCopy to make a copy of a Point object into
   // another Point object
   public Point copy(Point p) {
       Point copyPoint = new Point();
       copyPoint.x = p.getX();
       copyPoint.y = p.getY();
       return copyPoint;
   }

   public Point getCopy(Point p) {
       return copy(p);
   }

   // method named distanceFromOrigin to calculate the distance between a point
   // and the origin at(0, 0)
   public double distanceFromOrigin(Point p) {
       // Using Euclidian Distance Formula d = sqrt( x^2 + y^2 )

       double d = Math.sqrt((p.getX() * p.getX()) + (p.getY() * p.getY()));
       return d;
   }

   // method named distance to calculate the distance from a point to a given
   // point.
   public double distance(Point p1, Point p2) {
       // Using Euclidian Distance Formula d = sqrt( (x1^2 - x2^2) + (y1^2 -
       // y2^2) )

       double d = Math.sqrt(Math.abs((Math.pow(p1.getX(), 2) - Math.pow(
               p2.getX(), 2)))
               + Math.abs((Math.pow(p1.getY(), 2) - Math.pow(p2.getY(), 2))));
       return d;
   }

   // method named translate to shift the location of a point by a given
   // amount.
   public Point translate(Point p, int amount) {
       p.set(p.getX() + amount, p.getY() + amount);
       return p;

   }

   // method named isHorizontal that returns true if any given point lines up
   // horizontally with a given point.
   public boolean isHorizontal(Point p1, Point p2) {
       // if y coordinate is same then they are horizontal
       return (p1.getY() == p2.getY());
   }

   // method named isVertical that returns true if any given Point object lines
   // up vertically with a given Point object.
   public boolean isVertical(Point p1, Point p2) {
       // if x coordinate is same then they are vertical
       return (p1.getX() == p2.getX());
   }

   // method named slope that returns the slope of the line between this Point
   // object and a given Point object.
   public double slope(Point p1, Point p2) {
       double sl = (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
       return sl;
   }

   @Override
   public String toString() {
       return "(" + x + ", " + y + ")";
   }

   /*
   * Creating an Scanner class object which is used to get the inputs
   * entered by the user
   */
   static Scanner sc = new Scanner(System.in);
   public static void main(String[] args) {
      

       // DecimalFormat class is used to format the output
       DecimalFormat df = new DecimalFormat(".00");
      
       int x, y;
       Point p1 = new Point();
       Point p2 = new Point(7, 13);
       Point p3 = new Point(7, 15);
       System.out.println("---After declaration, constructors invoked--- Using toString():");
       System.out.println("First point is " + p1);
       System.out.println("Second point is " + p2);
       System.out.println("Third point is " + p3);
       boolean b = p2.isVertical(p2, p3);
       if (b)
           System.out.println("Second point " + p2
                   + " lines up vertically with third point " + p3);
       else
           System.out.println("Second point " + p2
                   + " does'nt lines up vertically with third point " + p3);

       b = p2.isHorizontal(p2, p3);
       if (b)
           System.out.println("Second point " + p2
                   + " lines up horizontally with third point " + p3);
       else
           System.out.println("Second point " + p2
                   + " does'nt lines up horizontally with third point " + p3);
      
       String str1="Enter the x-coordinate for first point:";
       String str2="Enter the y-coordinate for first point:";
      
  
       x=getValidCoordinate(str1);
       y=getValidCoordinate(str2);
       p1.set(x, y);
       System.out.println("First point (after call to set) is "+p1);
   System.out.println("Distance from origin for first point = "+df.format(p1.distanceFromOrigin(p1)));
   System.out.println("Distance from origin for second point = "+df.format(p2.distanceFromOrigin(p2)));
   System.out.println("Distance between first point and second point = "+df.format(p1.distance(p1, p2)));
     
   b=p1.equals(p1, p2);
   if(b)
   {
       System.out.println("---Call to equals: The 2 points are equal.");
   }
   else
   {
       System.out.println("---Call to equals: The 2 points are NOT equal.");
   }
  
   Point p4=new Point(5, 10);

   System.out.println("---Calls to copy and print---");
   p1=p1.copy(p1);
   System.out.println("First point (after call to copy) is "+p1);
   p2=p2.copy(p1);
   System.out.println("Second point (after call to copy) is "+p2);
   b=p1.equals(p1, p2);
   if(b)
   {
       System.out.println("---Call to equals: The 2 points are equal.");
   }
   else
   {
       System.out.println("---Call to equals: The 2 points are NOT equal.");
   }  
  

  
  

   }

   private static int getValidCoordinate(String str) {
       int val;
       while (true) {
           try {
               System.out.print(str);
               val = sc.nextInt();
               if(val<0)
               {
                   System.out.print("ERROR! Should be positive. ");
                   continue;
               }
               break;
           } catch (InputMismatchException e) {
               sc.nextLine();
               System.out.print("Not an integer! Try again! ");
               continue;
           }
       }
       return val;
   }

}

__________________________

Output:

---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) does'nt lines up horizontally with third point (7, 15)
Enter the x-coordinate for first point:retgre
Not an integer! Try again! Enter the x-coordinate for first point:89.67
Not an integer! Try again! Enter the x-coordinate for first point:-13
ERROR! Should be positive. Enter the x-coordinate for first point:15
Enter the y-coordinate for first point:fwgfe
Not an integer! Try again! Enter the y-coordinate for first point:90.6
Not an integer! Try again! Enter the y-coordinate for first point:-32
ERROR! Should be positive. Enter the y-coordinate for first point:b
Not an integer! Try again! Enter the y-coordinate for first point:23
First point (after call to set) is (15, 23)
Distance from origin for first point = 27.46
Distance from origin for second point = 14.76
Distance between first point and second point = 23.15
---Call to equals: The 2 points are NOT equal.
---Calls to copy and print---
First point (after call to copy) is (15, 23)
Second point (after call to copy) is (15, 23)
---Call to equals: The 2 points are equal.

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Java Help 2. Task: Create a client for the Point class. Be very thorough with your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Python 3: Create a point p1 of coordinates (0; 0) and un point p2 of...

    Using Python 3: Create a point p1 of coordinates (0; 0) and un point p2 of coordinates (1; 2). Print out the coordinates of the two points on the same line, by calling toString on the two points. Print the result of applying the method equals on point p1, using p2 as argument. Set the x coordinate of p2 equal to the x coordinate of p1, using the methods setX and getX. Set the y coordinate of p2 equal to...

  • INSTRUCTIONS: I NEED TO CREATE A PointApp PROGRAM THAT USES THE FOLLOWING API DOCUMENTATION. Below 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...

  • In Java please! Problem You will be using the point class discussed in class to create...

    In Java please! Problem You will be using the point class discussed in class to create a Rectangle class. You also need to have a driver class that tests your rectangle. Requirements You must implement the 3 classes in the class diagram below and use the same naming and data types. Following is a description of what each method does. Point Rectangle RectangleDriver - x: int - top Left: Point + main(args: String[) - y: int - width: int +...

  • In Pl you will create an interface, Drawable, and an abstract class Polygon. The Point class...

    In Pl you will create an interface, Drawable, and an abstract class Polygon. The Point class is provided. Briefly go over the JavaDocs comments to see what is available to you. «interface Drawable +printToConsole(): void Polygon - points : List<Point> - colour : String Point + Polygon(String colour) + getPoints(): List<Point> + addPoint(Point p): void + equals(Object other): boolean +getArea(): double • The Polygon constructor initializes the List to a List collection of your choosing • add Point: Adds a...

  • public class Point f private int x; private int y; public Point(int x, int y) this.x...

    public class Point f private int x; private int y; public Point(int x, int y) this.x X; this.y y; public int getX() return x; public int getY() return Y: public double distance (Point other) double dx this.x-other.x; double dy this.y-other.y double dist Math.sqrt(dx dx + dy dy); return dist;

  • For this question you must write a java class called Rectangle and a client class called...

    For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of this assignment.) // A Rectangle stores an (x, y) coordinate...

  • What is wrong with the following code: class Point { private : int x, y; public...

    What is wrong with the following code: class Point { private : int x, y; public : Point (int u, int v) : x(u), y(v) {} int getX () { return x; } int getY () { return y; } void setX (int newX ) const { x = newX ; } }; int main () { Point p(5, 3); p.setX (9001) ; cout << p. getX () << ’ ’ << p. getY (); return 0; }

  • // ====== FILE: Point.java ========= // package hw3; /** * A class to that models a...

    // ====== 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...

  • Java homework Imagine that you have a Point class, like the one that we used in...

    Java homework Imagine that you have a Point class, like the one that we used in the improved Circle class. It has the following constructors:    Point(double x, double y)    Point(Point p) And the following methods:    public double getX()    public double getY()    public void setX(double xValue)    public void setY(double yValue)    public double distance(Point p) For this question, write statements that create an array of three Point references. Then initialize each element with a Point object that has with x and...

  • Must be in Java. Show proper reasoning with step by step process. Comment on the code...

    Must be in Java. Show proper reasoning with step by step process. Comment on the code please and show proper indentation. Use simplicity. Must match the exact Output. CODE GIVEN: LineSegment.java LineSegmentTest.java Point.java public class Point { private double x, y; // x and y coordinates of point /** * Creates an instance of Point with the provided coordinates * @param inX the x coordinate * @param inY the y coordinate */ public Point (double inX, double inY) { this.x...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT