Question

In: Computer Science

my question hasnt been answered yet. So, posting it again. Follow program instructions carefully. spacing is...

my question hasnt been answered yet. So, posting it again.

Follow program instructions carefully. spacing is important.

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 <the missing 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.

Piece of the code:

//This is the interface 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(<your descriptive String> 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(<your descriptive String> 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(<your descriptive String> 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

Thanks for the question, here is the implemented class Point. 

As per the question the following 3 methods are commented so I didnt implement them - these are -

halfwayTo()
slopeTo()

remaining all method declared in PointInterface are implemented correctly.

Let me know for any changes or modification, do comment please.

============================================================================

public class Point implements PointInterface {

    int x;
    int y;

    //A default constructor that will set the values to (2,-7)
    public Point() {
        x = 2;
        y = -7;
    }

    //A parameterized constructor that will receive 2 ints (x then y) and set the data to what is received.
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    //A copy constructor that will receive a Point. If it receives null, then it should
    public Point(Point point) {
        if (point == null) {
            throw new IllegalArgumentException("Point cannot be null");
        }
        this.x = point.x;
        this.y = point.y;
    }

    @Override
    public double distanceTo(Point otherPoint) {
        if (otherPoint == null) {
            throw new IllegalArgumentException("Point cannot be null");
        }

        return Math.sqrt((x - otherPoint.x) * (x - otherPoint.x) + (y - otherPoint.y) * (y - otherPoint.y));
    }

    @Override
    public boolean inQuadrant(int quadrant) {

        if (quadrant == 1 && x > 0 && y > 0) return true;
        if (quadrant == 2 && x < 0 && y > 0) return true;
        if (quadrant == 3 && x < 0 && y < 0) return true;
        if (quadrant == 4 && x > 0 && y < 0) return true;
        return false;
    }

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

        x = xMove;
        y = yMove;
    }

    @Override
    public boolean onXAxis() {
        return y == 0;
    }

    @Override
    public boolean onYAxis() {
        return x == 0;
    }

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

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (obj instanceof Point) {
            Point aPoint = (Point) obj;
            return x == aPoint.x && y == aPoint.y;
        }
        return false;
    }
}

===================================================================

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(<your descriptive String> 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(<your descriptive String> 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(<your descriptive String> 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)
}

Related Solutions

Please Read Carefully Before start answering this question. Please follow the instructions. This Question is from...
Please Read Carefully Before start answering this question. Please follow the instructions. This Question is from 'BSBFIM501 Manage budgets and financial plans' course. There are no parts missing for this Question; guaranteed!. This is the original Screenshot direct from the question. Therefore, there are nothing any further information can be provided. Thanks for your understanding and Cooperation. Please answer the following questions from the topics discussed for Prepare, implement, monitor and modify contingency plans: 1.a. Explain the process of preparing...
Follow the instructions carefully. [20 marks] Question Sampling is the process of selecting a representative subset...
Follow the instructions carefully. [20 marks] Question Sampling is the process of selecting a representative subset of observations from a population to determine characteristics (i.e. the population parameters) of the random variable under study. Probability sampling includes all selection methods where the observations to be included in a sample have been selected on a purely random basis from the population. Briefly explain FIVE (5) types of probability sampling.
Follow the instructions carefully. [20 marks] Question Sampling is the process of selecting a representative subset...
Follow the instructions carefully. [20 marks] Question Sampling is the process of selecting a representative subset of observations from a population to determine characteristics (i.e. the population parameters) of the random variable under study. Probability sampling includes all selection methods where the observations to be included in a sample have been selected on a purely random basis from the population. Briefly explain FIVE (5) types of probability sampling.
This is very important. You must follow my instructions exactly. There are 9 questions. Each question...
This is very important. You must follow my instructions exactly. There are 9 questions. Each question requires you to answer TRUE or FALSE followed by an explanation. If you do not do this I will interpret your answer as I see it, so the word TRUE or FALSE is critical as a start to each question. The second important thing is to put each answer below the relevant question, not all at the end of the exam. So, you will...
Question 2 Not yet answered Marked out of 1.00 Flag question Question text __________ is the...
Question 2 Not yet answered Marked out of 1.00 Flag question Question text __________ is the return to investors divided by total assets. Select one: a. Return on investment b. Return on average assets c. Return on assets d. Return on risk Question 3 Not yet answered Marked out of 1.00 Flag question Question text Which form of payment causes a company to pay taxes twice on the same amount? Select one: a. Dividends b. Interest c. Stock splits d....
USING THE FOLLOWING DATABASE: [Please read the instructions thoroughly, this is my 6th time posting this...
USING THE FOLLOWING DATABASE: [Please read the instructions thoroughly, this is my 6th time posting this same question.] ========= -- SQL Script for Generating Kitchen Database drop database if exists kitchen; create database kitchen; use kitchen; drop table if exists recipe; drop table if exists food; create table food (fid int, fname varchar(45) not null unique, primary key(fid)); drop table if exists ingredient; create table ingredient (iid int, iname varchar(45) not null unique, caloriepergram float, category varchar(20), primary key(iid) );...
Question 1 Not yet answered Marked out of 1.00 Flag question Question text The function of...
Question 1 Not yet answered Marked out of 1.00 Flag question Question text The function of tRNA is to Select one: a. provide a site for polypeptide synthesis b. transport amino acids to the ribosome c. transcribe DNA d. transform DNA Question 2 Not yet answered Marked out of 1.00 Flag question Question text The lipid bi-layer is Select one: a. hydrophilic b. hydrophobic c. hydrophilic and hydrophobic d. depends on the surrounding medium Question 3 Not yet answered Marked...
Question 17 Not yet answered Marked out of 1.00 Flag question Question text The political and...
Question 17 Not yet answered Marked out of 1.00 Flag question Question text The political and military alliance by which the Macedonians united Greece under their own rule was known as Select one: a. the Macedonian League b. the Delian League c. the League of Corinth d. the National League Question 20 Not yet answered Marked out of 1.00 Flag question Question text Regardless of the specific form of government under which they lived, classical Greeks valued most highly Select...
Question 3 Not yet answered Marked out of 1.00 Flag question Question text Which of the...
Question 3 Not yet answered Marked out of 1.00 Flag question Question text Which of the following is the correct order of the steps in the accounting cycle? Select one: A. Report, analyze, close, record, and adjust B. Adjust, report, analyze, record, and close C. Record, report, analyze, adjust, and close D. Analyze, record, adjust, report, and close eBook Print Question 4 Not yet answered Marked out of 1.00 Flag question Question text Cash collected on accounts receivable would produce...
Question 1 Not yet answered Marked out of 2.00 Flag question Question text The Law of...
Question 1 Not yet answered Marked out of 2.00 Flag question Question text The Law of Demand states that a(n) Select one: Change in price will lead to no change in the quantity demanded Decrease in price will lead to a decrease in the quantity demanded Increase in price will lead to an increase in the quantity demanded Increase in price will lead to a decrease in the quantity demanded Clear my choice Question 2 Not yet answered Marked out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT