How to fix the errors in this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
23rd Aug 2021, 10:21 AM
Constantine Xsavior
Constantine Xsavior - avatar
8 Answers
0
The problem comes from the Median function: you don't pass the array to it and it is not a method of the class – "this" referrs to nothing and "a" cannot be accessed either. Either make Median a method of the class and change "this" or pass the array as a parameter
23rd Aug 2021, 11:23 AM
Lisa
Lisa - avatar
0
Can you pls show me how to do it I really need it , it is for school purpose Lisa
23rd Aug 2021, 12:11 PM
Constantine Xsavior
Constantine Xsavior - avatar
0
Did you change the code? I cannot find the Median function in it...
23rd Aug 2021, 12:19 PM
Lisa
Lisa - avatar
0
Yes because I try a another method but the output is incorrect but there is no error how to fix it though pls help Lisa
23rd Aug 2021, 12:35 PM
Constantine Xsavior
Constantine Xsavior - avatar
0
There are several things: * sort the array before searching the median – use the method that the class already has! * according to the task description, the median should be a method not a function Here's the basic median method based on you attempt in the main. Put it into your class public double Median() { this.insertionSort(); long mdn; if(nElems%2 == 1){ mdn=a[(nElems+1)/2-1]; } else { mdn=(a[nElems/2 - 1]+a[nElems/2])/2; } return mdn; }
23rd Aug 2021, 12:53 PM
Lisa
Lisa - avatar
0
Sololearn has a Java course. Why don't you give it a try? OOP will be explained in it.
23rd Aug 2021, 12:54 PM
Lisa
Lisa - avatar
0
Okay I will try thanks anyways Lisa
23rd Aug 2021, 12:55 PM
Constantine Xsavior
Constantine Xsavior - avatar
0
Sometimes I just don't get what is the error in my program so is there anything with which my program is corrected on it's own and I can see the changes for better understanding
20th Sep 2022, 12:47 PM
Pooja Garg