CodeGym /Courses /New Java Syntax /Practice with static methods

Practice with static methods

New Java Syntax
Level 11 , Lesson 4
Available

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

11
Task
New Java Syntax, level 11, lesson 4
Locked
Class counter
Declare a static int variable catCount in the Cat class. Declare a constructor, i.e. public Cat(), that increases this variable by 1.
11
Task
New Java Syntax, level 11, lesson 4
Locked
Static methods: int getCatCount() and setCatCount(int)
In the Cat class, implement two static methods: int getCatCount() and setCatCount(int), which you can use to get/change the number of cats (catCount variable).
11
Task
New Java Syntax, level 11, lesson 4
Locked
StringHelper class
In the StringHelper class, implement two static methods: - String multiply(String s, int count) - returns a string that has been repeated count times - String multiply(String s) - returns a string that has been repeated 5 times. Example: Amigo -> AmigoAmigoAmigoAmigoAmigo
11
Task
New Java Syntax, level 11, lesson 4
Locked
Controlling body weight
The program should read a weight entered by the user in kilograms and a height in meters. Then it should display a message about the user's body mass index. Implement a static method in the Body class. The method should calculate the body mass index and display the following message: "Underweight: B
11
Task
New Java Syntax, level 11, lesson 4
Locked
ConsoleReader class
In the ConsoleReader class, implement four static methods: - String readString() - reads a string from the keyboard - int readInt() - reads a number from the keyboard - double readDouble() - reads a fractional number from the keyboard boolean readBoolean() - reads the string "true" or "false" from t
11
Task
New Java Syntax, level 11, lesson 4
Locked
Calculator
Make a Calculator class, which will have 5 static methods: int plus(int a, int b) - returns the sum of a and b int minus(int a, int b) - returns the difference between a and b int multiply(int a, int b) - returns the product of a and b double divide(int a, int b) - returns the result of dividing a b
11
Task
New Java Syntax, level 11, lesson 4
Locked
Distance between two points
Implement the static double getDistance(x1, y1, x2, y2) method. It should calculate the distance between two points. Use the double Math.sqrt(double a) method, which calculates the square root of the passed argument. The distance between points is calculated according to the formula: https://javaru
Comments (91)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
larsintech Level 16, Switzerland Expert
22 January 2025
Distance between two points is calculates distance of two points of a linear function using Pythagoras
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 :]