0

I am writing a program to show the lamp level with "O", but when I compile, it show "cannot find symbol", I have declare "message" and "brightness", Is there anything else I miss to declare? class Lamp and class TestLamp I save in different file, when I compile Lamp ,It show no error. But it show "cannot find symbol" when compiling TestLamp

class Lamp {

    // Sub-task 1: Declare and initialize data member with default value
    int brightness=1;

    // Sub-task 2: Define a method to indicate the brightness level of lamp
    String getBrightness() {
        String message = "";
        while(brightness>0) { 
            brightness--;
            message += "O"; 
        } 

        return message;
    }

    // Sub-task 3: Define a method to update the brightness of the lamp
    void setBrightness(int b) {

        if(b<1 || b>5) 
            brightness=2;
        else
            brightness=b;

    }
}

class TestLamp {
    public static void main (String[] args) {
        // Sub-task 4: Declare and create 3 lamp objects
        Lamp lamp1,lamp2,lamp3;

        // Sub-task 5: Adjust the lamp brightness according to the requirement
        lamp1.setBrightness(3);
        lamp2.setBrightness(10);

        // Sub-task 6: Display the information of the created lamps
        lamp1.getBrightness();
        System.out.println("Lamp1"+lamp1.message);
        lamp2.getBrightness();
        System.out.println("Lamp2"+lamp2.message);
    }
}
2
  • Could you post the full error ? Also, if TestLamp is not in the same package, you must put import package.Lamp before your class declaration. Commented Nov 8, 2013 at 7:42
  • 1
    Cannot find which symbol? And at which line? Commented Nov 8, 2013 at 7:43

9 Answers 9

5

you are just creating the reference Lamp lamp1,lamp2,lamp3; but you are not creating any object create object first like the below

Lamp lamp1=new Lamp();

The scope of String message is with in the method getBrightness() so lamp1.message will give you error

So to print message either you can define String message at class level or use lamp1.getBrightness()

Please see the below working code

class Lamp {

// Sub-task 1: Declare and initialize data member with default value
    int brightness=1;
    String message = "";
// Sub-task 2: Define a method to indicate the brightness level of lamp
String getBrightness() {

    while(brightness>0) { 
        brightness--;
        message += "O"; 
    } 


    return message;
}

// Sub-task 3: Define a method to update the brightness of the lamp
void setBrightness(int b) {

    if(b<1 || b>5) 
        brightness=2;
    else
        brightness=b;


}


  }

 class ddd {
public static void main (String[] args) {
    // Sub-task 4: Declare and create 3 lamp objects
    Lamp lamp1 = new Lamp();
    Lamp lamp2=new Lamp();

    // Sub-task 5: Adjust the lamp brightness according to the requirement
    lamp1.setBrightness(3);
    lamp2.setBrightness(10);

    // Sub-task 6: Display the information of the created lamps
    lamp1.getBrightness();
      System.out.println("Lamp1"+lamp1.message);
    lamp2.getBrightness();
      System.out.println("Lamp2"+lamp2.message);


}
      }
Sign up to request clarification or add additional context in comments.

1 Comment

@JasonC friend while going through the code this stuck to my mind,I was just editing my answer and found you have already provided the solution.So i stopped
3

You haven't instantiated your lamp objects, and your Lamp class lacks a message field

Comments

3

Before your setBrightness() method, you should instantiate the lamp object.

Lamp lamp1 = new Lamp();

Do the same for all the lamp objects.

Then change your

System.out.println("Lamp1"+lamp1.message);

to

System.out.println("Lamp1"+lamp1.getBrightness());

Comments

3

You use lamp1.message but there is no message field at Lamp class.

Also, you're not initializing the instances of Lamp, which will result in another compile-time error. (Thanks JasonC, I'm so used to IDEs that I forget basic things).

1 Comment

Use of the uninitialized variables would lead to a compilation error, not an NPE (unless the poster initializes them to null, of course).
2

Corrected the errors.

Error 1:
System.out.println("Lamp1"+lamp1.getBrightness()); //changed from lamp1.message
System.out.println("Lamp1"+lamp1.getBrightness()); //changed from lamp2.message

Error 2:
        lamp1 = new Lamp(); //missing instance creation
        lamp2 = new Lamp();//missing instance creation

/* if you are planning to use lamp3 and lamp4, create that instance as well */

Working Code after fix:

 class Lamp {

// Sub-task 1: Declare and initialize data member with default value
    int brightness=1;

// Sub-task 2: Define a method to indicate the brightness level of lamp
String getBrightness() {
    String message = "";
    while(brightness>0) { 
        brightness--;
        message += "O"; 
    } 


    return message;
}

// Sub-task 3: Define a method to update the brightness of the lamp
void setBrightness(int b) {

    if(b<1 || b>5) 
        brightness=2;
    else
        brightness=b;


}


  }

 class TestLamp {
public static void main (String[] args) {
    // Sub-task 4: Declare and create 3 lamp objects
    Lamp lamp1,lamp2,lamp3;

    lamp1 = new Lamp();
    lamp2 = new Lamp();

    // Sub-task 5: Adjust the lamp brightness according to the requirement
    lamp1.setBrightness(3);
    lamp2.setBrightness(10);

    // Sub-task 6: Display the information of the created lamps
    lamp1.getBrightness();
      System.out.println("Lamp1"+lamp1.getBrightness());
    lamp2.getBrightness();
      System.out.println("Lamp2"+lamp2.getBrightness());


}
      }

Comments

1

Create an Object for Lamp in Testlamp class

Lamp lamp1=new Lamp();

1 Comment

+/-0 Great catch, but this is not the reason for the compilation error the OP describes (although it will certainly be the reason for the next compilation error he receives after fixing his current one).
1

First of all:

  • You dont have message variable in Lamp class, which you try to call in System.out.printline, use System.out.println("Lamp1"+lamp1.getBrightness()); instead

Next, some convention things to cover:

  • It's good thing to have one of classes in you source file public -> public class TestLamp {
  • Mostly getters are used for retriving private/protected variables as it is from inside of class instance. In your case if you whant to return some string representation it's better to call method toString() or getDispleyText()

Comments

1

Its important to remember that in Java Object objectName; Isn't actually making a new object. It makes a pointer/reference to an object. When it's first created, it's set to address 0 (null). to actually create the object you need to use the new keyword. In your case, you should do the following...

class TestLamp 
{
    public static void main (String[] args) 
    {
        // Sub-task 4: Declare and create 3 lamp objects
        Lamp lamp1,lamp2,lamp3;

        lamp1 = new Lamp();
        lamp2 = new Lamp();
        lamp3 = new Lamp();

        // Sub-task 5: Adjust the lamp brightness according to the requirement
        lamp1.setBrightness(3);
        lamp2.setBrightness(10);

        // Sub-task 6: Display the information of the created lamps
        lamp1.getBrightness();
        System.out.println("Lamp1"+lamp1.message);
        lamp2.getBrightness();
        System.out.println("Lamp2"+lamp2.message);
    }
}

EDIT: Also, your string message in Lamp can't be reached by the main method as its within a function. instead use the method you madse for that, getBrightness().

        System.out.println("Lamp1" + lamp1.getBrightness(););
        System.out.println("Lamp2" + lamp2.getBrightness(););

Comments

0

You try to use lamp1.message, but message is not a field of lamp. You have:

    // Sub-task 2: Define a method to indicate the brightness level of lamp
    String getBrightness() {
        String message = "";
        while(brightness>0) { 
            brightness--;
            message += "O"; 
        } 


        return message;
    }

Then you go on to use it as:

        lamp1.getBrightness();
        System.out.println("Lamp1"+lamp1.message);

But what you probably meant was:

        System.out.println("Lamp1"+lamp1.getBrightness());

Note that you are using the return value of the getBrightness() method, which returns the string you described.

By the way, by convention, property getters and setters generally get and set the same value type. More common practice would be for you to have int getBrightness() that returned the current brightness value (the exact opposite of setBrightness(int)), and add a special method e.g. String getBrightnessMessage() to generate the string you are using.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.