Question

In: Computer Science

Define an interface called Shape that requires methods to get x and y, and a method...

Define an interface called Shape that requires methods to get x and y, and a method to calculate the area. Call these methods getX(), getY(), and getArea(). Write a class called MyRectangle that implements the Shape interface and contains:

the double data fields x, y , and width with get and set methods

a no-arg constructor that creates a default rectangle with 0 for both x and y and 1 for both length and width

a constructor that creates a rectangle with the specified x, y , length, width

a method getX() that returns x location value of the rectangle

a method getY() that returns y location value of the rectangle

a method getArea() that returns the area of the rectangle

Solutions

Expert Solution

//Shape.java
public interface Shape {
    public double getX();
    public double getY();
    public double getArea();
}
//////////////////////////////////////////////

//MyRectangle.java
public class MyRectangle implements Shape {
    private double x,y, width, length;

    public MyRectangle() {
        x = 0;
        y = 0;
        width = 1;
        length = 1;
    }

    public MyRectangle(double x, double y, double width, double length) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.length = length;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double getArea() {
        return width*length;
    }
}

Related Solutions

Define an interface called Vehicle, which requires the following methods getName: returns a String getTopSpeed: returns...
Define an interface called Vehicle, which requires the following methods getName: returns a String getTopSpeed: returns an int getMaxPassengers: returns an int Note that the class Car implements this interface and is preloaded (in one of the following questions you will define this class) For example: Test Vehicle v = new Car("BMW", 280, 5); System.out.println(v.getName()); System.out.println(v.getTopSpeed()); System.out.println(v.getMaxPassengers()); Results : BMW 280 5 Test 2 : Vehicle v = new Car("Smart", 150, 2); System.out.println(v.getName()); System.out.println(v.getTopSpeed()); System.out.println(v.getMaxPassengers()); Results : Smart 150 2
create an interface called Visible that has two methods called makeVisiible, makeInvisible. both methods take no...
create an interface called Visible that has two methods called makeVisiible, makeInvisible. both methods take no parameters and should return a boolean result. HTML EditorKeyboard Shortcuts 12pt Paragraph 0 words Flag this Question Question 58 pts The following classes have been created with the given behaviors: public class Leo extends Don { public void method1() { System.out.print("Leo 1 "); } public void method3() { System.out.print("Leo 3 "); } public String toString() { return "Leo 1 "; } } class Mike...
In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y),...
In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y), where x and y are integers. Define methods to individually set the x and y coordinates of your new point and retrieve their values. Write an Objective-C program to implement your new class and test it (main section of the file). Your test program needs to create two instances of your class. Make sure your program test prints out the values that you have...
Programming Language: C++ Create a base class called Shape which has 2 attributes: X and Y...
Programming Language: C++ Create a base class called Shape which has 2 attributes: X and Y (positions on a Cartesian coordinate system). Since a shape is amorphous, set the class up so that an object of type Shape can not be instantiated. Create three derived classes of your choice whose base class is Shape. These derived classes should have accessors/mutators for their class specific attributes, as well as methods to compute the area and the perimeter of the shape. In...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random. 2- Document: that implements turn(), which changes the page on the document to the...
Suppose (X, dX) and (Y, dY ) are metric spaces. Define d : (X ×Y )×(X...
Suppose (X, dX) and (Y, dY ) are metric spaces. Define d : (X ×Y )×(X × Y ) → R by d((x, y),(a, b)) = dX(x, a) + dY (y, b). Prove d is a metric on X × Y .
Using Java, write code as follows: Create an interface called Items. It should define at least...
Using Java, write code as follows: Create an interface called Items. It should define at least the following methods: public void add( Object item ) - adds a single object to the collection. public Object get( int index ) - returns an item at a specific index. public int size() - returns the number of items stored in the collection. public void addAll( Object[] items ) - adds all of the elements in the array to the collection. Write a...
5. Equations of the form y’ = P(x)*y^2 + Q(x)*y + R(x) are called Riccati equations....
5. Equations of the form y’ = P(x)*y^2 + Q(x)*y + R(x) are called Riccati equations. i) If we know a solution y = φ(x) of this equation, then any other solution can be written in the form y(x) = φ(x)+ 1/v(x), where v(x) is an unknown function which satisfies a certain linear equation. Using the fact that φ and y both solve the above Riccati equation, find the differential equation that v satisfies. ii) Consider the equation 3y’ +...
5. Equations of the form y’ = P(x)*y^2 + Q(x)*y + R(x) are called Riccati equations....
5. Equations of the form y’ = P(x)*y^2 + Q(x)*y + R(x) are called Riccati equations. i) If we know a solution y = φ(x) of this equation, then any other solution can be written in the form y(x) = φ(x)+ 1/v(x), where v(x) is an unknown function which satisfies a certain linear equation. Using the fact that φ and y both solve the above Riccati equation, find the differential equation that v satisfies. ii) Consider the equation 3y’ +...
x^2*y''+x*y'+(x^2-1)y=0 what is the solution by using Frobenius method
x^2*y''+x*y'+(x^2-1)y=0 what is the solution by using Frobenius method
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT