Question

Given the following code fragment public class Point { public int x; // Because these are...

Given the following code fragment


public class Point {

public int x; // Because these are public, you can access them public int y; // directly without getters and setters

    };
    public class Rectangle {
        private Point ll; // the lower left corner of the rectangle
        private Point ur; // the upper right corner of the rectangle
        public Point getLLPoint() {return ll;}
        public Point getURPoint() {return ur;}

}


  1. (a) Write a method equals for the Rectangle class that takes a Rectangle as a parameter and returns true if two rectangles have the same area, and false otherwise


  2. (b) Write a method manhattanDistance for the Point class that takes a Point object and calculates the distance between the current Point and the the given other Point assuming travel only along horizontal and vertical lines (e.g. between the manhattan distance between (0,0) and (1,1) would be 2)

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

public boolean equal_area(Rectangle r1,Rectangle r2){
   int r1_length = r1.getURPoint().x - r1.getLLPoint().x;
   int r1_breadth = r1.getLLPoint().y - r1.getURPoint().y;
   int r1_area = r1_length*r1_breadth;
   int r2_length = r2.getURPoint().x - r2.getLLPoint().x;
   int r2_breadth = r2.getLLPoint().y - r2.getURPoint().y;
   int r2_area = r2_length*r2_breadth;
   if (r1_area == r2_area)
       return true;
   return false;
}
public int manhattanDistance(Point p){
   return abs(p.x - x) + abs(p.y - y);
   // abs is absolute value
}

Add a comment
Know the answer?
Add Answer to:
Given the following code fragment public class Point { public int x; // Because these are...
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
  • public class Point f private int x; private int y; public Point(int x, int y) this.x...

    public class Point f private int x; private int y; public Point(int x, int y) this.x X; this.y y; public int getX() return x; public int getY() return Y: public double distance (Point other) double dx this.x-other.x; double dy this.y-other.y double dist Math.sqrt(dx dx + dy dy); return dist;

  • Question 11 (20 points) Given the class Point below, define a class Circle that represent a...

    Question 11 (20 points) Given the class Point below, define a class Circle that represent a circle with a given center and radius. The circle class should have a center attribute named center as well as a floating point radius attribute. The center is a point object, defined by the class Point. The class should also have these members: the constructor of the class, which should take parameters to initialize all attributes - a getter for center a setter for...

  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • What is wrong with the following code: class Point { private : int x, y; public...

    What is wrong with the following code: class Point { private : int x, y; public : Point (int u, int v) : x(u), y(v) {} int getX () { return x; } int getY () { return y; } void setX (int newX ) const { x = newX ; } }; int main () { Point p(5, 3); p.setX (9001) ; cout << p. getX () << ’ ’ << p. getY (); return 0; }

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • for c++ answer the following and create the following (1)Declare a class to hold Employee Data....

    for c++ answer the following and create the following (1)Declare a class to hold Employee Data. It must have at least 2 attributes and 1 method in addition to getters/setters for 1 of the 2 attributes. Also, include at least 2 constructors and a destructor. Put the appropriate members in public and private sections. You don't need to write the code to implement the class except for getters and setters which should include the appropriate inline code. (2) What is/are...

  • Implement the Point class (code for this up to the isHigher method was discussed in the...

    Implement the Point class (code for this up to the isHigher method was discussed in the lecture). The class has the following instance variables: • x coordinate (an int) • y coordinate (an int) and the following methods: • Constructor that sets the x and y coordinates • Get and set methods • Method equals if this point is equal to another point object (if the x and y coordinates are the same). • Method isHigher if this point is...

  • Consider the Automobile class: public class Automobile {    private String model;    private int rating;...

    Consider the Automobile class: public class Automobile {    private String model;    private int rating; // a number 1, 2, 3, 4, 5    private boolean isTruck;    public Automobile(String model, boolean isTruck) {       this.model = model;       this.rating = 0; // unrated       this.isTruck = isTruck;    }        public Automobile(String model, int rating, boolean isTruck) {       this.model = model;       this.rating = rating;       this.isTruck = isTruck;    }                public String getModel()...

  • In the following program: public  class Problem2{     public static int sample(int x, int y){         int z;         .........

    In the following program: public  class Problem2{     public static int sample(int x, int y){         int z;         ......      }     public static void  main(String[] args)     {          int a, b;          ......          sample(a,b);     } } (1) What is the return type of sample? (2) What are the local variables of sample? (3) What are the parameters of sample? (4) What are the arguments of sample? (5) Is sample a per-object method? Or a per-class method? (6) Is sample a public method, or private method?

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