Question in regards to my Intro to Java Programing. There are a few issues with
ID: 3850743 • Letter: Q
Question
Question in regards to my Intro to Java Programing.
There are a few issues with the following code. Report the lines that have issues and how they may be corrected so the code will work.
1.
2. //Begin Main Method
3. public static void main(String[] args) {
4. printMax(1, 2, 2, 1, 4);
5. printMax(new double[]{1, 2, 3, 78});
6. printMax(new int[]{2, 5, 9, 23});
7. } //End Main Method
8.
9. public static void printMax(double... numbers) {
10. if (numbers.length == 0) {
11. System.out.println("This is an empty set.");
12. return;
13. }
14.
15. double result = numbers[0];
16.
17. for (int i = 0; i <= numbers.length; i++) {
18. if (numbers[i] > result) {
19. result = numbers[i];
20. }
21. }
22. System.out.printf("Max is: %f ", result);
23. }//End printMax method Previous Next
24.
Explanation / Answer
Hi
There are few issues that have fixed it and highlighted the same below.
Below are the lines that have the issues
Lines: 6, 17
Test.java
package a2;
public class Test {
public static void main(String[] args) {
printMax(1, 2, 2, 1, 4);
printMax(new double[]{1, 2, 3, 78});
printMax(new double[]{2, 5, 9, 23});
} //End Main Method
public static void printMax(double... numbers) {
if (numbers.length == 0) {
System.out.println("This is an empty set.");
return;
}
double result = numbers[0];
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] > result) {
result = numbers[i];
}
}
System.out.printf("Max is: %f ", result);
}//End printMax method Previous Next
}
Output:
Max is: 4.000000
Max is: 78.000000
Max is: 23.000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.