+ 2
Plz explain from line 35
10 Answers
+ 11
Sanzid Sadman
The Java printf() method is used to write the formatted strings. The 'f' in 'printf' keyword means 'formatted'.
For example:
intĀ xĀ =Ā 15;
floatĀ yĀ =Ā 5.6f;
charĀ chĀ =Ā 'S';
StringĀ strĀ =Ā "Hi there!";
// Display results:
System.out.printf(
"The formatted string: %d %f %c %s", x, y, ch, str);
ā¢ The list of conversion characters that you may use in the 'printf':
%d ā for signed decimal intege
%f ā for the floating point
%o ā octal number
%c ā for a character
%s ā a string
%i ā use for integer base 10
%u ā for unsigned decimal number
%x ā hexadecimal number
%% ā for writing % (percentage)
%n ā for new line = \n
You may use the precision that specifies the number of digits after the decimal point for floating numbers.
For example:
double x = 56.1525;
double yĀ =Ā 26.2481119;
System.out.printf(
"x = %.2f %n y = %.4f", x, y );
Output:
x = 56.15
y = 26.2481
+ 10
Sanzid Sadman
Here is an useful link,
ā¢ A 'printf' format reference page ( cheat sheet ) ā https://alvinalexander.com/programming/printf-format-cheat-sheet
+ 7
David Carroll š
You gave a great answer and I doubt there is anything to add.š»
+ 6
I recommend you to give <distance> variable (declared at line 21) another name, just to evade from confusion. Because 'distance' is also a method name in there.
You could also consult with the original code author.
+ 6
The compiler is able to differentiate between the local variable of type double on the left side of the equal operator and the static method sharing the same name on the right side. When the code is compiled, the named symbols will resolve to different memory addresses and byte code commands.
Danijel IvanoviÄ Do you want to add any insight?
--------
Update:
----
Sanzid Sadman DOH! š¤¦āāļøI just realized the question was asking about the code starting from line 35 and not the line itself. Still yet, I thought he referenced the wrong line believing 34 was the line in question as it looks so strange and would not work in dynamically typed languages.
Sorry for posting an irrelevant answer. Hopefully, it will be of value to others. š¤·āāļøš
+ 6
Yes, David Carroll me too...š
Sanzid Sadman
Line 35?
System.out.println();
+ 5
%d is for integers and %f is for decimal numbers, %.2f means number will get Rounded off to 2 decimal places(you can see more about them in C lesson here).
if you are asking for variable name same as function name then it is allowed in java, you can read more about it here https://stackoverflow.com/questions/9960560/java-instance-variable-and-method-having-same-name
+ 4
%d displays the integer and %f displays a float
System.out.printf(
" The distance between points:\n A(%d, %d) and B(%d, %d) \n\n distance = %.2f", x1, y1, x2, y2, distance);
The first %d displays x1, the second %d displays y1, third %d displays x2, fourth %d displays y2, and %.2f displays distance with 2 decimal places
+ 3
it displays the results
- 1
Hello, Does anyone know how to make a mitisse connection with Java?