1

I have the following code:

public class Rectangle extends java.awt.Rectangle
{
    private Position position;


public Rectangle(Rectangle toCopy)
{
    this.position = toCopy.position;
}

public Rectangle(Position position, int width, int height)
{
    super(width, height);
    this.position = position;
}

This works fine, however I need width and height to be floats. The problem is the constructor in java.awt.Rectangle takes in an int type for width and height. Is there any way to make width and height floats within the constructor?

MDS18
  • 81
  • 8
  • 3
    Q: Do you really want to call "your" Rectangle exactly the same name as java.awt.Rectangle? Probably a bad idea. Q: Do you really want to cast to (int); losing precision? Probably a bad idea. MAIN Q: Why do you want to subclass java.awt.Rectangle in the first place? What exactly is your motivation for your custom "Rectangle" class? ALSO: look here for alternatives to casting: [Java Float To Int](http://javadevnotes.com/java-float-to-int) – paulsm4 Mar 28 '18 at 19:21
  • @paulsm4 If it were up to me, I would just make my own Rectangle class.... I dont know why we are inheriting from this java.awt.Rectangle class at all. This is just for a project, and my project leader designed this part like it. I guess he just didn't want to write methods, just leave it to the built in Rectangle class. Like I said, if it were up to me, I'd just write my own Rectangle class, it would save alot of headaches for me I think. – MDS18 Mar 28 '18 at 19:34

3 Answers3

2

I believe you're looking for casting. This allows you to turn an float into a int. The implementation in your example would be:

public Rectangle(Position position, float width, float height) // Changed types to float
{
    super((int) width, (int) height); // Cast from float to int
    this.position = position;

    // width & height are floats, like requested
}
Aaron N. Brock
  • 4,276
  • 2
  • 25
  • 43
  • Thank you so much!!! I stupidly thought that the constructor in my code would have to take in ints, and then I cast them to floats after. – MDS18 Mar 28 '18 at 19:24
  • 1
    Just beware that "1.9999" will truncate to "1"; and "0.9999" to "0"... Please do look at this link if you want to "fine tune" this behavior: http://javadevnotes.com/java-float-to-int – paulsm4 Mar 28 '18 at 22:14
0

Just cast the int to a float using casting like this: float myfloat = (float) myInt;

So change this:

      public Rectangle(Position position, int width, int height)
{
super(width, height);
this.position = position;
}

To This:

   public Rectangle(Position position, int width, int height)
{
super((float) width,(float) height);
this.position = position;
}
SteelToe
  • 2,477
  • 1
  • 17
  • 28
  • 1
    Huh, so, I thought the OP wanted the constructor arguments to be floats. Now I'm not 100% sure. – Aaron N. Brock Mar 28 '18 at 19:12
  • So i tried that and got this compile error: constructor Rectangle.Rectangle(int, int) is not applicable argument mismatch; possible lossy conversion from float to int) – MDS18 Mar 28 '18 at 19:13
  • @AaronN.Brock The constructor for the code needs to take in the variables from the java.awt.Rectangle class. And yes, they need to be floats. But that java.awt.Rectangle class has those variables as ints, and I cant figure out how to convert them to floats. – MDS18 Mar 28 '18 at 19:16
  • @MattyS11 Why do they need to be floats? The super constructor for java.awt.rectangle takes ints. – SteelToe Mar 28 '18 at 19:17
  • @SteelToe this is the problem. My project leader for the assignment drew up the class diagram, and in the constructor it says that they need to be floats. And I need to apparently get these from java.awt.rectangle. The reason is because the rectangle that we are using is gonna be used as sort of a hitbox, and the positions need to be floats in order to be mathematically accurate, I guess. – MDS18 Mar 28 '18 at 19:21
0

Maybe you can try this:

int i;
float f = i * 1.0f;
Elon_wang
  • 31
  • 3