• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please Explain program below

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The enum is nested, so that isn't a problem. But Meeta's right - tell us what error you get.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You still haven't corrected the name everywhere.
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Ranch Hand
Posts: 46
1
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rajesh Chokkalingam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 46
1
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic