CodeGym /Java Course /Java Syntax /Practice with static methods

Practice with static methods

Java Syntax
Level 6 , Lesson 8
Available

"Hi, Amigo. Here are some interesting tasks involving static methods:"

6
Task
New Java Syntax, level 6, lesson 8
Locked
Filling an array
Implement a static main(String[]) method that populates the array variable with the values valueStart and valueEnd. If the length of the array is even, then its first half must be filled with valueStart values, and the second half with valueEnd. If the length of the array is odd, then fill the large
6
Task
New Java Syntax, level 6, lesson 8
Locked
Splitting an array
Implement the main(String[]) method, which splits an array into two subarrays and fills a two-dimensional result array with them. If the length of the array is odd, then the larger half should be copied into the first subarray. Use the Arrays.copyOfRange(int[], int, int) method to split the array. D
6
Task
New Java Syntax, level 6, lesson 8
Locked
Sorting an array
Implement the main(String[]) method, which sorts array in ascending order. Use the Arrays.sort(int[]) method to sort the array.
6
Task
New Java Syntax, level 6, lesson 8
Locked
Is anyone there?
Implement the main(String[]) method, which displays true if the element is contained in the passed array, otherwise false. The array variable should not change the position of its elements. To search for an element in an array, you need a binary search. To do this, use the static Arrays.binarySearc
Comments (90)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Fadhil Radhian Level 18, Semarang, Indonesia
24 March 2023
quite challenging tasks but accmplishable
Anonymous #10798360 Level 8, Woking, United Kingdom
19 July 2022
Hi, For the StringHelperClass task, I've got it right but then I had a look at the solution to compare. I don't understand it. Can someone please explain in to me? Don't understand sb was created and why append is used. Thanks in advance :)
Krig Raseri Level 38, Dallas, United States
30 August 2022
Just a more tedious way to do it by appending the string by the count times. Way easier to just use .repeat
Brentachii Level 13, United States of America, United States
24 January 2022
The calculator task is odd... I tried (b * 100.0) / a; that outputs correctly but it won't take it as correct. Is it just me or does that seem like if it wanted me to use a specific method of calculation they should have specified it exactly how they want it? !! edit !! I found out this answer was totally wrong thanks to some awesome people.
whoseunassailable Level 28, India, India
24 January 2022
multiply by 0.01 instead
Brentachii Level 13, United States of America, United States
25 January 2022
someone helped me I was going about it the wrong way I needed to first multiply a and b then divide. I don't think you need a separate 1.0 step because the 100.0 makes it a floating-point number but if someone knows if that is right then please let me know
Korlat Level 25, Gasteiz, Basque Country, Spain
9 February 2022
The method receives two int as a parameter, you must convert them to double.

return (double) (a * b) / 100;
Franco Polizzi Level 11, Werther, Germany
24 July 2022
I agree. this task is really very odd worded. Especially the percentage part.
miguel Level 12, Corredor
13 July 2021
In Body Mass Index I passed the solution but I have this error in INTELLIJ IDEA Exception in thread "main" java.lang.NumberFormatException: For input string: "68.4" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at com.codegym.task.task05.task0532.Solution.main(Solution.java:13) I need help to solve this... please...
Le Phan Level 14, Véry, France
15 January 2022
Your input is expected to be an integer, but the real input is 68.4 which is a double number. Better use Double.parseDouble to correct the issue.
Anonymous #10765714 Level 16, Kathmandu
12 July 2021
Static method vs non-static i.e instance method - More i do more i get confused. Can someone share some good idea with example to refresh my head, please? What i understood is: Static method are those method if happen to use than than we just use the classname.method() Non-static method if want to use: then we need to create a class instance like Cat cat = new Cat() and to access its values we use this is that right? Argh, i am so confused with static and non static Any help with example would be very very appreciated Thanks
Krig Raseri Level 38, Dallas, United States
30 August 2022
Might be wrong but.... Static is basically when it's standalone and can be used by itself, like adding to a count that doesn't have to do with any objects or anything. count(5); increases the count by 5. Non-static are not standalone and are used with objects, so instead of adding to a non specific count we want to increase the count of cat objects or something. Cat jim = new Cat() jim.count(5); There are now 5 Jims, this is by far too many Jims. It doesn't just have to be objects either, it can also be classes and stuff. In short static is standalone and can just be called thing(); non-static means you need to use it with something like an object cat.thing(); There is more to it, but this is a basic level if I understand it right. I hope this helps a bit.
sreedhar s Level 9, Wellington, New Zealand
21 March 2021
Calculator task is poorly explained particularly for the percent method. Either it should be clarified or taken down from the question altogether. The answer for this one does not seem to be convincing. Quoting a previous users comment on 20 January 2019 and I agree with the comment still in 2021 : "I am a mathematician and I cannot appreciate the logic that b as a % of a is (b/100)*a. b as a percentage of a is always b/a * 100. For the sake of programming the rules of mathematics particularly as a major part of the world understands should not be changed. Please correct this error in your lessons." please update this question!
kar-fai chow Level 8, Hong Kong, Hong Kong
19 November 2020
a quick trick of forcing int to become a double is: int*1.0
13 February 2021
Double is: double numberDouble = (number int) * 1.0; :)
Jordi Claes Level 17
17 February 2021
or just typecast it as a double works as well! (double)a/(double)b
Kiran Nyayapati Level 47, Hyderabad, India
3 April 2022
Or perhaps can be written shorter which looks clean and elegant.

return (double) a / b;
Someone please like my comment. Thanks very much in advance.
Mihai Bone Level 8, Bucharest, Romania
10 October 2020
For "Distance between two points" - I did use Math.pow and Math.sqrt but you have to google it to see how it works (like I did).
Maryem Vickers Level 7, HT..., United Kingdom
7 October 2020
Guys! I have found the key to solving the Calculator task easily! Here's what will help you a lot!; (This is a new concept. Keep that in mind. :).). For the multiply and divide problems, use this to help you;

        double k = (double)b / c;
Get it now?! :>. I hope you do!
Mihai Bone Level 8, Bucharest, Romania
10 October 2020
Yup, if you divide int 3 / int 100 = 0 if you divide double 3 / double 100 = 0.03 . Thank you.
Maryem Vickers Level 7, HT..., United Kingdom
13 October 2020
Np :]
Roman Grygorczuk Level 19, Cracow, Poland
4 September 2020
going fine with for and while loop
Maryem Vickers Level 7, HT..., United Kingdom
5 September 2020
Yes. I agree so MuCh.