xyz1
xyz1

Reputation: 11

Multiple Methods in determining Area of Rectangle

The question that I am really stuck on is this:

Write a program that asks the user to enter the width and length of a rectangle, and then display the rectangle’s area. The program should call the following methods:

• getLength – This method should ask the user to enter the rectangle’s length, and then return that value as a double.

• getWidth – This method should ask the user to enter the rectangle’s width, and then return that value as a double.

• getArea – This method should accept the rectangle’s length and width as arguments, and return the rectangle’s area. The area is calculated by multiplying the length by width.

• displayArea – This method should accept the rectangle’s length, width, and area as arguments, and display them in an appropriate message to the screen.

I don't know how to complete this code because right now what I have is this:

    import java.util.Scanner;
public class WidthLengthAreaMethods
{
  public static void main(String[]args)
  {

  Scanner keyboard = new Scanner(System.in);

  double length;
  double width;
  double area;

  length = getLength();
  width = getWidth();
  area = getArea(double length, double width);
  displayData(length, width, area);
  }
  public static double getLength()
  {
    System.out.println("Enter length. ");
    length = keyboard.nextDouble();
    System.out.println("The length is " + length);
  }
  public static double getWidth()
  {
    double width;
    System.out.println("Enter width. ");
    width = keyboard.nextDouble();
    System.out.println("The width is " + width);
  }
  public static double getArea()
  {
   double length;
   double width;
   double area = length * width;

   System.out.println("The area is: " + area);
  }
  public static void displayData(double length, double width, double area)
  {
    System.out.println(" The length is: \t" + length);
    System.out.println(" The width is: \t" + width);
    System.out.println(" The area is: \t" + area);
  }
}

What am I screwing up on and how would I go about fixing it? I am a beginner in programming so please bear with me :D.

Thanks guys!!

Upvotes: 1

Views: 15845

Answers (2)

mschug
mschug

Reputation: 21

Since your program is broken up into several methods, the data inside each method is local unless you store it inside the class itself.

For example, your helper functions for getLength() and getWidth() wouldn't be able to access your keyboard Scanner unless you declared it outside of the main method, as such:

import java.util.Scanner;

public class WidthLengthAreaMethods {

    Scanner keyboard = new Scanner( System.in ); 
    // Initialized within the class, but outside of any methods

    public static void main( String[] args ) {
        double length = getLength();
        double width = getWidth();
        double area = getArea( length, width );
        displayData( length, width, area );
    }

}

Another alternative would be to pass your Scanner to each of the helper methods in their function calls, e.g.

public static double getLength( Scanner keyboard ){}

While passing the Scanner to each function separately would allow your methods to work as intended, the first option is slightly more readable.

The other thing to consider is that when a method has a return value, such as a double in the case of getLength(), getWidth(), and getArea(), the piece of code calling the function is expecting some variable of that type to be returned. In the case of a void function, such as main() or displayData(), the method states that it will not return a variable of any specific type.

Therefore, when you set length to equal getLength(), what you're trying to do is set the value of your local length variable to equal the value coming back from your helper function. If that value will never be sent, the program will most likely be unable to compile - an error will be thrown stating something along the lines of "expected type double" when you try to call that function. To fix the compiler error, a return statement needs to be added in to the helper functions, such as:

  Scanner keyboard = new Scanner( System.in );

  public static double getWidth() {

    System.out.println("Enter width.");
    double width = keyboard.nextDouble(); // Sets the value to return to your main function
    System.out.println("The width is " + width);
    return width; // Returns the value to your main function 
                  // Causes any code underneath the return statement to be ignored
  }

Combining all of that should allow the compiler errors to stop, and make your program work correctly.

Upvotes: 2

Msk
Msk

Reputation: 857

Here is the working solution

import java.util.Scanner;

public class WidthLengthAreaMethods {

      public static void main(String[]args)
      {


      double length;
      double width;
      double area;

      length = getLength();
      width = getWidth();
      area = getArea(length, width);
      displayData(length, width, area);
      }

      public static double getLength()
      {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter length. ");
        double length = keyboard.nextDouble();
        System.out.println("The length is " + length);
        return length;
      }
      public static double getWidth()
      {
        Scanner keyboard = new Scanner(System.in);
        double width;
        System.out.println("Enter width. ");
        width = keyboard.nextDouble();
        System.out.println("The width is " + width);
        return width;
      }

      public static double getArea(double length, double width)
      {
       double area = length * width;
       System.out.println("The area is: " + area);
       return area;
      }

      public static void displayData(double length, double width, double area)
      {
        System.out.println(" The length is: \t" + length);
        System.out.println(" The width is: \t" + width);
        System.out.println(" The area is: \t" + area);
      }
    }

Upvotes: 1

Related Questions