Feel like I'm overthinking this problem.
I want to make a class that creates a rectangle:
Code Java:
public class Rectangle
{
private double x; // x & y need to be the upper left corner of the rectangle
private double y; // x & y need to be the upper left corner of the rectangle
private double width;
private double height;
}
I don't know if I am overthinking it, or what, but I can't figure out how to initialize these things so that when set width and height to say, 3 and 5, x and y are the upper left corner.
Re: Feel like I'm overthinking this problem.
x and y define the location of the rectangle. This is based on the top left corner of the container it is in to the top left corner of the shape you are drawing. As long as the width and height are not minus numbers, x and y will be the top left corners.
I think this was what you were asking? correct me if I misunderstood :/
If you meant the upper left corner of the container, then set x and y to be 0,0
hope this helps ;)
Re: Feel like I'm overthinking this problem.
Quote:
but I can't figure out how to initialize these things
Are you confused about initializing the double variables? Since a double is a primitive, it is given a default value when you create it. For doubles, this value is 0.0. So, the current statements you have no will, by default, initialize each of those doubles to 0.
For a list of primitives and their defaults, see this link: Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
Now if your question is about how to set those variables based on the user's specifications (or hard-coded specifications), create a constructor where you should set the values of those variables. I'm sure you have basic notes on how to create a constructor.