Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The method is undefined for the type

 
Abid Ramay
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




hi all - i am working on this code. my method getChannel is giving an error

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getChannel() is undefined for the type TV

at TestTV.main(TestTV.java:18)


please help.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at your brackets for your getChannel() method. The first one is misplaced.

Also, your switchTvOn() method has some problems.
1. It's return type is int, but you're returning a boolean.
2. Why isn't the return type void? You're not getting the TV's state, you're setting it.
3. Calling switchTvOn(false) should turn the TV off, and switchTvOn(true) should turn it on, but you're just setting things to true.
4. Also, you have two on variables, a local method variable and a state variable. When you do on = true; you're setting the local variable to true instead of the state variable.

Here's how switchTvOn() should look:



In the above, this.on refers to the on state variable at the top of your class, and on refers to the local on variable that was passed in to the method. All you're doing is setting the your TV on or off, depending on whatever boolean value the user passes in.

One more thing:
You should read up on access modifiers. Basically, classes, class variables, methods, and more can all be labeled with an access modifier (e.g. public, private, protected). When using ClassA from ClassB, ClassA's access modifiers will either block or allow the visibility of its various components. As a rule of thumb for now:
1. Classes should be public (e.g. public class Tester{})
2. If methods and variables are only to be used within the class they're declared in, make them private.
3. If methods and variables are to be used from other classes, make them public.

Cheers!
 
Abid Ramay
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:Look at your brackets for your getChannel() method. The first one is misplaced.

Also, your switchTvOn() method has some problems.
1. It's return type is int, but you're returning a boolean.
2. Why isn't the return type void? You're not getting the TV's state, you're setting it.
3. Calling switchTvOn(false) should turn the TV off, and switchTvOn(true) should turn it on, but you're just setting things to true.
4. Also, you have two on variables, a local method variable and a state variable. When you do on = true; you're setting the local variable to true instead of the state variable.

Here's how switchTvOn() should look:



In the above, this.on refers to the on state variable at the top of your class, and on refers to the local on variable that was passed in to the method. All you're doing is setting the your TV on or off, depending on whatever boolean value the user passes in.

One more thing:
You should read up on access modifiers. Basically, classes, class variables, methods, and more can all be labeled with an access modifier (e.g. public, private, protected). When using ClassA from ClassB, ClassA's access modifiers will either block or allow the visibility of its various components. As a rule of thumb for now:
1. Classes should be public (e.g. public class Tester{})
2. If methods and variables are only to be used within the class they're declared in, make them private.
3. If methods and variables are to be used from other classes, make them public.

Cheers!


Hi

1) thanks for the tip. i new i made a silly mistake like {} not using properly

2)

in this i was experimenting, i get your message loud and clear.

3) again, thanks for the link - (acces modifier)
 
Abid Ramay
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i more question - what about my constructor



then i used int newChannel again for method setChannel



do you find this good programming practice or can it be improved ?

thanks
 
Abid Ramay
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


when i call this methods changeVolume tv2.setChannel(10);

it produces '0' output despite passing any value...

thanks for your help
 
Tony Docherty
Bartender
Posts: 2957
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then i used int newChannel again for method setChannel
...
do you find this good programming practice or can it be improved ?

Reusing a local variable name is perfectly acceptable provided the name is appropriate for the information the variable holds as it does in your example. What you should avoid is using a local variable name that is the same as an instance or class variable name as this is confusing.
 
Tony Docherty
Bartender
Posts: 2957
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i call this methods changeVolume tv2.setChannel(10);

it produces '0' output despite passing any value...

Look at your if statement and work out what will happen for different values of 'on' and 'volume'
 
Abid Ramay
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


ok .. i figured it out .. thanks to you guyz

1) i changed if statement ->
if (on == true)
ealier i was using assignment operator which changed the value to
true
instead of comparing it .

2) i changed
if (( volume == 1) && (volume == 100))
with brackets which works more appropriately.

3) now the results are as expected.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abid Ramay wrote:ok .. i figured it out .. thanks to you guyz
1) i changed if statement -> if (on == true)
ealier i was using assignment operator which changed the value to
true
instead of comparing it .

Which is why you should never use that form. If the field is a boolean, use:
if (on) ...

Winston
 
Abid Ramay
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i realize that now

statement -> if (on == true) will result a boolean and cannot be compared to an 'int'

(Y. Daniel Liang-> intro to java programming - pg- 90 ) has good explanantion on the topic
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15274
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abid Ramay wrote:statement -> if (on == true) will result a boolean and cannot be compared to an 'int'

The expression between the () of an if-statement must be a boolean.

You will not get that error if you write if (on), because on is already a boolean.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic