Hi all, I have an assignment to hand in tomorrow for programming and would appreciate any help that is given. I have done a couple of years programming with VB but java is like on a completely level compared to it.
Ok, so basically I have to program an application that will have a constructor to create a circle with a unit radius. The method will then return
- The radius
- The area
- The circumference
- The total area of the object and another circle
- The total circumference of the object and another circle

I’ve practically finished with the program but keeps giving me 1 error at the end that I can’t seem to solve.
My code for the method is here:

//Circle
//An application that can create a circle using the radius given from the user and to calculate the
//area, circumference of it and to display it to the user.

import java.io.*;
public class Circle {
    public int radiusInt;
    public String radiusStringInput;
   public void main(String[] args) throws IOException {

// Create BufferedReader instance to enable input from keyboard
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader keyboardInput = new BufferedReader(input);

System.out.println("please enter the radius:");
radiusStringInput = keyboardInput.readLine();
radiusInt = Integer.parseInt(radiusStringInput);
}
   public Circle(int x, int y,int r){
	r = radiusInt;
	x = radiusInt *2;
	y= radiusInt * 2;
	       }
public int radiusInt(){
	    radiusInt = radiusInt;
	    return (radiusInt);
    }
 public Double getCircumference(){
	Double Circ = 2 * 3.14 * radiusInt;
	return (Circ);
}
    public Double getArea(){
    double Area  =  3.14 * radiusInt * radiusInt;
    return (Area);
}
}
[B]My code for the constructor is here:[/B]
import java.io.*;
public class CircleApp {
public static void main(String[] args) {
  Circle Circle = new Circle(Circle.radiusInt);
System.out.println("Circumference is " + Circle.getCircumference());
System.out.println("The Area is:" + Circle.getArea());
System.out.println("The Radius is:" + Circle.radiusInt);
int radiusB = 5;
Circle CircleB = new Circle(radiusB);
double circleTotalArea =  3.14* radiusB * radiusB;
double circleTotalCircumference = 2 *  3.14 * radiusB;
System.out.println("Circumference is " + Circle.getCircumference());
System.out.println("The Area is:" + Circle.getArea());
System.out.println("The Radius is:" + Circle.radiusInt);
System.out.println("The Total Area is:" + circleTotalArea);
System.out.println("The Circumference is:" + circleTotalCircumference);
	}
}

Recommended Answers

All 29 Replies

please put code in code tags, and what is the error?

And what is that error? It should indicate the nature of the problem and the line number on which it occurs.

One thing I would point out is the missing parenthesis on this method call Circle.radiusInt

sorry my bad, its this part here from top of the constructor code

public class CircleApp {

public static void main(String[] args) {


Circle Circle = new Circle (Circle.radiusInt);
Error is: cannot find symbol


System.out.println("Circumference is " + Circle.getCircumference());
System.out.println("The Area is:" + Circle.getArea());
System.out.println("The Radius is:" + Circle.radiusInt);

I've added and edited it so many times just can't seem to get this one right. it needs to take the value of the radius to create a circle and as far as i know this is the method of doing it.

then just as ezzaral stated its the radiusint getter

also i would suggest you to name it as

public int getRadiusInt()

just as your other ones have been named

What they suggested above isn't your problem (well, it is a problem, but its not your error). Your problem is that you can't name your variable with the class name. Circle Circle is invalid because Circle is the class name. The word after the first Circle should be your variable name, which cannot be the name of a class or a reserved word (such as int). For example, you could use

Circle myCircle = new Circle();


However, you should also consider the fact that your code is confusing. You have a variable named radiusInt and you also have a method named radiusInt(). This won't confuse the compiler, since the compiler knows methods are called with parentheses, but it will confuse people. Method names should be descriptive, for example, a method that returns your radiusInt should be called getRadiusInt, like the guy above me said.

Yeah, I didn't even look at that line. The declaration is invalid and even if that is fixed, you can't use a method on that object in it's constructor call - because it doesn't exist yet.

I didn't even notice that, good catch Ezzaral. Also, AceAtch, take a look at the comments I put for your constructor.

public Circle(int x, int y,int r){
r = radiusInt; //This makes r have the value that was in radiusInt
x = radiusInt *2; //this makes x have 2 * the value that was in radiusInt
y= radiusInt * 2; //this makes y have 2 * the value that was in radiusInt


//You probably meant to do this:
radiusInt = r;

//The other code you have doesn't make any sense, since 'Circle' doesn't have any
//variables other than radiusInt

}

I don't think you're understanding the basic idea of how things work. I'll write something real quick to try to help you out. Then, you can try to apply the ideas I give you to your own code.

public class Rectangle{
private int height;
private int width;

public static void main(String[] args){

Rectangle rect = new Rectangle(1, 5);
System.out.println(rect.getWidth()); // prints 5
rect.setWidth(3);
System.out.println(rect.getWidth()); //prints 3


}


public int getWidth(){
return width;
}

public int getHeight(){
return height;
}

public void setWidth(int wdth){
this.width = wdth;
}

//Calling this method creates a new Rectangle object,
//which will have its height and width set to the h and w 
//you pass in.
public Rectangle(int h, int w){
height = h;
width = w;
}

/**Note, this method has the SAME method header as the method below
*and the compiler will NOT let you declare the same method twice.
*However, this method does the exact same thing as the one above.
*The reason? Because when you want to create an object, you 
*use a constructor (like this one). Where I have this.height, 
*saying 'this' means I am referring to the height variable of
*the object I am creating, NOT the height that got passed in (the one in parens).
*/
public Rectangle(int height, int width){
this.height = height;
this.width = width;
}


}

thanks for the replys! i have changed all of my variables to "myCircle" instead but it still get stuck on that very same line, also i have created a "getradius" public integer. which hasnt helped much.. :/

Post the revised code.

ok cheers i understand BestJewSinceJC, i will try and impletment that into my program, thanks for the help guys, will post later on with the conclusion :)

thanks for the replys! i have changed all of my variables to "myCircle" instead but it still get stuck on that very same line, also i have created a "getradius" public integer. which hasnt helped much.. :/

The idea is that every time you do the following:

Circle whateverName = new Circle();

You are creating a new Object. So you should have a different name every time you create a different Circle. For example,

//Two good Circles
Circle myCircle = new Circle();
Circle myOtherCircle = new Circle();

//Wrong
Circle myCircle = new Circle();
Circle myCircle = new Circle();

The idea is that every time you do the following:

Circle whateverName = new Circle();

You are creating a new Object. So you should have a different name every time you create a different Circle. For example,

//Two good Circles
Circle myCircle = new Circle();
Circle myOtherCircle = new Circle();

//Wrong
Circle myCircle = new Circle();
Circle myCircle = new Circle();

I did do that i have reciently changed:
Circle Circle = new Circle() to Circle myCircle = new Circle()
and my second circle was:
Circle CircleB = new Circle()

Oh ok, I misunderstood you then. Thats fine. Just post up your code after you're finished editing it if you still have any problems.

I've tryed my absolute best on this but this is what i got so far.

the method code:

import java.io.*;
public class myCircle {
   public int radiusInt;
    public String radiusStringInput;
public void main(String[] args) throws IOException {

// Create BufferedReader instance to enable input from keyboard
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader keyboardInput = new BufferedReader(input);

System.out.println("please enter the radius:");
radiusStringInput = keyboardInput.readLine();
radiusInt = Integer.parseInt(radiusStringInput);
}
	   public myCircle(int r){
        radiusInt = r;
    }
   private void setRadius(int r){
        radiusInt = r;
    }
	public int getRadius(){
	return (radiusInt);
    }
    public double getCircumference(){
	Double Circ = 2 * 3.14 * radiusInt;
	return (Circ);
}
    public Double getArea(){
    double Area  =  3.14 * radiusInt * radiusInt;
    return (Area);
}
}

the constructor code:

//CircleApp
//An application that can create a circle using the radiues given from the user and to calculate the
//area, cicumference of it and to display it to the user.

//02/10/08

import java.io.*;

public class CircleApp {

public static void main(String[] args) {


    Circle myCircle = Circle(myCircle.r);

System.out.println("Circumference is " + myCircle.getCircumference());
System.out.println("The Area is:" + myCircle.getArea());
System.out.println("The Radius is:" + myCircle.getRadius);





Circle CircleB = new CircleB (5);


int circleTotalArea = myCircle.getRadius + (3.14* 5 * 5);
double circleTotalCircumference = myCircle.getRadius + (2 *  3.14 * 5);


System.out.println("The Circumference is " + myCircle.getCircumference());
System.out.println("The Area is:" + myCircle.getArea());
System.out.println("The Radius is:" + myCircle.getRadius);
System.out.println("The Total Area is:" + circleTotalArea);
System.out.println("The Total Circumference is:" + circleTotalCircumference);
	}

}

the errors are:
CircleApp.java:14: cannot find symbol
symbol : variable r
location: class Circle
Circle myCircle = Circle(myCircle.r);
^
CircleApp.java:18: cannot find symbol
symbol : variable getRadius
location: class Circle
System.out.println("The Radius is:" + myCircle.getRadius);
^
CircleApp.java:24: cannot find symbol
symbol : class CircleB
location: class CircleApp
Circle CircleB = new CircleB (5);
^
CircleApp.java:27: cannot find symbol
symbol : variable getRadius
location: class Circle
int circleTotalArea = myCircle.getRadius + (3.14* 5 * 5);
^
CircleApp.java:27: incompatible types
found : <nulltype>
required: int
int circleTotalArea = myCircle.getRadius + (3.14* 5 * 5);
^
CircleApp.java:28: cannot find symbol
symbol : variable getRadius
location: class Circle
double circleTotalCircumference = myCircle.getRadius + (2 * 3.14 * 5);
^
CircleApp.java:28: incompatible types
found : <nulltype>
required: double
double circleTotalCircumference = myCircle.getRadius + (2 * 3.14 * 5);
^
CircleApp.java:33: cannot find symbol
symbol : variable getRadius
location: class Circle
System.out.println("The Radius is:" + myCircle.getRadius);
^
8 errors


I know there’s a problem with radiusInt being a variable but I don’t know how to solve it. i have tried to implement x and y coordinates but they seem to confuse things even more. I mean I've only really started programming java about 2 weeks ago and this is all still completely new to me.

First, if you want to create a BufferedReader object, making an input stream using System.in is unnecessary. It might be necessary for streams that are associated with text files... but for System.in, it is not.

Just say

BufferedReader input = new BufferedReader(System.in);

One error:
You tried to create a 'Circle' object, but you do not have a class called Circle. Read my example below.

public class Rectangle{ <------After this line, rectangle is a class type.... which means you are allowed to create Objects of the type Rectangle. Saying Rectangle blah = new Rectangle(); creates a new Rectangle called blah. You can create as many Rectangles as you want, as long as the name isn't already taken. 



int width; //This creates a variable named width
int height; //This creates a variable named height


public Rectangle(int h, int w){
width = w;
height = h;
//Calling this method creates a Rectangle object with width = w and height = h.
}


public static void main(String[] args){
Rectangle r = new Rectangle(1, 3); //this creates a rectangle called r
Rectangle r = new Rectangle(2, 4); // this is NOT allowed, you already have a rectangle called r.

Rectangle g = new Rectangle(1,1); // this IS allowed
Rectangle h = new Rectangle(1,1); // ALSO allowed!

}


}

Second off, this line of code is a huge error:
Circle myCircle = Circle(myCircle.r);

Why? You do not have a class type called Circle! Earlier, you had the code Circle Circle = new Circle(). I told you to change it to myCircle because of the naming conflict. I did not mean change the class to myCircle (which is what you did), I meant change it like this: Circle myCircle = new Circle(). Also, there is a second reason that line is a huge logical error. Lets assume, first of all, that you go back and change the name of the class to Circle. It would look like this.

public class Circle{
int radiusInt;


public Circle(int r){
radiusInt = r;
}

//other stuff
}

Now, assuming your class now looks like that, the correct way to make a new Circle is the following. .

Circle myCircle = new Circle(4);

4 can also be replaced by any integer that you want. Or, you can declare a variable, assign it a value, and use it where I had the 4. An example of declaring a variable, assigning it a value, and then using it where I have the 4 is as follows:

int myInt = 20;
Circle myCircle = new Circle(myInt);

CircleApp.java:33: cannot find symbol
symbol : variable getRadius
location: class Circle
System.out.println("The Radius is:" + myCircle.getRadius);
8 errors

I take it getRadius is a method, so call it like a method, not like some static variable

`System.out.println("The Radius is: " + myCircle.getRadius());`

I take it getRadius is a method, so call it like a method, not like some static variable

You mean public variable.

You mean public variable.

No, stultuske means: public method.

If it was public variable it should be declared: public double radius = 0; Instead it is: getRadius. So when AceAtch writes: myCircle.getRadius ,
probably has something like this:

private double radius = 0;

public double getRadius() {
  return radius;
}

No, stultuske means: public method.

No, he meant public variable. He/she was saying that the OP should be saying myCircle.getRadius() instead of myCircle.getRadius (notice the parentheses). In the latter case, you would need to declare a public variable named getRadius in the Circle class.

You could make this variable static as well (as stultuske said), however, static attributes are typically called with MyClass.staticAttribute rather than myClassInstance.staticAttribute . Regardless, the variable would still need to be public.

No, he meant public variable.

I'm pretty sure I ment public method :)

commented: Thanks for the confirmation +3

No, he meant public variable. He/she was saying that the OP should be saying myCircle.getRadius() instead of myCircle.getRadius (notice the parentheses). In the latter case, you would need to declare a public variable named getRadius in the Circle class.

You could make this variable static as well (as stultuske said), however, static attributes are typically called with MyClass.staticAttribute rather than myClassInstance.staticAttribute . Regardless, the variable would still need to be public.

As stultuske replied (it is a method), when you get an error for this:
myCircle.getRadius
95% of cases it was supposed to be like this: myCircle.getRadius() and someone forgot the parenthesis,
instead of forgetting to declare the variable: getRadius public.

The right way to write java is declare variables:
radius as private
and use get methods.

Even though you can, it is not best practice to name public variables: getRadius. I mean: Yes, maybe the poster had actually a public variable getRadius (haven't seen the code) and I would be wrong.

But the right way and the most common way to write java is have:
private variableName and
public getVariableName()

No, reread what you/he wrote.

I take it getRadius is a method, so call it like a method, not like some static variable

I'm not saying he was suggesting making it a public variable, just that in the context of his comment, he meant public variable.

No, reread what you/he wrote.

I'm not saying he was suggesting making it a public variable, just that in the context of his comment, he meant public variable.

Yes you are right. Sorry

hi, your guys. you are all cheated by this guy. He declare a constructor have three parameters. He is not a java programmer, and not family with many syntax of Java. His constructor also has problem. the constructor can get what he want. I think he may be should buy a basic book about Java. OK?

I think you should learn to write in English, but if this was an English Help Forum, I would still help you without criticizing you based on your horrible verb usage.

hi, your guys. you are all cheated by this guy. He declare a constructor have three parameters. He is not a java programmer, and not family with many syntax of Java. His constructor also has problem. the constructor can get what he want. I think he may be should buy a basic book about Java. OK?

are you saying that it is bad coding, because his constructor takes three arguments??
If that's the case, you should by the "Java-for-dummies" AND an English dictionary

commented: Some time ago someone flagged all your posts as negative. After a lot of work they are back to normal +13

He is not a java programmer

Wow, and I thought that his code was written in Pascal

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.