Reputation: 703
I am new to Java programming. I have three classes which are Main
, Point
and Rectangle
. I can use all constructors in Rectangle
class except this one: Rectangle(Point p, int w, int h)
. Java compiler gives: "Cannot resolve constructor 'Rectangle(java.awt.Point, int, int)'" error. Thanks.
Here my classes:
Main.java
import java.awt.*;
import java.awt.Point;
import java.awt.Rectangle;
public class Main {
public static void main(String[] args) {
Point originOne = new Point(5,10);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
}
}
Point.java
public class Point {
public int x = 0;
public int y = 0;
//contructor
public Point(int a, int b){
x = a;
y = b;
}
}
Rectangle.java
public class Rectangle {
public Point origin;
public int width = 0;
public int height = 0;
//four contructors
public Rectangle() {
origin = new Point(0, 0);
}
public Rectangle(Point p){
origin = p;
}
public Rectangle(int w, int h){
origin = new Point(0,0);
width = w;
height = h;
}
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
// a method for moving the rectangle
public void move(int x, int y) {
origin.x = x;
origin.y = y;
}
//a method for computing the area of rectangle
public int getArea() {
return width * height;
}
}
Upvotes: 2
Views: 2399
Reputation: 238
Your answer is fairly simple. You've imported the wrong import, rather than import java.awt.Rectangle
, import your own class.
By doing:
import <nameOfTheProject>.Rectangle;
If I had a project in Eclipse called MyShapes and had the same classes. I would import it like:
import MyShapes.Rectangle;
Now this obviously depends on how your file structure is, but if it's inside a sub-folder (sub-package), I would do like:
import MyShapes.ShapesClasses.Rectangle;
This also applies to the Point class if you're planning on using your own Point class and not Java.awt's one!
Upvotes: 4