Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Feel like I'm overthinking this problem.

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Feel like I'm overthinking this problem.

    I want to make a class that creates a rectangle:

    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.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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
    Last edited by Ciaran54; February 22nd, 2011 at 08:27 AM.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Feel like I'm overthinking this problem.

    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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Replies: 1
    Last Post: February 10th, 2012, 10:05 AM
  2. Look & Feel
    By Asido in forum AWT / Java Swing
    Replies: 3
    Last Post: September 10th, 2010, 09:13 PM
  3. Replies: 2
    Last Post: October 14th, 2009, 10:10 AM