aspose file tools*
The moose likes Beginning Java and the fly likes The method is undefined for the type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The method is undefined for the type" Watch "The method is undefined for the type" New topic
Author

The method is undefined for the type

Abid Ramay
Ranch Hand

Joined: Apr 06, 2013
Posts: 43




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

Joined: Apr 20, 2011
Posts: 241
    
    1

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

Joined: Apr 06, 2013
Posts: 43
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

Joined: Apr 06, 2013
Posts: 43
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

Joined: Apr 06, 2013
Posts: 43


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

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

thanks for your help
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
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

Joined: Aug 07, 2007
Posts: 2366
    
  50
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

Joined: Apr 06, 2013
Posts: 43


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

Joined: Mar 17, 2011
Posts: 8244
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Abid Ramay
Ranch Hand

Joined: Apr 06, 2013
Posts: 43
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

Joined: Aug 16, 2005
Posts: 14350
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The method is undefined for the type