Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Enum types, how does he know %s value?

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Enum types, how does he know %s value?

    public enum Planet {
        MERCURY (3.303e+23, 2.4397e6),
        VENUS   (4.869e+24, 6.0518e6),
        EARTH   (5.976e+24, 6.37814e6),
        MARS    (6.421e+23, 3.3972e6),
        JUPITER (1.9e+27,   7.1492e7),
        SATURN  (5.688e+26, 6.0268e7),
        URANUS  (8.686e+25, 2.5559e7),
        NEPTUNE (1.024e+26, 2.4746e7);
     
        private final double mass;   // in kilograms
        private final double radius; // in meters
        Planet(double mass, double radius) {
            this.mass = mass;
            this.radius = radius;
        }
        private double mass()   { return mass; }
        private double radius() { return radius; }
     
        // universal gravitational constant  (m3 kg-1 s-2)
        public static final double G = 6.67300E-11;
     
        double surfaceGravity() {
            return G * mass / (radius * radius);
        }
        double surfaceWeight(double otherMass) {
            return otherMass * surfaceGravity();
        }
        public static void main(String[] args) {
            if (args.length != 1) {
                System.err.println("Usage:  java Planet <earth_weight>");
                System.exit(-1);
            }
            double earthWeight = Double.parseDouble(args[0]);
            double mass = earthWeight/EARTH.surfaceGravity();
           for (Planet p : Planet.values())
               System.out.printf("Your weight on %s is %f%n",
                                 p, p.surfaceWeight(mass));
        }
    }
    This code is taken from Oracle's own web site. My question is about the last of the code. I mean this part:

     for (Planet p : Planet.values())
               System.out.printf("Your weight on %s is %f%n",
                                 p, p.surfaceWeight(mass));

    %s and %d%n are used in this code. How does he know where to use %s and where to use %d%n? I mean he did not define these anywhere.

    What am I missing?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Enum types, how does he know %s value?

    The printf() method uses the arguments following the initial String to fill in the values for the %x places.
    Given: %s is %f%n",
    %s <- p,
    %f <- p.surfaceWeight(mass)
    %n ???

    Are you sure you copied this correctly

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Enum types, how does he know %s value?

    Well, the code is taken exactly from this link: Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    So, a second question then: How does he decide that p = %s and p.surfaceWeight(mass) = %f? Taking a look at the link and lesson, I couldn't see any information.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Enum types, how does he know %s value?

    does he decide that p = %s and p.surfaceWeight(mass) = %f?
    It's positional: the first % gets the value of first arg after the string(p), second % get value of second arg(p.surfaceWeight(mass)) ...
    The value of p replaces the %s and the value of p.surfaceWeight(mass) replaces the %f%n

  5. The Following User Says Thank You to Norm For This Useful Post:

    beer-in-box (July 18th, 2011)

  6. #5
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Enum types, how does he know %s value?

    Actually why is it %s instead of %a?
    Hey, if you have a good guide link about this stuff, let me know. I'll read it, try to understand it and then if I have any questions after that, I ask again.

    I don't want to give you a headache

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Enum types, how does he know %s value?

    why is it %s instead of %a?
    Did you look up what the s and what the a are used for? I haven't so I can't tell you.
    I know there are letters for the different data types and they are all listed with the printf doc.

    Here is how you find a method if you don't know its class.
    Go to the API doc page. At the top of the page is a light blue bar. In that bar is the word Index.
    Click on that to go to the index page. Click on the first letter of the method, here p.
    Find the name of the method in the list on that page.
    Then read and follow links until you find what you need.

  8. #7
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Enum types, how does he know %s value?

    Oh, I think I got what I need. You say the letters are used for only one type of data.

    But I couldn't understand how to find a method if I don't know its class.

    (I am new to the whole thing, I figured I should read and comprehend the whole lessons more or less before I really get into the action. I will read all the lessons again once I am done for the first time. Reading once and practicing once doesn't seem to help. Am I right?)

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Enum types, how does he know %s value?

    But I couldn't understand how to find a method if I don't know its class.
    Well now you know.

    System.out.printf(...

    Also You could find the class by looking in the System class for the out variable.

  10. #9
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Enum types, how does he know %s value?

    Actually, no. I don't I couldn't understand your explanation. But I guess I should read more before asking more questions about it.
    By the way, thank you for your help.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Enum types, how does he know %s value?

    Here is a link to the API doc: Java Platform SE 6

  12. The Following User Says Thank You to Norm For This Useful Post:

    beer-in-box (July 18th, 2011)

  13. #11
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Enum types, how does he know %s value?

    I knew I overlooked something. Thank you for your patience and help I guess that solved my question completely now.

  14. #12
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Enum types, how does he know %s value?

    I learnt more about my question now. Those %s, f, d and n are formatters. You can use them in a Formatter object or in printf() method. %n creates a new line, and there is %% which adds a percentage sign.
    To sum up,
    %s for strings, %f for decimal floating numbers and %d for decimal integers.
    They are used respectively. First %s matches with first argument.
    Of course that is not all and if one wants to learn about it, he should read about Formatter.

    Just wanted to add if someone needs to learn

Similar Threads

  1. Trouble using enum in constructor when creating a class
    By willmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 13th, 2011, 10:48 AM
  2. [SOLVED] Enum problem
    By pajfilms in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 21st, 2011, 07:26 AM
  3. enum, value of: Why is this code line structured the way it is?
    By SPACE MONKEY in forum Java Theory & Questions
    Replies: 5
    Last Post: March 28th, 2011, 09:15 AM
  4. Enumerated Types
    By mushy in forum Object Oriented Programming
    Replies: 3
    Last Post: January 27th, 2011, 10:06 AM
  5. incompatible types
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2011, 10:40 AM