4 Replies - 582 Views - Last Post: 07 October 2012 - 01:53 PM Rate Topic: -----

#1 need_java_help_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 12

Cannot get a created class to compile

Posted 07 October 2012 - 12:36 PM

Particulars: Write a Java class for a Rectangle object. Class has double attributes 'length' and 'width'. It has member methods to calc perimeter and area of the rectangle. It has set and get methods for both length and width. Set methods verify that length and width are each double type numbers > 0 and <= 20.0. If invalid value entered by user for either length or width, then you set the values to these data members to '0', and display an error message explaining invalid data entered and what the program is doing. Are to use a predicate method, member Boolean, to determine if the rectangle created is a square (if length and width differ by less than .0001). All reading of data from keyboard will be performed by Rectangle methods. Also write an application to test this class. It will ask user to enter length and width using JOptionPane methods, as well as display appropriate results with the same method type. The output will be perimeter and area of rectangle created, and whether or not the rectangle is considered a square. The program will run long enough to enter information and receive results on three different rectangles, one at a time. Here is the code I wrote for the class:

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Cannot get a created class to compile

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Cannot get a created class to compile

Posted 07 October 2012 - 12:51 PM

In the Rectangle setLength() and setWidth() methods, a parameter would normally be passed. This would also make sense with what you've written, because as it is now, len in setLength() and wid in setWidth() are undefined. You should be getting an errors that say the symbols len and wid cannot be found. (Why not just use variable names length and width?)

The signature should look like:

public void setLength( int length ) . . .

You'll then have to modify setRactangle() to pass the parameters to setLength() and setWidth(). There may be other things to fix as I haven't completely worked those through. If you still have errors you can't figure out after making those corrections, come back and post the errors, copied and pasted just as they appear at your end.

This post has been edited by GregBrannon: 07 October 2012 - 12:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 need_java_help_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 12

Re: Cannot get a created class to compile

Posted 07 October 2012 - 01:25 PM

Greg,
Thanks for the information. I modified the code in Rectangle as you suggested. That left me with 2 errors both having to do with the JOptionPane issue...I had been having some errors with that earlier as well, so I modified those two ouput display statements and feel that I may have messed them up a little too. The errors I got were identical, one for line 32 and one for line 44. They are both as follows:

Rectangle.java:32: error: no suitable method found for showMessageDialog(<null>,
String,String)
JOptionPane.showMessageDialog( null,
^
method JOptionPane.showMessageDialog(Component,Object,String,int,Icon) is no
t applicable
(actual and formal argument lists differ in length)
method JOptionPane.showMessageDialog(Component,Object,String,int) is not app
licable
(actual and formal argument lists differ in length)
method JOptionPane.showMessageDialog(Component,Object) is not applicable
(actual and formal argument lists differ in length)

Here is a copy of my changed code:

import javax.swing.JOptionPane;

public class Rectangle
{
	private double length;
	private double width;
	
	public Rectangle()
	{
		length = width = 1.0;
	}
	
	public void setRectangle()
	{
		setLength(length);
		setWidth(width);
	}
	
	public void setLength(double length)
	{
		if ( length <= 0 && length > 20.0 )
		{
			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" );
		}
		
	}
	
	public void setWidth(double width)
	{
		if ( width <= 0 && width > 20.0 )
		{
			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" );
		}
	}
	
	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);
	}
}



Should I just do a simple output display on the screen for those issues instead of utilizing the JOptionPane method my requirements ask for, or, is there something about that method concept I am not now getting correct...or likely wasn't before either?

Thanks,

Sam
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Cannot get a created class to compile

Posted 07 October 2012 - 01:37 PM

The error message is indicating that you're attempting to call a JOptionPane method that doesn't exist, at least not in the same form that you're calling. You can consult the JOptionPane API to review the showMessageDialog() methods that do exist and adjust your calls to conform to one of those.
Was This Post Helpful? 1
  • +
  • -

#5 need_java_help_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 12

Re: Cannot get a created class to compile

Posted 07 October 2012 - 01:53 PM

View PostGregBrannon, on 07 October 2012 - 01:37 PM, said:

The error message is indicating that you're attempting to call a JOptionPane method that doesn't exist, at least not in the same form that you're calling. You can consult the JOptionPane API to review the showMessageDialog() methods that do exist and adjust your calls to conform to one of those.


Greg,

That did it.

Thanks for the help.

Sam
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1