user2967961
user2967961

Reputation: 17

Java error: cannot find symbol

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);
    }
}

Upvotes: 0

Views: 540

Answers (9)

axon
axon

Reputation: 45

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(););

Upvotes: 1

SpringLearner
SpringLearner

Reputation: 13854

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);


}
      }

Upvotes: 5

orique
orique

Reputation: 1303

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).

Upvotes: 3

Bolesław Denk
Bolesław Denk

Reputation: 573

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()

Upvotes: 1

Jason C
Jason C

Reputation: 40436

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.

Upvotes: 0

vinay Maneti
vinay Maneti

Reputation: 1451

Create an Object for Lamp in Testlamp class

Lamp lamp1=new Lamp();

Upvotes: 1

CodingBird
CodingBird

Reputation: 735

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());

Upvotes: 3

Raghu
Raghu

Reputation: 1393

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());


}
      }

Upvotes: 2

Alex
Alex

Reputation: 1110

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

Upvotes: 3

Related Questions