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 8 of 8

Thread: Are these math statements correct?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Are these math statements correct?

    I wrote a program to take two values from the user that will determine perimeter and area of a rectangle. However, when I run the program, it is only adding the area input values together instead of multiplying them, and it always returns me 0 for the perimeter value.

    I assume here that java respects standard math order of operations, but the java class that I'm taking right now has not gone that in depth with math statements. Here is the code snippet that handles the math:

        /**
         * This method will take the inputs from the RectangleProgram class and calculate
         * the area & perimeter.
         */
        public void rectData(double widthIn, double heightIn) {
            // Set width value
            widthInput = widthIn;
            // Set height value
            heightInput = heightIn;
            // Calculates perimeter of rectangle
            rectPerimeter = widthInput * heightInput;
            // Calculates area of rectangle
            rectArea = (widthInput * 2) + (heightInput * 2);
        }

    Does anything look off here?


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Are these math statements correct?

    Everything looks fine to me, I am assuming that rectArea is a global variable? Also is that defined as a double? Are you definately passing in the data you should be to this method?
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Are these math statements correct?

    Print out the values of widthIn and heightIn to be sure they are what you expect the method to receive.

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Are these math statements correct?

    Also note you've got your perimeter and area calculations the wrong way around
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. The Following User Says Thank You to newbie For This Useful Post:

    javadude (January 4th, 2012)

  6. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Are these math statements correct?

    I'm going to post both classes since it makes more sense that way.

    RectangleProgram:

    import java.util.Scanner;
    /**
     * This is the main class of RectangleProgram. This class will take
     * width and height inputs from the user and pass them to the
     * helper class RectangleFormula to return perimeter and area
     * calculations.
     * 
     * @author javadude
     * @version 1.0
     */
    public class RectangleProgram
    {
        public static void main(String[] args) {
            double widthInput = 0.0;
            double heightInput = 0.0;
            Scanner keyInput = new Scanner(System.in);
            RectangleFormula rf1 = new RectangleFormula();
            RectangleFormula rf2 = new RectangleFormula();
            System.out.print("Enter a height: ");
            heightInput = keyInput.nextDouble();
            rf1.rectData(heightInput, widthInput);
            System.out.print("Enter a width: ");
            widthInput = keyInput.nextDouble();
            // rf2.rectData(widthInput);
            System.out.println("The area of the rectangle with the given width of " + widthInput);
            System.out.println("and the given height of " + heightInput + " is " + rf1.getArea());
            System.out.println("The perimeter of the rectangle with the given width of " + widthInput);
            System.out.println("and the given height of " + heightInput + " is " + rf1.getPerimeter());
        }
    }

    RectangleFormula:

    /**
     * This class contains all of the formulas to determine area & perimeter of a rectangle.
     * Data is passed to it from RectangleProgram and this will return it's calculations to 
     * the same class.
     * 
     * @author javadude
     * @version 1.0
     */
    public class RectangleFormula
    {
        // widthInput holds the width
        // heightInput holds the height
        //private double widthInput;
        //private double heightInput;
        private double rectPerimeter;
        private double rectArea;
     
        /**
         * Default constructor for RectangleFormula sets the default
         * value of the widthInput and heightInput variables to 0.0
         */
        public RectangleFormula() {
            // initialize the two instance variables
            this(0.0, 0.0);
        }
     
        /**
         * This constructor method will create and assign the width and height values to 
         * the setHeight and setWidth objects.
         */
        public RectangleFormula(double widthIn, double heightIn) {
        //  rectWidth(widthIn);
        //  rectHeight(heightIn);
            rectData(widthIn,heightIn);
        }
     
        /**
         * This method will take the inputs from the RectangleProgram class and calculate
         * the area & perimeter.
         */
        public void rectData(double widthIn, double heightIn) {
            // Set width value
            //widthInput = widthIn;
            // Set height value
            //heightInput = heightIn;
            // Calculates area of rectangle
            rectArea = widthIn * heightIn;
            // Calculates perimeter of rectangle
            rectPerimeter = (widthIn * 2) + (heightIn * 2);
        }
     
        /**
         * getPerimeter retrieves the perimeter of the rectangle
         */
        public double getPerimeter() {
            return rectPerimeter;
        }
     
        /**
         * getArea retrieves the area of the rectangle
         */
        public double getArea() {
            return rectArea;
        }
     
    }

    I've commented a couple global variables out since they weren't actually doing anything (using local variables widthIn and heightIn instead). I'm attempting to use breakpoints & debug in eclipse and failing there. The breakpoints stop repeatedly at the rectArea & rectPerimeter statements, but I was more or less hoping to stop there and see what they've actually store. Guess breakpoints don't actually work that way.... :-P

  7. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Are these math statements correct?

    There is no reason for the code to print 0 unless both your inputs are 0.
    Additionally, your width parameter will always be 0 as you call: rf1.rectData(heightInput, widthInput) before retrieving the value from the user.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  8. The Following User Says Thank You to newbie For This Useful Post:

    javadude (January 4th, 2012)

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Are these math statements correct?

    To see what your code is doing, add some printlns to print out the values of the variables as they are used to compute the results.

  10. The Following User Says Thank You to Norm For This Useful Post:

    javadude (January 4th, 2012)

  11. #8
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Are these math statements correct?

    Once again newbie hit the nail on the head. I had placed rf1.rectData(heightInput, widthInput); before actually taking the widthInput data from the user, so that's why it was coming up with funky results - because one of the variables was actually 0!

    I'm learning... slowly....

Similar Threads

  1. Can anybody correct the code?
    By ur2cdanger in forum JDBC & Databases
    Replies: 1
    Last Post: October 17th, 2011, 08:07 PM
  2. Is this correct? Tips if any please
    By giga97 in forum Object Oriented Programming
    Replies: 1
    Last Post: October 8th, 2011, 11:03 AM
  3. Replies: 2
    Last Post: August 2nd, 2011, 09:11 AM
  4. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 02:28 AM
  5. Correct Coupling
    By poulenc in forum Java Theory & Questions
    Replies: 0
    Last Post: December 26th, 2010, 04:28 AM