Question

Follow program instructions carefully. spacing is important. I think It needs a static void main too....

Follow program instructions carefully. spacing is important. I think It needs a static void main too. Need to run it!

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)
}

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

If you have any doubts, please give me comment...

PointInterface.java

//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)

}

Point.java

public class Point implements PointInterface {

    private int x;

    private int y;

    public Point(){

        x = 2;

        y = -7;

    }

    public Point(int x, int y){

        this.x = x;

        this.y = y;

    }

    public Point(Point pt){

        if(pt==null)

            throw new IllegalArgumentException("Null pointer exception occured");

        x = pt.x;

        y = pt.y;

    }

    public String toString() {

        return "(" + x + ", " + y + ")";

    }

    // 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) {

        if (otherPoint == null)

            throw new IllegalArgumentException("Point can't be null");

        return Math.hypot(Math.abs(otherPoint.y - y), Math.abs(otherPoint.x - x));

    }

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

    public boolean equals(Object obj) {

        Point pt = (Point) obj;

        return pt.x == x && pt.y == y;

    }

    // 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) {

        if (quadrant < 1 || quadrant > 4)

            throw new IllegalArgumentException("Quadrant is out of range");

        if ((x > 0 && y > 0 && quadrant == 1) || (x < 0 && y > 0 && quadrant == 2) || (x < 0 && y < 0 && quadrant == 3)

                || (x > 0 && y < 0 && quadrant == 4))

            return true;

        return false;

    }

    // 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) {

        x += xMove;

        y += yMove;

    }

    // onXAxis

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

    public boolean onXAxis() {

        return y == 0;

    }

    // onYAxis

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

    public boolean onYAxis() {

        return x == 0;

    }

}

PointTest.java

public class PointTest{

    public static void main(String[] args) {

        Point pt1 = new Point();

        Point pt2 = new Point(3,5);

        System.out.println("Point-1: "+pt1);

        System.out.println("Point-2: "+pt2);

        System.out.println("Distance between Point-1 and Point-2 is: "+pt1.distanceTo(pt2));

        System.out.println("is Point-1 equals to Point-2: "+pt1.equals(pt2));

        System.out.println("is Point-1 in first Quadrant: "+pt1.inQuadrant(1));

        pt1.translate(3,7);

        System.out.println("After translating point-1 with (3,7): "+pt1);

        System.out.println("Is Point-1 on X-axis: "+pt1.onXAxis());

        System.out.println("Is Point-2 on Y-axis: "+pt1.onYAxis());

    }

}

Add a comment
Know the answer?
Add Answer to:
Follow program instructions carefully. spacing is important. I think It needs a static void main too....
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
  • You are to write a class called Point – this will represent a geometric point in...

    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 (3, -5)  A parameterized constructor that will receive 2 ints (x then y) and set the data to what is...

  • 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...

  • Write a program to pack boxes with blobs. Write two classes and a driver program. A...

    Write a program to pack boxes with blobs. Write two classes and a driver program. A Blob class implements the following interface. (Defines the following methods.) public interface BlobInterface { /** getWeight accessor method. @return the weight of the blob as a double */ public double getWeight(); /** toString method @return a String showing the weight of the Blob */ public String toString(); } A Blob must be between 1 and 4 pounds, with fractional weights allowed. A default constructor...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • Can you help with the merge method? This method should create and return an ArrayBagcontaining one...

    Can you help with the merge method? This method should create and return an ArrayBagcontaining one occurrence of any item that is found in either the called object or the parameter other. For full credit, the resulting bag should not include any duplicates. Give the new ArrayBag a maximum size that is the sum of the two bag’s maximum sizes. import java.util.*; /** * An implementation of a bag data structure using an array. */ public class ArrayBag { /**...

  • // Client application class and service class Create a NetBeans project named StudentClient following the instructions...

    // Client application class and service class Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans Project your application class StudentC lient should contain the following executable code: package studentclient; public class...

  • Create a simple Java class for a Month object with the following requirements:  This program...

    Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...

  • Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions...

    Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions Your task is to write a class called Car. Your class should have the following fields and methods: private int position private boolean headlightsOn public Car() - a constructor which initializes position to 0 and headlightsOn to false public Car(int position) - a constructor which initializes position to the passed in value and headlightsOn to false, and it should throw a NegativeNumberException with a...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

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