Enum types, how does he know %s value?
Code :
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:
Code :
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?
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
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.
Re: Enum types, how does he know %s value?
Quote:
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
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 :D
Re: Enum types, how does he know %s value?
Quote:
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.
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?)
Re: Enum types, how does he know %s value?
Quote:
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.
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.
Re: Enum types, how does he know %s value?
Here is a link to the API doc: Java Platform SE 6
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.
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 :)