aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please Explain program below Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please Explain program below" Watch "Please Explain program below" New topic
Author

Please Explain program below

Rajesh Chokkalingam
Greenhorn

Joined: Jul 09, 2013
Posts: 15
Hello Dudes,

In K&B book page no 281, i am not understand following program and i'm not able to compile the program. I'm using java 7 to compile this program.

public class Bridge{
public enum Suits{
CLUBS(20), DIAMONDS(20),HEARTS(30),SPADES(30),NOTRUMP(40){
public int getValue(int bid){
return ((bid-1)*30)+40;
}
};
Suits(int points){
this.points = points;
}
private int points;
public int getVaue(int bid){
return points*bid;
}
}
public static void main(String[] args){
System.out.println(Suits.NOTRUMP.getBidValue(3));
System.out.println(Suits.SPADES+" "+Suits.SPADES.points);
System.out.println(Suits.values());
}
}


A. The output could contain 30
B. The output could contain @bf73fa
C. The output could contain DIAMONDS
D. Compilation fails due to an error on line 6
E. Compilation fails due to an error on line 7
F. Compilation fails due to an error on line 8
G. Compilation fails due to an error on line 9
H. Compilation fails due to an error within lines 12 to 14
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Rajesh Chokkalingam wrote:i'm not able to compile the program.



Not able means what ? we are not taro card reader that we know what compile error you got , mention compile error with program.

class and enum both have public access modifier which is not allowed in same file.


My one regret in life is that I am not someone else.
- Woody Allen
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

The enum is nested, so that isn't a problem. But Meeta's right - tell us what error you get.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

But here's a more specific hint - check the spelling of your methods (when you declare them and when you call them).
Rajesh Chokkalingam
Greenhorn

Joined: Jul 09, 2013
Posts: 15
This is the error, i am getting while compiling the program. i have changed name getBidValue to getValue still have same compile error

Bridge.java:17: cannot find symbol
symbol : method getBidValue(int)
location: class Bridge.Suits
System.out.println(Suits.NOTRUMP.getBidValue(3));
^
1 error
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

You still haven't corrected the name everywhere.
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Rajesh Chokkalingam wrote:This is the error, i am getting while compiling the program. i have changed name getBidValue to getValue still have same compile error

Bridge.java:17: cannot find symbol
symbol : method getBidValue(int)
location: class Bridge.Suits
System.out.println(Suits.NOTRUMP.getBidValue(3));
^
1 error


So do you have that method in your program ?
Rajesh Chokkalingam
Greenhorn

Joined: Jul 09, 2013
Posts: 15
Matthew,

method name changed in every places and compiled getting same error.

Bridge.java:17: cannot find symbol
symbol : method getValue(int)
location: class Bridge.Suits
System.out.println(Suits.NOTRUMP.getValue(3));
^
1 error

please try this yourself and let me know
Rajesh Chokkalingam
Greenhorn

Joined: Jul 09, 2013
Posts: 15
meeta,
As I already mentioned this program taken from k&b book page no 281 and answer is A,B.
First problem, i'm getting compile error.
second, need clarification about how can be answer is a&b by running this program?
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 799
    
    1
In the main method, you have



But in the Bridge, I don't see any getBidValue method defined. I can only see getValue(int bid).
vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
Rajesh Chokkalingam wrote:Hello Dudes,

In K&B book page no 281, i am not understand following program and i'm not able to compile the program. I'm using java 7 to compile this program.

public class Bridge{
public enum Suits{
CLUBS(20), DIAMONDS(20),HEARTS(30),SPADES(30),NOTRUMP(40){
public int getValue(int bid){
return ((bid-1)*30)+40;
}
};
Suits(int points){
this.points = points;
}
private int points;
public int getVaue(int bid){
return points*bid;
}
}
public static void main(String[] args){
System.out.println(Suits.NOTRUMP.getBidValue(3));
System.out.println(Suits.SPADES+" "+Suits.SPADES.points);
System.out.println(Suits.values());
}
}


A. The output could contain 30
B. The output could contain @bf73fa
C. The output could contain DIAMONDS
D. Compilation fails due to an error on line 6
E. Compilation fails due to an error on line 7
F. Compilation fails due to an error on line 8
G. Compilation fails due to an error on line 9
H. Compilation fails due to an error within lines 12 to 14

Namaste Rajesh !!

Following resource Enum Constant specific body might help...
Ben Pheonix
Ranch Hand

Joined: Dec 11, 2012
Posts: 46
    
    1

Dear Rajesh,

Please try the following modified source code.



It should work now. There were two basic issues with your source code. First of all on first println line, see below



There is no getBidValue() method defined. I have changed it to getVaue. Additional thing, for NOTRUMP, you are trying
to override a specific enum method but the method name wasn't correct even, so it will never call the overriden method.
If we call your code. Your previous line depicted as.



Hope you get the issues with your code.
HTH,
Ben


Twitter:ben_pheonix
Rajesh Chokkalingam
Greenhorn

Joined: Jul 09, 2013
Posts: 15
Thanks Ben Pheonix. I reviewed the code more than 10 times. but i didn't check the spelling of the method. Usually i'm using editors for programs. but for OCPJP preparation, i started typing code in cmd prompt itself.

Thanks to all.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

Rajesh Chokkalingam wrote:but i didn't check the spelling of the method

Well, I did say that the spelling was what you needed to check! Glad you've got it sorted now, though.
Ben Pheonix
Ranch Hand

Joined: Dec 11, 2012
Posts: 46
    
    1

Dear Rajesh,

I am glad your problem is sorted and yes Mathew was correct it was indeed a spelling error.
Please do use source code with proper code formatting as it readability of your code.
Hth,
Ben
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Please Explain program below