7 Replies - 1945 Views - Last Post: 27 October 2008 - 02:09 PM Rate Topic: -----

#1 Alleluia707  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-October 08

Error: Cannot find symbol

Post icon  Posted 27 October 2008 - 01:22 PM

Hi, I'm a total noob. I wrote a program for my Intro to Java class that asks a user for the width of a square and then calculates the area and perimeter. But I kept getting this error message:

TestSquare.java:42: cannot find symbol
symbol : method setWidth(double)
location : class Square
sq.setWidth(20.0);
^
1 error

The error is with the "." symbol. I don't know what I'm doing wrong, here is my code:


Square.java

public class Square
{

	private double length;	 // The square's length
	private double width;	  // The square's width



	/**
	 * Constructor
	 */

	public Square()
	{
		length = 0;
		width = 0;
	}
	public Square(double len)
	{
		length = len;
		width = 0;
	}



	/**
	 * The setLength method accepts an argument that
	 * is stored in the length field.
	 */

	public void setLength(double len)
	{
		length = len;
	}



	/**
	 * The set method accepts an argument that is
	 * stored in the length fields
	 */

	public void set(double len)
	{
		length = len;
		width = 0;
	}



	/**
	 * The getLength method returns the value stored
	 * in the length field.
	 */

	public double getLength()
	{
		return length;
	}



	/**
		* The getWidth method returns the value
	* stored in the width field.
	*/

	public double getWidth()
	{
		return width;
	}



	/**
	* The getArea method calculates and returns the
	* value of the area.
	*/

	public double getArea()
	{
		return length * width;
	}



	/**
	* The getPerimeter method calculates and returns
	* the value of the perimeter.
	*/

	public double getPerimeter()
	{
		return 4 * width;
	}


}




TestSquare.java

import java.util.Scanner;   // Needed for the Scanner class


/**
* This program tests the Square class.
*/


public class TestSquare
{

   public static void main(String[] args)
   {

	Square sq;		   // Reference to the Square object
	double length,	   // Square length
		width,		 // Square width
		area,		// Area of the Square
		perimeter;   // Perimeter of the Square


	// Create a Scanner object for keyboard input.
	Scanner keyboard = new Scanner(System.in);


	// Get and store the square's width
	System.out.print("What is the width of the "
						 + "square? ");
	width = keyboard.nextDouble();


	// Create a Square object
	sq = new Square();
	

	// Set the square's length to 10 and width to 20 
	sq.setLength(10.0);	
	sq.setWidth(20.0);
		
	
	// Display the area and perimeter of the square
	System.out.println("The area of the square is "
						 + sq.getArea());
	System.out.println("The perimeter of the square is "
						 + sq.getPerimeter());

   }


}


Is This A Good Question/Topic? 0
  • +

Replies To: Error: Cannot find symbol

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Error: Cannot find symbol

Posted 27 October 2008 - 01:27 PM

View PostAlleluia707, on 27 Oct, 2008 - 01:22 PM, said:

Hi, I'm a total noob. I wrote a program for my Intro to Java class that asks a user for the width of a square and then calculates the area and perimeter. But I kept getting this error message:

TestSquare.java:42: cannot find symbol
symbol : method setWidth(double)
location : class Square
sq.setWidth(20.0);
^
1 error

The error is with the "." symbol. I don't know what I'm doing wrong, here is my code:


Square.java

public class Square
{

	private double length;	 // The square's length
	private double width;	  // The square's width



	/**
	 * Constructor
	 */

	public Square()
	{
		length = 0;
		width = 0;
	}
	public Square(double len)
	{
		length = len;
		width = 0;
	}



	/**
	 * The setLength method accepts an argument that
	 * is stored in the length field.
	 */

	public void setLength(double len)
	{
		length = len;
	}



	/**
	 * The set method accepts an argument that is
	 * stored in the length fields
	 */

	public void set(double len)
	{
		length = len;
		width = 0;
	}



	/**
	 * The getLength method returns the value stored
	 * in the length field.
	 */

	public double getLength()
	{
		return length;
	}



	/**
		* The getWidth method returns the value
	* stored in the width field.
	*/

	public double getWidth()
	{
		return width;
	}



	/**
	* The getArea method calculates and returns the
	* value of the area.
	*/

	public double getArea()
	{
		return length * width;
	}



	/**
	* The getPerimeter method calculates and returns
	* the value of the perimeter.
	*/

	public double getPerimeter()
	{
		return 4 * width;
	}


}




TestSquare.java

import java.util.Scanner;   // Needed for the Scanner class


/**
* This program tests the Square class.
*/


public class TestSquare
{

   public static void main(String[] args)
   {

	Square sq;		   // Reference to the Square object
	double length,	   // Square length
		width,		 // Square width
		area,		// Area of the Square
		perimeter;   // Perimeter of the Square


	// Create a Scanner object for keyboard input.
	Scanner keyboard = new Scanner(System.in);


	// Get and store the square's width
	System.out.print("What is the width of the "
						 + "square? ");
	width = keyboard.nextDouble();


	// Create a Square object
	sq = new Square();
	

	// Set the square's length to 10 and width to 20 
	sq.setLength(10.0);	
	sq.setWidth(20.0);
		
	
	// Display the area and perimeter of the square
	System.out.println("The area of the square is "
						 + sq.getArea());
	System.out.println("The perimeter of the square is "
						 + sq.getPerimeter());

   }


}



Does the code compile? If it does then where does it receive the error? If it doesn't compile then I recommend using eclipse and it should get it at least able to compile.
Was This Post Helpful? 0
  • +
  • -

#3 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Error: Cannot find symbol

Posted 27 October 2008 - 01:30 PM

The problem is that you haven't implemented a setWidth-method.

You have a setLength and a set method. I think you should rewrite the set-method and call it setWidth. Most likely you just forgot about it.
Was This Post Helpful? 0
  • +
  • -

#4 Alleluia707  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-October 08

Re: Error: Cannot find symbol

Posted 27 October 2008 - 01:33 PM

View Postmarkhazlett9, on 27 Oct, 2008 - 01:27 PM, said:

View PostAlleluia707, on 27 Oct, 2008 - 01:22 PM, said:

Hi, I'm a total noob. I wrote a program for my Intro to Java class that asks a user for the width of a square and then calculates the area and perimeter. But I kept getting this error message:

TestSquare.java:42: cannot find symbol
symbol : method setWidth(double)
location : class Square
sq.setWidth(20.0);
^
1 error

The error is with the "." symbol. I don't know what I'm doing wrong, here is my code:


Square.java

public class Square
{

	private double length;	 // The square's length
	private double width;	  // The square's width



	/**
	 * Constructor
	 */

	public Square()
	{
		length = 0;
		width = 0;
	}
	public Square(double len)
	{
		length = len;
		width = 0;
	}



	/**
	 * The setLength method accepts an argument that
	 * is stored in the length field.
	 */

	public void setLength(double len)
	{
		length = len;
	}



	/**
	 * The set method accepts an argument that is
	 * stored in the length fields
	 */

	public void set(double len)
	{
		length = len;
		width = 0;
	}



	/**
	 * The getLength method returns the value stored
	 * in the length field.
	 */

	public double getLength()
	{
		return length;
	}



	/**
		* The getWidth method returns the value
	* stored in the width field.
	*/

	public double getWidth()
	{
		return width;
	}



	/**
	* The getArea method calculates and returns the
	* value of the area.
	*/

	public double getArea()
	{
		return length * width;
	}



	/**
	* The getPerimeter method calculates and returns
	* the value of the perimeter.
	*/

	public double getPerimeter()
	{
		return 4 * width;
	}


}




TestSquare.java

import java.util.Scanner;   // Needed for the Scanner class


/**
* This program tests the Square class.
*/


public class TestSquare
{

   public static void main(String[] args)
   {

	Square sq;		   // Reference to the Square object
	double length,	   // Square length
		width,		 // Square width
		area,		// Area of the Square
		perimeter;   // Perimeter of the Square


	// Create a Scanner object for keyboard input.
	Scanner keyboard = new Scanner(System.in);


	// Get and store the square's width
	System.out.print("What is the width of the "
						 + "square? ");
	width = keyboard.nextDouble();


	// Create a Square object
	sq = new Square();
	

	// Set the square's length to 10 and width to 20 
	sq.setLength(10.0);	
	sq.setWidth(20.0);
		
	
	// Display the area and perimeter of the square
	System.out.println("The area of the square is "
						 + sq.getArea());
	System.out.println("The perimeter of the square is "
						 + sq.getPerimeter());

   }


}



Does the code compile? If it does then where does it receive the error? If it doesn't compile then I recommend using eclipse and it should get it at least able to compile.

It doesn't compile. The Square class is supposed to be demonstrated by the TestSquare. But I keep getting that error message when I try to compile the TestSquare file.
Was This Post Helpful? 0
  • +
  • -

#5 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Error: Cannot find symbol

Posted 27 October 2008 - 01:38 PM

Also, the perimeter is 2 * (length + width) not 4 * width
Was This Post Helpful? 1
  • +
  • -

#6 Alleluia707  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-October 08

Re: Error: Cannot find symbol

Posted 27 October 2008 - 01:45 PM

View PostGloin, on 27 Oct, 2008 - 01:30 PM, said:

The problem is that you haven't implemented a setWidth-method.

You have a setLength and a set method. I think you should rewrite the set-method and call it setWidth. Most likely you just forgot about it.

Well, I changed the set to setWidth and the program compiles. Now there's a new problem. When I'm prompted to enter the width the program produces a perimeter of 0 and an area of 0. It should display any other answer but 0. How do I fix this?
Was This Post Helpful? 0
  • +
  • -

#7 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Error: Cannot find symbol

Posted 27 October 2008 - 01:53 PM

How did you implement the method?
This used to be your set-method,

public void set(double len)
	{
		length = len;
		width = 0;
	}



As you can see it won't let you set the width the way you want. It always set the width to 0 and since x * 0 = 0, the area will always be 0.

The method should look like this:

public void setWidth(double wid)
	{
		width = wid;
	}



Btw, did you change the perimeter-method like I said?
Was This Post Helpful? 1
  • +
  • -

#8 Alleluia707  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-October 08

Re: Error: Cannot find symbol

Posted 27 October 2008 - 02:09 PM

View PostGloin, on 27 Oct, 2008 - 01:53 PM, said:

How did you implement the method?
This used to be your set-method,

public void set(double len)
	{
		length = len;
		width = 0;
	}



As you can see it won't let you set the width the way you want. It always set the width to 0 and since x * 0 = 0, the area will always be 0.

The method should look like this:

public void setWidth(double wid)
	{
		width = wid;
	}



Btw, did you change the perimeter-method like I said?

Yes, it works now. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1