14 Replies - 4223 Views - Last Post: 21 September 2011 - 09:32 AM Rate Topic: -----

#1 enadanohj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-September 11

Rectangle class-Test class Java

Posted 19 September 2011 - 07:40 AM

Help..

// Rectangle.java:

package rectangle;
public class Rectangle
{
private double width = 1;
private double height = 1;
private static String color = "white";
public Rectangle() { }
public Rectangle(double w, double h, String c)
{
width = w;
height = h;
color = c;
}
public void setWidth(double w) { width = w; }
public void setHeight(double h) { height = h; }
public static void setColor(String c) { color = c; }

public double getWidth() { return width; }
public double getHeight() { return height; }
public static String getColor() { return color; }

public double findArea() { return height * width; }
public double findPerimeter() { return (2 * height) + (2 * width); }
}



My Test.java code shows these errors

J:\files\java\ClientProgram.java:9: cannot find symbol
symbol : class Rectangle
location: class rectangle.ClientProgram
Rectangle r1 = new Rectangle(5, 5, "green");
^
J:\files\java\ClientProgram.java:9: cannot find symbol
symbol : class Rectangle
location: class rectangle.ClientProgram
Rectangle r1 = new Rectangle(5, 5, "green");
^
2 errors

Tool completed with exit code 1


// Test.java

package rectangle;
public class Test
{
public static void main(String args[])
{

Rectangle r1 = new Rectangle(5, 5, "green");

/*double width = mySquare.getWidth();
double height = mySquare.getHeight();
String color = mySquare.getColor();*/

System.out.print("\n Width = " + r1.getWidth() );
System.out.print("\n Height = " + r1.getHeight() );
System.out.print("\n Color = " + r1.getColor() );

r1.setWidth(15);
r1.setHeight(10);
r1.setColor("red");

System.out.print("\n Width = " + r1.getWidth() );
System.out.print("\n\n Height = " + r1.getHeight() );
System.out.print("\n\n Height = " + r1.getColor() );
System.out.print("\n Area = " + r1.findArea() );
}
}



Please help.. thanks..

This post has been edited by enadanohj: 19 September 2011 - 07:41 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Rectangle class-Test class Java

#2 Viske  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 70
  • Joined: 07-June 11

Re: Rectangle class-Test class Java

Posted 19 September 2011 - 08:13 AM

You can't set a static variable in the constructor, like this:
public class Rectangle {
    private static String color = "white";
    public Rectangle(String c) {
        color = c;
    }
}


r1.setColor("red") should also be Rectangle.setColor("red"), as setColor is static.

I'm not sure you want a static variable in this case either, as the Color will be the same for all instances of Rectangle. For example..

public class Rectangle {
    private static String color;
    // Static constructor
    static {
        color = "white";
    }
    public Rectangle() { }
    
    public static void setColor(String newColor) {
        color = newColor;
    }
    public static String getColor() {
        return color;
    }
}

// In your main class.

public static void main(String[] args) {
    Rectangle recOne = new Rectangle();
    Rectangle recTwo = new Rectangle();

    System.out.println(recOne.getColor());    // Will be white
    System.out.println(recTwo.getColor());    // Will also be white

    recOne.setColor("red");

    System.out.println(recOne.getColor());    // Will be red
    System.out.println(recTwo.getColor());    // Will also be red, as color is static and is the same for all instances

    Rectangle.setColor("purple");

    System.out.println(recOne.getColor());    // Will be purple, as static methods can be accessed through the Class
    System.out.println(recTwo.getColor());    // Will also be purple
}



Using Rectangle.getColor() would be the same as recOne.getColor() or recTwo.getColor() in the above cases.

Unless this is what you are wanting to happen, change
private static String color = "white";
to
private String color = "white"

This post has been edited by Viske: 19 September 2011 - 08:14 AM

Was This Post Helpful? 1
  • +
  • -

#3 enadanohj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-September 11

Re: Rectangle class-Test class Java

Posted 19 September 2011 - 09:31 AM

my problem is that, whenever i compile it, it always display...

cannot find symbol
symbol : class Rectangle
Was This Post Helpful? 0
  • +
  • -

#4 Viske  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 70
  • Joined: 07-June 11

Re: Rectangle class-Test class Java

Posted 19 September 2011 - 09:43 AM

The error is telling you that it can't find the class Rectangle. Make sure that the files are named correctly.
Was This Post Helpful? 0
  • +
  • -

#5 cmpshr  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Rectangle class-Test class Java

Posted 19 September 2011 - 09:43 AM

Quote

You can't set a static variable in the constructor, like this:

Yes you can...95% of the static static variables are initialized that way.
And it was not in the constructor, and if it would have been there it will have worked.
Anyhow the problem is somewhere else.

Is your Test.java and Rectangle.java in the same directory /rectangle ?

If so try to remove the package statement from both class and try again
Was This Post Helpful? 0
  • +
  • -

#6 Viske  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 70
  • Joined: 07-June 11

Re: Rectangle class-Test class Java

Posted 19 September 2011 - 09:50 AM

View Postcmpshr, on 19 September 2011 - 05:43 PM, said:

Quote

You can't set a static variable in the constructor, like this:

Yes you can...95% of the static static variables are initialized that way.
And it was not in the constructor, and if it would have been there it will have worked.
Anyhow the problem is somewhere else.


Yeah, my mistake - you can set static variables this way. Personally, I don't set them in the non-static constructor as they are used across the class and aren't unique to instances. I did mean to address the error in my other post as well, guess I forgot.
Was This Post Helpful? 0
  • +
  • -

#7 enadanohj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-September 11

Re: Rectangle class-Test class Java

Posted 19 September 2011 - 10:30 PM

both my files are on the same directory...
Was This Post Helpful? 0
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Rectangle class-Test class Java

Posted 19 September 2011 - 11:10 PM

The code in your original post compiles and runs for me.

Why do the error messages refer to "ClientProgram.java"?
Was This Post Helpful? 0
  • +
  • -

#9 enadanohj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-September 11

Re: Rectangle class-Test class Java

Posted 21 September 2011 - 06:19 AM

so sorry, i'm new to this... i've been working on this prob, but it still says it can't find the symbol: class Rectangle
Was This Post Helpful? 0
  • +
  • -

#10 Viske  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 70
  • Joined: 07-June 11

Re: Rectangle class-Test class Java

Posted 21 September 2011 - 06:23 AM

Quote

J:\files\java\ClientProgram.java:9: cannot find symbol
symbol : class Rectangle
location: class rectangle.ClientProgram


What is ClientProgram? Are the Rectangle and Test classes in ClientProgram.java?
Was This Post Helpful? 0
  • +
  • -

#11 enadanohj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-September 11

Re: Rectangle class-Test class Java

Posted 21 September 2011 - 08:20 AM

my apologies...
these are the errors that display:

J:\files\java\Test.java:9: cannot find symbol
symbol : class Rectangle
location: class rectangle.Test
Rectangle r1 = new Rectangle(5, 5, "green");
^
J:\files\java\Test.java:9: cannot find symbol
symbol : class Rectangle
location: class rectangle.Test
Rectangle r1 = new Rectangle(5, 5, "green");
^
2 errors

Tool completed with exit code 1
Was This Post Helpful? 0
  • +
  • -

#12 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Rectangle class-Test class Java

Posted 21 September 2011 - 08:28 AM

have you tried to rename the class you created that is Rectangle to little r? rectangle? Rectangle is an actual class in the API so it is most likely confused

It works fine for me. You need to make sure that you have both files in the same directory.

This post has been edited by Fuzzyness: 21 September 2011 - 08:39 AM

Was This Post Helpful? 0
  • +
  • -

#13 enadanohj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-September 11

Re: Rectangle class-Test class Java

Posted 21 September 2011 - 09:26 AM

View PostFuzzyness, on 21 September 2011 - 08:28 AM, said:

have you tried to rename the class you created that is Rectangle to little r? rectangle? Rectangle is an actual class in the API so it is most likely confused

It works fine for me. You need to make sure that you have both files in the same directory.

yeah i have tried replacing r with R and vice versa.. but it still doesn't work..
Was This Post Helpful? 0
  • +
  • -

#14 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Rectangle class-Test class Java

Posted 21 September 2011 - 09:30 AM

yeah that doesn't matter actually. You need to have both files in the same directory. What I mean by that is being in the same folder. That is the only reason why it wouldn't work. Me and another person in this thread have both tried it and worked for us so that must be the thing you are doing wrong.

This post has been edited by Fuzzyness: 21 September 2011 - 09:31 AM

Was This Post Helpful? 0
  • +
  • -

#15 cmpshr  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Rectangle class-Test class Java

Posted 21 September 2011 - 09:31 AM

Post again all your classes. Don't forget :code:
We still don't know where rectangle. ClientProgram comes from.
And while we are at it, also an image of your directory.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1