6 Replies - 1097 Views - Last Post: 05 April 2013 - 03:00 PM Rate Topic: -----

#1 DHarrison  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-March 13

LandTract program and Demo program, Cannot find symbol

Posted 05 April 2013 - 02:11 PM

Make a LandTract class that has two fields, once for the tract's length and one for the width. The class should have a method that returns the tract's area, as well as an equals method and a toString method. Demonstrate the class the class in a program that asks the user to enter the dimensions for the two tracts of land. The program should display the area of each and indicate whether the tracts are of equal size.

Here's my LandTract program.
public class LandTract {

	private double tractLength;
	private double tractWidth;
	private double area;

	public LandTract()
	{
		area = 0.0;
	}
	
	public LandTract(double length, double width)
	{
		tractLength = length;
		tractWidth = width;
	}
	
	public double getLength(double length)
	{
	return length;
	}
	
	public double getWidth(double width)
	{
	return width;
	}
	
	public double getArea(double area, double length, double width)
	{
		area = length * width;
		return area;
	}
	
	public boolean equals(LandTract tract2)
	{
	return this.area == tract2.area;
	}
	
	public String toString(int area)
	{
		return "Area: " + area;
	}
}


Compiles fine. And here's my LandTractDemo program
import java.util.Scanner;

   public class LandTractDemo
   {
      public static void main(String[] args)
      {
      	double length;          
         double width;        
         
      
         Scanner keyboard = new Scanner(System.in);
      
      
         System.out.print("Enter the length of Tract 1: ");
         length = keyboard.nextDouble();
			System.out.print("Enter the width of Tract 1: ");
         width = keyboard.nextDouble();
      
      
         LandTract tract1 = new LandTract(length, width);
      
      
         System.out.print("Enter the length of tract 2: ");
         length = keyboard.nextDouble();
			System.out.print("Enter the width of tract 2: ");
         width = keyboard.nextDouble();
      
      
         LandTract tract2 = new LandTract(length, width);
      
      
         System.out.println("\nTract 1 Area: " + tract1);
         System.out.println("Tract 2 Area: " + tract2 + "\n");
      
      // Determine whether the two tracts are equal
         if (tract1.equals(tract2))
            System.out.println("The tracts are the same size");
         else
            System.out.println("The tracts are NOT the same size");
      }
   }

I get this error

Quote

LandTractDemo.java:20: error: cannot find symbol
LandTract tract1 = new LandTract(length, width);
^
symbol: class LandTract
location: class LandTractDemo
LandTractDemo.java:20: error: cannot find symbol
LandTract tract1 = new LandTract(length, width);
^
symbol: class LandTract
location: class LandTractDemo
LandTractDemo.java:29: error: cannot find symbol
LandTract tract2 = new LandTract(length, width);
^
symbol: class LandTract
location: class LandTractDemo
LandTractDemo.java:29: error: cannot find symbol
LandTract tract2 = new LandTract(length, width);
^
symbol: class LandTract
location: class LandTractDemo
4 errors



It says cannot find symbol, but shouldn't LandTract tract1 = new LandTract make it work? Any suggestion are appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: LandTract program and Demo program, Cannot find symbol

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: LandTract program and Demo program, Cannot find symbol

Posted 05 April 2013 - 02:24 PM

Are both source files in the same directory? If not, put them in the same directory.
Was This Post Helpful? 1
  • +
  • -

#3 DHarrison  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-March 13

Re: LandTract program and Demo program, Cannot find symbol

Posted 05 April 2013 - 02:29 PM

Alright fixed, runs now. But I'm getting this output.

Quote

Tract 1: LandTract@8a64642
Tract 2: LandTract@5c4e9b7d

The tracts are the same size

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: LandTract program and Demo program, Cannot find symbol

Posted 05 April 2013 - 02:36 PM

When you're getting output like that - alphanumerics with an '@' symbol - you're printing the object's memory location. tract1 and tract2 are references to LandTract objects. Write a LandTract.toString() method that prints the part(s) of the object you want to print.
Was This Post Helpful? 0
  • +
  • -

#5 DHarrison  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-March 13

Re: LandTract program and Demo program, Cannot find symbol

Posted 05 April 2013 - 02:40 PM

View PostGregBrannon, on 05 April 2013 - 02:36 PM, said:

When you're getting output like that - alphanumerics with an '@' symbol - you're printing the object's memory location. tract1 and tract2 are references to LandTract objects. Write a LandTract.toString() method that prints the part(s) of the object you want to print.


Something similar to this?

public String toString()
	{
		return "Area: " + area;
	}

That eliminates the @'s, but now the output is

Quote

Tract 1: Area: 0.0
Tract 2: Area: 0.0

The tracts are the same size

Was This Post Helpful? 0
  • +
  • -

#6 DHarrison  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-March 13

Re: LandTract program and Demo program, Cannot find symbol

Posted 05 April 2013 - 02:56 PM

Fixed it!! Never created anything about area in the demo class. Output now displays correctly.

Quote

Tract 1: Area: 60.0
Tract 2: Area: 40.0

The tracts are NOT the same siz

Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: LandTract program and Demo program, Cannot find symbol

Posted 05 April 2013 - 03:00 PM

Do you call the getArea() method to calculate the area? Actually, you should rename the existing getArea() to setArea() with the same parameters, call it so that the area is calculated, and then write a getArea() method that simply returns 'area'. Then when you call the toString() method, the area will be something other than zero.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1