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:

Code :

/**
* 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?

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?

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.

Re: Are these math statements correct?

Also note you've got your perimeter and area calculations the wrong way around ;)

Re: Are these math statements correct?

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

RectangleProgram:

Code :

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:

Code :

/**
* 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

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.

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.

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.... :)