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

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

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

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

  • Consider the java Graph class below which represents an undirected graph in an adjacency list. How...

    Consider the java Graph class below which represents an undirected graph in an adjacency list. How would you add a method to delete an edge from the graph? // Exercise 4.1.3 (Solution published at http://algs4.cs.princeton.edu/) package algs41; import stdlib.*; import algs13.Bag; /** * The <code>Graph</code> class represents an undirected graph of vertices * named 0 through V-1. * It supports the following operations: add an edge to the graph, * iterate over all of the neighbors adjacent to a vertex....

  • Write a unit test to test the following class. Using Java : //Test only the debit...

    Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...

  • Create a new class called Point that represents a point in the Cartesian plane with two...

    Create a new class called Point that represents a point in the Cartesian plane with two coordinates. Each instance of Point should have to coordinates called x and y. The constructor for Point should take two arguments x and y that represent the two coordinates of the newly created point. Your class Point should override the __str__(self) method so that it returns a string showing the x- and y-coordinates of the Point enclosed in parentheses and separated by a comma....

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