Jump to content

Calculation Of Two Classes?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
reachpradeep

reachpradeep

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
if you see the following. when i enter a number for width in the size class it doesn't do the calculation:

public class RoomTest 

{

    public static void main(String[] args)

    {

       String displaySize;


	    Size sizeA = new Size();


	    displaySize = sizeA.getSize();

	    System.out.println(displaySize);

	}

}


import javax.swing.*;

public class Size

{

 	private double width;

	private double length;


	public Size()

	{

	     String widthA = JOptionPane.showInputDialog(null, "Enter width:");

	     double width = Double.parseDouble(widthA);

	     length = 5.0;

	}

	

	public String getSize()

	{

	     double area = (width*length);

	     String sizeString = ("The area is: " + area);

	     return sizeString;

	}

}

It doesn't taking the number i entered to do the calculation
any kind of Help appreciated
THANKS

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
In the Size() class on line 4 you declared width as an instance variable and on line 10 you redeclared width as a local variable and therefore the scope of that variable is only in that block of code rather than the entire class. Remove the double in front of width on line 10 and it will work