Question

In: Computer Science

Requirements:   You are to write a class called Point – this will represent a geometric point...

Requirements:   You are to write a class called Point – this will represent a geometric point in a Cartesian plane (but x and y should be ints).   Point should have the following:

Data:  

  • that hold the x-value and the y-value. They should be ints and must be private.

Constructors:

  • A default constructor that will set the values to (2,-7)
  • A parameterized constructor that will receive 2 ints (x then y) and set the data to what is received.
  • A copy constructor that will receive a Point. If it receives null, then it should

throw new IllegalArgumentException(<”your meaningful String here”>);

If it is OK, the it should initialize the data (of the new instance being created) to be the same as the Point that was received.

Methods:

  • The methods to be implemented are shown in the PointInterface.java. You can look at this file to see the requirements for the methods.
  • Your Point class should be defined like this:

public class Point implements PointInterface

When your Point.java compiles, Java will expect all methods in the interface to be implemented and will give a compiler error if they are missing. The compiler error will read “class Point is not abstract and does not implement method ”.

You can actually copy the PointInterface methods definitions into your Point class so it will have the comments and the method headers.   If you do this, be sure to take out the ; in the method headers or you will get a “missing method body” syntax error when compiling…

  • But a toString() method and a .equals(Object obj) method are automatically inherited from the Object class. So if you do not implement them, Java will find and use the inherited ones – and will not give a compiler error (but the inherited ones will give the wrong results). The Point class should have its own .toString and .equals methods.
  • ==============================================================================
  • This is the interface (called PointInterface.Java) for a Point class (which will represent a 2-dimensional Point)
  • public interface PointInterface

    {

    // toString

    // returns a String representing this instance in the form (x,y)

    (WITHOUT a space after the ,)

    public String toString();

    // distanceTo

    // throws a new IllegalArgumentException(

    if null is received

    // returns the distance from this Point to the Point that was

    received

    // NOTE: there is a static method in the Math class called hypot

    can be useful for this method

    public double distanceTo(Point otherPoint);

    //equals - returns true if it is equal to what is received (as an Object)

    public boolean equals(Object obj);

    // inQuadrant

    // returns true if this Point is in the quadrant specified

    // throws a new IllegalArgumentException if the quadrant is

    out of range (not 1-4)

    public boolean inQuadrant(int quadrant);

    // translate

    // changes this Point's x and y value by the what is received (thus

    "translating" it)

    // returns nothing

    public void translate(int xMove, int yMove);

    // onXAxis

    // returns true if this Point is on the x-axis

    public boolean onXAxis();

    // onYAxis

    // returns true if this Point is to the on the y-axis

    public boolean onYAxis();

    //=============================================

    // The method definitions below are commented out and

    // do NOT have to be implemented

    // //===========================================

    // halfwayTo

    // throws a new IllegalArgumentException(

    if null is received

    // returns a new Point which is halfway to the Point that is

    received

    //public Point halfwayTo(Point another);

    // slopeTo

    // throws a new IllegalArgumentException(

    if null is received

    // returns the slope between this Point and the one that is

    received.

    // since the slope is (changeInY/changeInX), then first check to see if

    changeInX is 0

    // if so, then return Double.POSITIVE_INFINITY; (since the

    denominator is 0)

    //public double slopeTo(Point anotherPoint)

    }

Solutions

Expert Solution

/****************************Point.java*****************************/


public class Point implements PointInterface {

   private int x;
   private int y;

   public Point() {

       this.x = 2;
       this.y = -7;
   }

   public Point(int x, int y) {
       super();
       this.x = x;
       this.y = y;
   }

   public Point(Point p) throws InvalidArgumentException {

       if (p == null) {

           throw new InvalidArgumentException("Null pointer exception!");
       } else {

           this.x = p.x;
           this.y = p.y;
       }
   }

   @Override
   public double distanceTo(Point otherPoint) {

       if (this == null || otherPoint == null) {

           throw new IllegalArgumentException();
       }
       return Math.sqrt(Math.pow((otherPoint.x - this.x), 2) + Math.pow((otherPoint.y - this.y), 2));
   }

   @Override
   public boolean inQuadrant(int quadrant) {

       if (quadrant==0||quadrant>4) {
                          
           throw new IllegalArgumentException();
       } else if (quadrant(this.x, this.y) == quadrant) {
           return true;
       }

       return false;
   }

   private static int quadrant(int x, int y) {
       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;
       }
   }

   @Override
   public void translate(int xMove, int yMove) {

       this.x = x + xMove;
       this.y = y + yMove;

   }

   @Override
   public boolean onXAxis() {

       if (this.y == 0) {

           return true;
       }
       return false;
   }

   @Override
   public boolean onYAxis() {

       if (this.x == 0) {

           return true;
       }
       return false;
   }

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

   @Override
   public boolean equals(Object arg0) {

       if (this == null || (Point) arg0 == null) {
           throw new IllegalArgumentException("");
       }
       if (this.x == ((Point) arg0).x && this.y == ((Point) arg0).y) {

           return true;
       } else {

           return false;
       }
   }

}

/*********************output*******************/

testing all

-----------------------------

==>Testing parameterized constructor/toString(): passing in -1 and -3
Point [x=-1, y=-3]

==>Testing parameterized constructor/toString(): passing in -1 and 4
Point [x=-1, y=4]

==>Testing parameterized constructor/toString(): passing in 0 and -3
Point [x=0, y=-3]

==>Testing parameterized constructor/toString(): passing in 0 and 4
Point [x=0, y=4]

==>Testing parameterized constructor/toString(): passing in 6 and -3
Point [x=6, y=-3]

==>Testing parameterized constructor/toString(): passing in 6 and 4
Point [x=6, y=4]

-----------------------------

==>Testing default constructor/toString()
Point [x=2, y=-7]

-----------------------------

==>Testing copy constructor (translate must work), passing in null
InvalidArgumentException

==>Testing copy constructor (translate must work), passing in Point [x=5, y=6]
Point that was created is: Point [x=5, y=6]

-----------------------------

==>Testing Point [x=-3, y=4] 's .translate(0, 0)
Point [x=-3, y=4]

==>Testing Point [x=-3, y=4] 's .translate(0, 7)
Point [x=-3, y=11]

==>Testing Point [x=-3, y=4] 's .translate(5, 0)
Point [x=2, y=4]

==>Testing Point [x=-3, y=4] 's .translate(5, 7)
Point [x=2, y=11]

-----------------------------

==>Testing Point [x=0, y=0] 's .inQuadrant(0)
true

==>Testing Point [x=0, y=6] 's .inQuadrant(0)
true

==>Testing Point [x=0, y=-2] 's .inQuadrant(0)
true

==>Testing Point [x=-3, y=0] 's .inQuadrant(0)
true

==>Testing Point [x=-3, y=6] 's .inQuadrant(0)
false

==>Testing Point [x=-3, y=-2] 's .inQuadrant(0)
false

==>Testing Point [x=5, y=0] 's .inQuadrant(0)
true

==>Testing Point [x=5, y=6] 's .inQuadrant(0)
false

==>Testing Point [x=5, y=-2] 's .inQuadrant(0)
false

==>Testing Point [x=0, y=0] 's .inQuadrant(1)
false

==>Testing Point [x=0, y=6] 's .inQuadrant(1)
false

==>Testing Point [x=0, y=-2] 's .inQuadrant(1)
false

==>Testing Point [x=-3, y=0] 's .inQuadrant(1)
false

==>Testing Point [x=-3, y=6] 's .inQuadrant(1)
false

==>Testing Point [x=-3, y=-2] 's .inQuadrant(1)
false

==>Testing Point [x=5, y=0] 's .inQuadrant(1)
false

==>Testing Point [x=5, y=6] 's .inQuadrant(1)
true

==>Testing Point [x=5, y=-2] 's .inQuadrant(1)
false

==>Testing Point [x=0, y=0] 's .inQuadrant(2)
false

==>Testing Point [x=0, y=6] 's .inQuadrant(2)
false

==>Testing Point [x=0, y=-2] 's .inQuadrant(2)
false

==>Testing Point [x=-3, y=0] 's .inQuadrant(2)
false

==>Testing Point [x=-3, y=6] 's .inQuadrant(2)
true

==>Testing Point [x=-3, y=-2] 's .inQuadrant(2)
false

==>Testing Point [x=5, y=0] 's .inQuadrant(2)
false

==>Testing Point [x=5, y=6] 's .inQuadrant(2)
false

==>Testing Point [x=5, y=-2] 's .inQuadrant(2)
false

==>Testing Point [x=0, y=0] 's .inQuadrant(3)
false

==>Testing Point [x=0, y=6] 's .inQuadrant(3)
false

==>Testing Point [x=0, y=-2] 's .inQuadrant(3)
false

==>Testing Point [x=-3, y=0] 's .inQuadrant(3)
false

==>Testing Point [x=-3, y=6] 's .inQuadrant(3)
false

==>Testing Point [x=-3, y=-2] 's .inQuadrant(3)
true

==>Testing Point [x=5, y=0] 's .inQuadrant(3)
false

==>Testing Point [x=5, y=6] 's .inQuadrant(3)
false

==>Testing Point [x=5, y=-2] 's .inQuadrant(3)
false

==>Testing Point [x=0, y=0] 's .inQuadrant(4)
false

==>Testing Point [x=0, y=6] 's .inQuadrant(4)
false

==>Testing Point [x=0, y=-2] 's .inQuadrant(4)
false

==>Testing Point [x=-3, y=0] 's .inQuadrant(4)
false

==>Testing Point [x=-3, y=6] 's .inQuadrant(4)
false

==>Testing Point [x=-3, y=-2] 's .inQuadrant(4)
false

==>Testing Point [x=5, y=0] 's .inQuadrant(4)
false

==>Testing Point [x=5, y=6] 's .inQuadrant(4)
false

==>Testing Point [x=5, y=-2] 's .inQuadrant(4)
true

==>Testing Point [x=0, y=0] 's .inQuadrant(8)
false

==>Testing Point [x=0, y=6] 's .inQuadrant(8)
false

==>Testing Point [x=0, y=-2] 's .inQuadrant(8)
false

==>Testing Point [x=-3, y=0] 's .inQuadrant(8)
false

==>Testing Point [x=-3, y=6] 's .inQuadrant(8)
false

==>Testing Point [x=-3, y=-2] 's .inQuadrant(8)
false

==>Testing Point [x=5, y=0] 's .inQuadrant(8)
false

==>Testing Point [x=5, y=6] 's .inQuadrant(8)
false

==>Testing Point [x=5, y=-2] 's .inQuadrant(8)
false

-----------------------------

==>Testing whether Point [x=4, y=-2] .equals null
java.lang.IllegalArgumentException

==>Testing whether Point [x=4, y=-2] .equals Point [x=4, y=-2] (as a STRING)
java.lang.ClassCastException

==>Testing whether Point [x=4, y=-2] .equals Point [x=4, y=1]
false

==>Testing whether Point [x=4, y=-2] .equals Point [x=9, y=-2]
false

==>Testing whether Point [x=4, y=-2] .equals Point [x=11, y=0]
false

==>Testing whether Point [x=4, y=-2] .equals Point [x=4, y=-2]
true

==>Testing whether Point [x=4, y=-2] .equals Point [x=-2, y=4]
false

-----------------------------

==>Testing Point [x=6, y=9] 's .distanceTo null
java.lang.IllegalArgumentException

==>Testing Point [x=-1, y=5] 's .distanceTo Point [x=-1, y=5]
0

==>Testing Point [x=-1, y=5] 's .distanceTo Point [x=-1, y=2]
3

==>Testing Point [x=-1, y=5] 's .distanceTo Point [x=5, y=5]
6

==>Testing Point [x=-1, y=5] 's .distanceTo Point [x=5, y=2]
6.708

==>Testing Point [x=-1, y=2] 's .distanceTo Point [x=-1, y=5]
3

==>Testing Point [x=-1, y=2] 's .distanceTo Point [x=-1, y=2]
0

==>Testing Point [x=-1, y=2] 's .distanceTo Point [x=5, y=5]
6.708

==>Testing Point [x=-1, y=2] 's .distanceTo Point [x=5, y=2]
6

==>Testing Point [x=5, y=5] 's .distanceTo Point [x=-1, y=5]
6

==>Testing Point [x=5, y=5] 's .distanceTo Point [x=-1, y=2]
6.708

==>Testing Point [x=5, y=5] 's .distanceTo Point [x=5, y=5]
0

==>Testing Point [x=5, y=5] 's .distanceTo Point [x=5, y=2]
3

==>Testing Point [x=5, y=2] 's .distanceTo Point [x=-1, y=5]
6.708

==>Testing Point [x=5, y=2] 's .distanceTo Point [x=-1, y=2]
6

==>Testing Point [x=5, y=2] 's .distanceTo Point [x=5, y=5]
3

==>Testing Point [x=5, y=2] 's .distanceTo Point [x=5, y=2]
0

Please let me know if you have any doubt or modify the answer, Thanks :)

2 Console X <terminated Point TesterHG [Java Application) C:\Program Files Java jre1.8.0_211\bin\javaw.exe (Sep 24, 2019, 7:11:18 AM) testing all and 3 ==>Testing parameterized constructor/toString(): passing in -1 Point [x=-1, y=-3] ==>Testing parameterized constructor/toString(): passing in -1 Point (x=-1, y=4] and 4 ==>Testing parameterized constructor/toString(): passing in Point (x=0, y=-3] and -3 and 4 ==> Testing parameterized constructor/toString(): passing in Point (x=0, y=4] ==> Testing parameterized constructor/toString(): passing in 6 and -3, Point (x=6, y=-3] ==>Testing parameterized constructor/toString(): passing in 6 and 4 Point [x=6, y=4] --------- ==>Testing default constructor/toString() Point (x=2, y=-7] ==>Testing copy constructor (translate must work), passing in null InvalidArgumentException ==> Testing copy constructor (translate must work), passing in Point [x=5, y=6] Point that was created is: Point [x=5, y=6] ==> Testing Point [x=-3, y=4] 's .translate(0, 0) Point [x=-3, y=4] ==>Testing Point [x=-3, y=4] 's translate(0, 7) Point (x=-3, y=11] ==>Testing Point (=-3, y=4] 's .translate(5, 0) Point [x=2, y=4]


Related Solutions

Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
The assignment is to write a class called data. A Date object is intented to represent...
The assignment is to write a class called data. A Date object is intented to represent a particuar date's month, day and year. It should be represented as an int. -- write a method called earlier_date. The method should return True or False, depending on whether or not one date is earlier than another. Keep in mind that a method is called using the "dot" syntax. Therefore, assuming that d1 and d2 are Date objects, a valid method called to...
1. Write a class called Rectangle that maintains two attributes to represent the length and width...
1. Write a class called Rectangle that maintains two attributes to represent the length and width of a rectangle. Provide suitable get and set methods plus two methods that return the perimeter and area of the rectangle. Include two constructors for this class. One a parameterless constructor that initializes both the length and width to 0, and the second one that takes two parameters to initialize the length and width. 2. Write a java program (a driver application) that tests...
Write the definition of a Point class method called clone that takes NO arguments and returns...
Write the definition of a Point class method called clone that takes NO arguments and returns a new Point whose x and y coordinates are the same as the Point’s coordinates.
Write a Python class definition called CellPhone to represent a monthly cell phone bill. The bill...
Write a Python class definition called CellPhone to represent a monthly cell phone bill. The bill should contain the following information: customer name (default value is the empty string) account number (default value is 0) number of gigabytes (GB) used over the monthly limit (default value is 0) Include the following methods: a constructor which given a customer name, account number, and number of GB used in excess of the monthly limit, creates a CellPhone object (be sure to account...
Write a python program using the following requirements: Create a class called Sentence which has a...
Write a python program using the following requirements: Create a class called Sentence which has a constructor that takes a sentence string as input. The default value for the constructor should be an empty string The sentence must be a private attribute in the class contains the following class methods: get_all_words — Returns all the words in the sentence as a list get_word — Returns only the word at a particular index in the sentence Arguments: index set_word — Changes...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in last lab). A MedicalStaff has specialty (i.e. Orthopedic, cardiology, etc.). 2- Then write two classes: Doctor class has office visit fee. Nurse class has title (i.e. RN, NP, etc.) Both classes inherit from MedicalStaff. Be sure these are all complete classes, including toString method. 3- Write a tester to test these classes and their methods, by creating an array or ArrayList of Person and...
3.44444444 The following are requirements of ALL programs that you write from this point on! You...
3.44444444 The following are requirements of ALL programs that you write from this point on! You must write instructions to the screen to tell the user exactly what you want him/her to do. Your instructions must be very clear and descriptive, and should not contain any spelling or grammar errors. You MUST choose variable names that describe the contents of the variable. Unless I specifically tell you to do so, single character variable names will not be acceptable unless you...
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT