Results 1 to 13 of 13
  1. #1
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default Java custom class, need help!

    I am a bit stuck right here.


    Next, we will be implementing a method named scale. It will be a mutator method for the class that can be used to change the size of the Circle. The signature line is done for you.

    * Inside the scale method write the Java statement(s) necessary to change the Circle's radius so that it is scaleFactor times its old value.

    # Next, implement the method named setRadius that does not return a value and which has one parameter for the new radius of the Circle.


    Here is the code. Radius and Scalefactor are at the bottom.


    Java Code:
    public class Circle
    {
        
        private double x;
        private double y;
        private double radius;
        
        /**
         * Default constructor for circles.
         * The default circle is at (0,0) and radius of 1.
         */
        public Circle()
        {
            x = 0;
            y = 0;
            radius = 1;
        }
        
        /**
         * constructs circles
         * 
         * @param initialX The initial X of the center.
         * @param initialY The initial Y of the center.
         * @param initialRadius The initial radius.
         */
        public Circle(double initialX, double initialY, double initialRadius)
        {
            x = initialX;
            y = initialY;
            radius = initialRadius;
        }
        
        
    
        /**
         * Accesses the X of the center point of the circle.
         * 
         * @return the X of the center point
         */
        public double getX()
        {
            return x;
        }
        
        /**
         * Accesses the Y of the center point of the circle.
         * 
         * @return the Y of the center point
         */
        public double getY()
        {
            return y;
        }
        
        /**
         * Accesses the radius of the circle.
         * 
         * @return the radius
         */
        public double getRadius()
        {
            return radius;
        }
        
        /**
         * Computes the diameter of the circle.
         * 
         * @return the diameter
         */
        public double getDiameter()
        {
            return radius * 2;
        }
        
        /**
         * Computes the area of the circle.
         * 
         * @return the area
         */
        public double getArea()
        {
            return Math.PI * radius * radius;
        }
        
        
        /**
         * Computes the circumference of the circle.
         * 
         * @return the circumference
         */
        public double getCircumference()
        {
            double c = 2 * Math.PI * radius;
            return c;
        }
        
        /**
         * Changes the location of the circle.  The new
         * location is <b>relative</b> to the old.
         * 
         * @param deltaX The distance in the X dimension that the circle is moved.
         *      More detailed explanation of deltaX.... blah blah blah.
         * @param deltaY The distance in the Y dimension that the circle is moved.
         */
        public void translate(double deltaX, double deltaY)
        {
            x = x + deltaX;
            y += deltaY; // y = y + deltaY;
        }
        
        /**
         * Changes the location of the circle.  The new
         * location replaces the old.
         * 
         * @param newX The new X coordinate of the center.
         * @param newY The new Y coordinate of the center.
         */
        public void setCenter(double newX, double newY)
        {
            x = newX;
            y = newY;
        }
        
        
        public void scale(double scaleFactor)
        {
           
            
         
        }
        
        public void setRadius(double newRadius)
        {
            
        }
    
    
    }
    Last edited by Fubarable; 10-10-2009 at 04:16 PM. Reason: Code tags added for readability

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    What's the question or problem? We can't help you if you don't ask for something in specific.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by CodesAway View Post
    What's the question or problem? We can't help you if you don't ask for something in specific.
    Inside the scale method write the Java statement(s) necessary to change the Circle's radius so that it is scaleFactor times its old value.

    # Next, implement the method named setRadius that does not return a value and which has one parameter for the new radius of the Circle.

    This is what I need implemented in towards the bottom of the code in the body, I'm just not sure how.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Keno777 View Post
    Inside the scale method write the Java statement(s) necessary to change the Circle's radius so that it is scaleFactor times its old value.
    Please try to attempt this yourself first, else you'll be cheating yourself.

    You have the scaleFactor which is the parameter to the scale method, you have the radius value (which is held by the variable "radius"), and you want to use these two bits of information to change the radius. So inside the scale method create a single line of code that does this (or at least try your best attempt) and post it here. We can then help direct you if you are wrong.

    # Next, implement the method named setRadius that does not return a value and which has one parameter for the new radius of the Circle.

    This is what I need implemented in towards the bottom of the code in the body, I'm just not sure how.
    Again, it's a single line of code, and again you'll be far better served to first attempt a solution and post it here.

    Much luck!

    Edit: I added code tags to your original post to make the code more readable. Please look at my signature if you wish to do this yourself in future posts. Doing this will increase the chances that someone will be willing to read your code and to help you. Again, luck.

  5. #5
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Please try to attempt this yourself first, else you'll be cheating yourself.

    You have the scaleFactor which is the parameter to the scale method, you have the radius value (which is held by the variable "radius"), and you want to use these two bits of information to change the radius. So inside the scale method create a single line of code that does this (or at least try your best attempt) and post it here. We can then help direct you if you are wrong.


    Again, it's a single line of code, and again you'll be far better served to first attempt a solution and post it here.

    Much luck!

    Edit: I added code tags to your original post to make the code more readable. Please look at my signature if you wish to do this yourself in future posts. Doing this will increase the chances that someone will be willing to read your code and to help you. Again, luck.

    Only thing I have tried and thought of was something like this for the scalefactor.

    x = scaleFactor * radius;

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Keno777 View Post
    Only thing I have tried and thought of was something like this for the scalefactor.

    x = scaleFactor * radius;
    You may wish to re-read your instructions carefully as they will tell you exactly what to do:
    * Inside the scale method write the Java statement(s) necessary to change the Circle's radius so that it is scaleFactor times its old value.
    So what do you need to change in the scale method? Is it x like you're showing us above? no. Again, the instructions are telling you all you need to know.

  7. #7
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    scaleFactor = radius * (something)

    maybe this is correct? Trying to figure out what it means by "old value"

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Keno777 View Post
    scaleFactor = radius * (something)

    maybe this is correct? Trying to figure out what it means by "old value"
    The left side of the expression is what you are trying to change, and the right side is how you are trying to change it. The instructions (again) are thus:
    * Inside the scale method write the Java statement(s) necessary to change the Circle's radius so that it is scaleFactor times its old value.
    So what are we trying change here? That goes on the left. How are you supposed to change this? That goes on the right. It would be easy to just give you the answer, but I'm trying to make you think here. Please try again.

  9. #9
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    radius = scaleFactor *


    Whats being changed is the Radius.(left) scaleFactor times old value is how to change it. (right) Still confused about what the old value would be after the times part.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    OK, you've got the left correct, and had the right side correct earlier, so now all you have to do is combine them:
    Java Code:
    radius = scaleFactor * radius;
    A shorthand for this is to do it thusly:
    Java Code:
    radius *= scaleFactor;
    for instance, if I do this:

    Java Code:
    int i = 2;
    
    i = 4 * i;  // i now is 8
    // another way to do this:
    // i *= 4;
    
    i *= 3;  // i now is 24
    // another way to do this:
    // i = 3 * i;
    make sense?
    Last edited by Fubarable; 10-12-2009 at 01:21 AM.

  11. #11
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Java Code:
    int i = 2;
    
    i = 4 * i;  // i now is 8
    // another way to do this:
    // i *= 4;
    
    i *= 3;  // i now is 24
    // another way to do this:
    // i = 3 * i;
    Sorry for correcting such a small detail, but i *= 3 is another way to do i = i * 3. In multiplication, this doesn't matter, but in string concatenation, it makes a BIG difference.

    For example,

    Java Code:
    String string = "a";
    string += "b"; // same as string = string + "b"
    
    // outputs "ab"
    System.out.println(string);
    As a note, these "shortened" assignment operators remove the left operand.

    Again, sorry for correcting such a minor detail.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by CodesAway View Post
    Sorry for correcting such a small detail, but i *= 3 is another way to do i = i * 3. In multiplication, this doesn't matter, but in string concatenation, it makes a BIG difference.
    //....
    Again, sorry for correcting such a minor detail.
    Good lord, never be sorry for correcting anything here, especially anything I post. I am grateful for all corrections as are most here. Many thanks.

  13. #13
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

Similar Threads

  1. Custom class in an array?
    By bugmenot in forum New To Java
    Replies: 5
    Last Post: 04-15-2009, 01:10 AM
  2. Importing custom class
    By BigRed in forum Eclipse
    Replies: 2
    Last Post: 02-14-2009, 03:12 PM
  3. Custom Class, Wrapper/Subclass?
    By Moncleared in forum Advanced Java
    Replies: 3
    Last Post: 02-10-2009, 12:08 AM
  4. Demonstration of the custom InputDialog class
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 08:56 PM
  5. Importing a Custom Class
    By jfredrickson in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 12:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •