Question

Consider the following class Point which represents a point in Cartesian coordinate system. The class has...

Consider the following class Point which represents a point in Cartesian coordinate system. The class has two instance variables x and y which represent its position along the x-axis and y-axis. The constructor of the class initializes the x and y of the point with the specific value init; the method move() moves the point as specified by its parameter amount.

Using the implementation of the class Point information and write its specification. The specification of the class includes the specification of its methods. Each method specification contains the method's name, signature, informal English description, precondition, postcondition (return), throws list.

class Point{

   private int x;

   private int y;

   public Point(int init){

                        x = init;

                        y = init;

   }

   public void move(int amount){

                        if(amount < 0 )

                                    throws new IllegalArgumentException();

                        else if(amount = 0)

                                    return;

                        else{

                                    x += amount;

                                    y += amount;

                        }

   }

}

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

<<Java Class>> C Point (default package) x: int y: int Point int) o move (int):void

/**
 * Below class represent a cartesian system point, where each point
 * has a x coordinate along with a y-coordinate.
 *
 */
public class Point {

    /**
     * Instance variable that holds the value of x-coordinate of the point
     */
    private int x;

    /**
     * Instance variable that holds the value of y-coordinate of the point
     */
    private int y;

    /**
     * This method constructs a Point object using an initial position.
     * both x and y coordinates are assigned with initial position
     * This method is known as Constructor
     * @param init: the intial position
     */
    public Point(int init) {
        x = init;
        y = init;
    }

    /**
     * By passing a specific amount, we can move the point in both
     * x and y axis. The point is shifter by adding this amount on
     * both x and y coordinates
     * @param amount
     * @throws IllegalArgumentException : If the given amount is negative, 
     * then it results in an error.
     */
    public void move(int amount) {

        // check if error condition
        if (amount < 0)
            throw new IllegalArgumentException();

        // if there is nothign to move
        else if (amount == 0)
            return;

        // move the points as asked by amount
        else {
            x += amount;
            y += amount;
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Consider the following class Point which represents a point in Cartesian coordinate system. The class has...
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
  • Consider the following class designed to store weather statistics at a particular date and time: public...

    Consider the following class designed to store weather statistics at a particular date and time: public class WeatherSnapshot {     private int tempInFahrenheit;     private int humidity;        // value of 56 means 56% humidity     private int dewPoint;        // in degrees Fahrenheit     private String date;         // stores the date as a String     private int time;            // in military time, such as 1430 = 2:30 pm     private boolean cloudy;      // true if 25% or more of the sky is covered     // constructor not shown, but it...

  • package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic *...

    package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic * collection to store elements where indexes represent priorities and the * priorities can change in several ways. * * This collection class uses an Object[] data structure to store elements. * * @param <E> The type of all elements stored in this collection */ // You will have an error until you have have all methods // specified in interface PriorityList included inside this...

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

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

  • For the code below write a public static main() method in class Student that: - creates...

    For the code below write a public static main() method in class Student that: - creates an ArrayList<Student> object called students - adds 4 new Student objects to the students list, with some made up names and dates - sort the students list by name and display the sorted collection to System.out. use function getCompByName() - sort the students list by enrollment date and display the sorted collection to System.out. use function getCompByDate() import java.util.Comparator;    import java.util.Date;    public...

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

  • 2. (15 marks) Consider the following program: >> Precondition: x and y E Z. Postcondition: Return...

    2. (15 marks) Consider the following program: >> Precondition: x and y E Z. Postcondition: Return the sum x + y. add(x, y): 1. if x == 0: 2. return y 3. elif x > 0: 4. return add(x - 1, y) + 1 5. else: 6. return add(x + 1, y) - 1 Prove that this program is correct in terms of its specification.

  • JAVA You are given a class Critter which represents an animal. Critter class will be the...

    JAVA You are given a class Critter which represents an animal. Critter class will be the superclass of a set of subclasses classes. A Critter has a weight and a position on a number line. The constructor initializes the position to 0 and sets the weight from the parameter. Critter has an ArrayList of Strings to keep a log of its activities. It has other methods which you can view here You are to implement the following subclasses of Critter...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • ANNOTATE BRIEFLY LINE BY LINE THE FOLLOWING CODE: package shop.data; import junit.framework.Assert; import junit.framework.TestCase; public class...

    ANNOTATE BRIEFLY LINE BY LINE THE FOLLOWING CODE: package shop.data; import junit.framework.Assert; import junit.framework.TestCase; public class DataTEST extends TestCase { public DataTEST(String name) { super(name); } public void testConstructorAndAttributes() { String title1 = "XX"; String director1 = "XY"; String title2 = " XX "; String director2 = " XY "; int year = 2002; Video v1 = Data.newVideo(title1, year, director1); Assert.assertSame(title1, v1.title()); Assert.assertEquals(year, v1.year()); Assert.assertSame(director1, v1.director()); Video v2 = Data.newVideo(title2, year, director2); Assert.assertEquals(title1, v2.title()); Assert.assertEquals(director1, v2.director()); } public void testConstructorExceptionYear()...

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