question: The program should prompt the user to enter data for the width and height of the 2 rectangles and use the setWidth() and setLength() methods to store this data in the instance variables.

driver

import javax.swing.*;
public class Room
{
	public static void main(String[ ] args)
	{
	      double sq;
             String input;
             String input2;
             double length;
             double width;
             
              input = JOptionPane.showInputDialog("Enter the length");
             length = Double.parseDouble(input);
             
             input2 = JOptionPane.showInputDialog("Enter the width");
             width = Double.parseDouble(input2);
                 
             sq = Arearect.getArea();

             JOptionPane.showMessageDialog(null,"The area is " + Arearect.getArea());
}
}

instantiable

public class Arearect
{
        public void setWidth(double width)
        {
             width = width;
        }
        
         public void setLength(double length)
         {
         length = length;
        }
        
        public static double getArea()
        {
         double area;
		area=length * width;  //<< problem here

               return area;
            }
}

how to call the area and display it to driver?
or my code all mess up?

you didn't set value for AreaRect...that is the problem...

you van do like this if this simple program...

import javax.swing.*;
public class Room
{
	public static void main(String[ ] args)
	{
	      double sq;
             String input;
             String input2;
             double length;
             double width;
             
              input = JOptionPane.showInputDialog("Enter the length");
             length = Double.parseDouble(input);
             
             input2 = JOptionPane.showInputDialog("Enter the width");
             width = Double.parseDouble(input2);
                 
             sq =getArea(length ,width ) ;//Arearect.getArea();

             JOptionPane.showMessageDialog(null,"The area is " + //Arearect.getArea());
}




public static double getArea(int length ,int width )

{

double area;

area=length * width; //<< problem here

 

return area;

}

else

else

import javax.swing.*;
public class Room
{
	public static void main(String[ ] args)
	{
	      double sq;
             String input;
             String input2;
             double length;
             double width;
             Arearect areaRect=new Arearect();
              input = JOptionPane.showInputDialog("Enter the length");
             length = Double.parseDouble(input);
             areaRect.setlength (length );
             input2 = JOptionPane.showInputDialog("Enter the width");
             width = Double.parseDouble(input2);
                 areaRect.setwidth  (width  );
             sq =areaRect.getArea();

             JOptionPane.showMessageDialog(null,"The area is " + sq );
}


}

i understand it now :$
thanks ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.