6 Replies - 1011 Views - Last Post: 22 May 2016 - 08:40 AM Rate Topic: -----

#1 kmerr98277   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-May 16

Java programming problem: cannot find symbol

Posted 21 May 2016 - 08:03 PM

Create a class named Circle with fields named radius, diameter, and area. Include a constructor that sets the radius to 1 and calculates the other two values. Also include methods named setRadius () and getRadius (). The setRadius () method not only sets the radius, it also calculates the other two values. (Remember that the diameter of a circle is twice the radius, and the area of a circle is pi multiplied by the square of the radius.) Save the class as Circle.java

Create a class named TestCircle whose main () method declares several Circle objects. Using the setRadius () method, assign one Circle a small radius value, and assign another a larger radius value. Do not assign a value to the radius of the third circle. Instead, retain the value assigned at construction. Display all the values for all the Circle objects. Save the application as TestCircle.java

Here is what I have so far:

package circle;

public class Circle 
{
    private double radius;
    private double diameter;
    private double area;
    
    public circle()
    {
        radius = 1;
        diameter = radius * 2;
        area = 3.14 * radius * radius;
    }
    public void setRadius(double rad)
    {
        radius = rad;
        diameter = radius * 2;
        area = 3.14 * radius * radius;
    }
    public double getRadius()
    {
        return radius;
    }
    public void display()
    {
        System.out.println("Radius: " + radius);
        System.out.println("Diameter: " + diameter);     
        System.out.println("Area: " + area);
    }    
}
Public class TestCircle
{
    public static void main(String[] args)
    {
        Circle obj1 = new Circle();
        Circle obj2 = new Circle();
        Circle obj3 = new Circle();
        obj1.setRadius(4.2);
        obj2.setRadius(10.8);
        
        System.out.println("Object1");
        obj1.Display();
        System.out.println("Object2");
        obj2.display();
        System.out.println("Object3");      
        obj3.display();
        
    }
}



I'm not sure how to set up the method properly

Errors:
Invalid method declaration - line 9
class, interface, or enum expected - line 41
cannot find symbol, symbol method Display(), location: variable obj1 of type circle - line 50

Is This A Good Question/Topic? 0
  • +

Replies To: Java programming problem: cannot find symbol

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11041
  • View blog
  • Posts: 18,857
  • Joined: 19-March 11

Re: Java programming problem: cannot find symbol

Posted 21 May 2016 - 08:06 PM

This is not a problem with NetBeans, it's a java programming problem. Your line numbers don't match up with the numbers in the error output, so it's a little hard to see what's going on here, but I see a few capitalization errors. Your constructor

public circle()



should have a capital Circle, to match the class name,

Public class TestCircle



should be lowercase "public" (all java keywords are lowercase)

and at

obj1.Display();



Display should be lowercase (since that's how you've defined the function)

Go ahead and fix those things and then see what remains to fix.
Was This Post Helpful? 0
  • +
  • -

#3 kmerr98277   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-May 16

Re: Java programming problem: cannot find symbol

Posted 21 May 2016 - 08:52 PM

Thanks for the corrections. Now the only error I have is the public class TestCircle needs to be declared in its own TestCircle.java file. When I let netbeans create the file it replaces Circle.java and I am left with the same error for public class Circle.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11041
  • View blog
  • Posts: 18,857
  • Joined: 19-March 11

Re: Java programming problem: cannot find symbol

Posted 21 May 2016 - 09:01 PM

I think you probably ought to stop letting NetBeans do things for you, at least long enough to understand what's going on and why.

Putting it simply, a public class in java must be declared in a file whose name matches the class name, with the extension ".java". This is how java finds the classes that you import. So if you have a public class called Circle, it must appear in Circle.java, and if you have a public class called TestCircle, it must appear in TestCircle.java.
Was This Post Helpful? 0
  • +
  • -

#5 kmerr98277   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-May 16

Re: Java programming problem: cannot find symbol

Posted 21 May 2016 - 09:10 PM

Ok. That fixed it. I created another file TestCircle.java and put the code pertaining to TestCircle in there. I thought all of the source code for both classes had to be on the same page of code for it to run.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,532
  • Joined: 12-December 12

Re: Java programming problem: cannot find symbol

Posted 22 May 2016 - 12:56 AM

Just to note that it is possible for a file to contain more than one class, but only one of them can be public and this one dictates the name of the file.

(It is also possible for a class to contain nested classes.)

At an early stage, though, and for small projects, you will probably continue to create a separate file for each class. (Some people would say that each class (not including nested classes) should always be in a separate file ;) )
Was This Post Helpful? 1
  • +
  • -

#7 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11041
  • View blog
  • Posts: 18,857
  • Joined: 19-March 11

Re: Java programming problem: cannot find symbol

Posted 22 May 2016 - 08:40 AM

I want to encourage you to spend some time looking at the structure of a java application - if not now, then soon. This is stuff that's largely obscured by your choice of tools, but it's very important to understand it. The Tutoracles include a good overview of how this all works. It's not the easiest reading in the world, but if you don't understand how things like the classpath work and how java finds classes it's going to make your life more difficult going forward, usually at inconvenient times.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1