I need help with Cartesian coordinates:

Write a Java interface named Centered with two abstract methods with no argument: one named getCenterX that returns a double, and another named getCenterY that returns also an double. The purpose of these two methods is to return the two Cartesian coordinates of the center of an object. Write a class called Rectangle that stores the Cartesian coordinates of two opposite corners of a rectangle, and write another class called Circle, that stores the Cartesian coordinates of the center, as well as the radius of the circle. Have both class implement the Centered interface. Write a test program using these two classes. Be sure your code compiles and runs as expected. Name your Java files Centered.java., Rectangle.java, Circle.java, TestCentered.java

Here is what I have so far:

public interface Centered {
    public abstract double getCenterX();
    public abstract double getCenterY();


}



public class Rectangle {
 // The CartesianRectangle is an implementation of a Rectangle in the Cartesian coordinate system
 // instance variables
 // This representation of a rectangle stores the x and y coordinates of opposite corners of the rectangle.

  public int[] xcoords; // stores the x-coordinates of one diagonal of the rectangle
  public int[] ycoords; // stores the y-coordinates of one diagonal of the rectangle

 // Constructor which takes the coordinates of the two opposite corners of the rectangle.
  public Rectangle (int x1, int y1, int x2, int y2) {
    // One way to initialize an array.
    xcoords = new int[2];
    xcoords[0] = x1;
    xcoords[1] = x2;

   int[] yArray = {y1,y2};
    ycoords = yArray;

}
}

I need help with Cartesian coordinates:

Write a Java interface named Centered with two abstract methods with no argument: one named getCenterX that returns a double, and another named getCenterY that returns also an double. The purpose of these two methods is to return the two Cartesian coordinates of the center of an object. Write a class called Rectangle that stores the Cartesian coordinates of two opposite corners of a rectangle, and write another class called Circle, that stores the Cartesian coordinates of the center, as well as the radius of the circle. Have both class implement the Centered interface. Write a test program using these two classes. Be sure your code compiles and runs as expected. Name your Java files Centered.java., Rectangle.java, Circle.java, TestCentered.java

Recommended Answers

All 7 Replies

Can you explain what your problem is with this assignment?

Write a class called Rectangle that stores the Cartesian coordinates of two opposite corners of a rectangle, and write another class called Circle, that stores the Cartesian coordinates of the center, as well as the radius of the circle. Have both class implement the Centered interface. Write a test progr

I dont understand how to write the catesian coordiantes...

Are those the x and y values used to locate a pixel on the screen?

no they are supposed to store values. disregard that class. i really didnt know what iw as doing..

Can you explain your problem now?

Okay,if your classes Rectangle and Circle implement the interface Centered,then you must use the implements keyword while declaring classes Rectangle and Circle like:

public class Rectangle implements Centered{

}

Then you must provide definitions for the methods getCenterX() and getCenterY() in your class Rectangle and Circle else your classes will be forced to be declared abstract.
Your approach to declaring the arrays are fine.Just have getCenterX() method return the values of the center of the shapes by returning (xcoords[0] + xcoords[1])/2 for the getCenterX() in rectangle and implementing similarly for the Y-coordinate and returning simply the center's (x,y) in the respective methods.

And yes,certain un-necessary keywords can be avoided like:
1.No need to use the word public before any method or data-member of an interface.
2.All methods of an interface are implicitly abstract.No need to declare them so.

All members of an interface are implicitly public,static and final

Java includes a concept called interfaces. An interface is a bit like a class, except you can only declare methods and variables in the interface. You cannot actually implement the methods. Interfaces are a way to achieve polymorphism in Java. I will get back to this later in this text. An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.