0

I'm new to coding and I'm having trouble with trying to get a double from another method. In trying to make it so I can set the length, width, and height of a cube and it outputs these numbers and the area, but I can't get one of my methods to take the math problem I made?

Cube.java

public class Cube {


    public double height;
    public double width;
    public double length;


    static void CalculateTotalArea(double height, double width, double length) {
        double area = (double) (height * width * length);
   
    }
 

    public void printWords(){
        System.out.println("This cube has a height of " + height + 
            " , a width of " + width + 
            " , and a depth of " + length + 
            ". Its total area is: " + area);
    }  
}

What I'm trying to do in printWords, is have it print the numbers type and show the area but it won't take my area from the math problem above it.

I don't know if you need to see the other class to understand what I'm doing so I'm just gonna post that too.

Lab5.java

import java.util.Scanner;

public static void main(String[] args) {
    
    Scanner scanner = new Scanner(System.in);
    
    System.out.println("Please type the Height, Width, and Length"
            + " of the cube you would like to see the area of."
            + " For example 10 5 2");
    
    // allows "scan(Hight, Width, and Length)" to be given a typed number
    double scanHeight = scanner.nextInt();
    double scanWidth = scanner.nextInt();
    double scanLength = scanner.nextInt();        
    
    Cube finalCube = new Cube();
    finalCube.height = scanHeight;
    finalCube.width = scanWidth;
    finalCube.length = scanLength;
    Cube.CalculateTotalArea(finalCube.height, finalCube.width, finalCube.length); 
    finalCube.printWords();
      
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

3 Answers3

0

You are making several mistakes here

  • Your code should not compile. In your printWords() you didn't define the area
  • You are assigning the calculated area to a local variable. So your print method does have scope to get that.

Change your Cube.java to this

public class Cube {

    public double height;
    public double width;
    public double length;

    public double area;

    public void calculateTotalArea() {
        this.area = this.height * this.width * this.length;
    }

    public void printWords() {
        System.out.println("This cube has a height of " + height +
                " , a width of " + width +
                " , and a depth of " + length +
                ". Its total area is: " + area);
    }
}

And call the calculate method by instance

finalCube.calculateTotalArea();
finalCube.printWords();
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Shafin Mahmud
  • 3,831
  • 1
  • 23
  • 35
0

Since, you are not storing the area calculated in any field in Cube class so when you are calling the printWords function, at that time, the value in the area is not defined.

The scope of the area variable is limited to CalculateTotalArea function. You can't use it in printWords.

Try this to solve the problem:

  1. Define a new field in the same class named area.
  2. In the CalculateTotalArea function, remove the double keyword and make area to this.area.
  3. Replace similarly in printWords function, area to this.area.
  4. Instead of calling as static function, use the object to call the printWords function.

finalCube.CalculateTotalArea(finalCube.height, finalCube.width, finalCube.length);

So, after the final changes, it looks like this:

public class Cube {

public double height;
public double width;
public double length;
public double area;

static void CalculateTotalArea(double height, double width, double length) {
    this.area = (double) (height * width * length);
   
}

public void printWords(){
    System.out.println("This cube has a height of " + height + 
            " , a width of " + width + 
            " , and a depth of " + length + 
            ". Its total area is: " + this.area);
   }  

}

And change your Lab5.java to this:

import java.util.Scanner;
public static void main(String[] args) {
    
    Scanner scanner = new Scanner(System.in);
    
    System.out.println("Please type the Height, Width, and Length"
            + " of the cube you would like to see the area of."
            + " For example 10 5 2");
    
    // allows "scan(Hight, Width, and Length)" to be given a typed number
    double scanHeight = scanner.nextInt();
    double scanWidth = scanner.nextInt();
    double scanLength = scanner.nextInt();        
    
    Cube finalCube = new Cube();
    finalCube.height = scanHeight;
    finalCube.width = scanWidth;
    finalCube.length = scanLength;
    finalCube.CalculateTotalArea(finalCube.height, finalCube.width, finalCube.length); 
    finalCube.printWords();
      
}

Other suggestions, just for good practices:

  1. Define a constructor for your Cube class.

    Class(int h, int w, int l){
    this.height = h;
    this.width = w;
    this.length = l;
    }

  2. While making a new instance of Cube, using this constructor.

Cube finalCube = new Cube(scanHeight, scanWidth, scanLength);

Sonu Sourav
  • 2,926
  • 2
  • 12
  • 25
0

Basically, you didn't declare an instance variable area in the Cube class. That's why you are facing error.

I have updated the code to this :

public class Cube {

    public double height;
    public double width;
    public double length;

    public double calculateTotalArea(double height, double width, double length) {
        return (height * width * length);

    }

    public void printWords() {
        System.out.println("This cube has a height of " + height + " , a width of " + width + " , and a depth of "
                + length + ". Its total area is: " + calculateTotalArea(height, width, length));
    }
}

Update main method to this :

public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Please type the Height, Width, and Length"
                + " of the cube you would like to see the area of." + " For example 10 5 2");

        // allows "scan(Hight, Width, and Length)" to be given a typed number
        double scanHeight = scanner.nextInt();
        double scanWidth = scanner.nextInt();
        double scanLength = scanner.nextInt();

        Cube finalCube = new Cube();
        finalCube.height = scanHeight;
        finalCube.width = scanWidth;
        finalCube.length = scanLength;
        finalCube.printWords();

    }

That's all.

Anish B.
  • 9,111
  • 3
  • 21
  • 41