import javax.swing.JOptionPane;
public class Rectangle
{
private double length;
private double width;
public Rectangle()
{
length = width = 1;
}
public void setRectangle()
{
setLength();
setWidth();
}
public void setLength()
{
if ( len > 0 && len <= 20.0 )
length = len;
else
{
length = 0;
JOptionPane.showMessageDialog( null,
"Invalid numbers outside of acceptable range have been entered. Range is from > 0 to <= 20.0. Number entered must be a floating decimal type.",
"Input Error Message", JOptionPane.PLAIN_MESSAGE );
}
}
public void setWidth()
{
if ( wid > 0 && wid <= 20.0 )
width = wid;
else
{
width = 0;
JOptionPane.showMessageDialog( null, "Invalid numbers outside of acceptable range have been entered. Range is from > 0 to <= 20.0. Number entered must be a floating decimal type.",
"Input Error Message", JOptionPane.PLAIN_MESSAGE );
}
}
public Boolean isSquare()
{
if ( length - width < 0.001 || width - length < 0.0001 )
return true;
else
return false;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double getArea()
{
return length * width;
}
public double getPerimeter()
{
return 2 * (length + width);
}
}
Here is the code I wrote for my application to test the class above:
import javax.swing.JOptionPane;
public class RectangleTest
{
public static void main( String[] args )
{
Rectangle box = new Rectangle();
String firstNum = JOptionPane.showInputDialog( "Enter length in inches: " );
String secondNum = JOptionPane.showInputDialog( "Enter width in inches: " );
for(int count = 1; count <= 3; count++)
{
double len = Double.parseDouble( firstNum );
double wid = Double.parseDouble( secondNum );
System.out.println( "Area for rectangle " + count + " is " + box.getArea() );
System.out.println( "Perimeter for rectangle " + count + " is " + box.getPerimeter() );
if (box.isSquare() )
System.out.println( "Rectangle " + count + " is a square." );
else
System.out.println( "Rectangle " + count + " is not a square." );
firstNum = JOptionPane.showInputDialog( "Enter length in inches: " );
secondNum = JOptionPane.showInputDialog( "Enter width in inches: " );
}
}
}
The application compiled and created a class. The Class Rectangle, did not. Errors change (increase) with everything I try to do to fix it. This is my introductory course on Java, and only the second assignment therein. So, I am shooting blind on this thing...and not used to object programming...logic seems a lot more obscured than merely fuzzy. Any suggestions would be appreciated.
Thanks.

New Topic/Question
Reply



MultiQuote




|