Codewars Signature

I am trying to get an account with Codewars and was surprised that you should show them that you have basic knowledge in one of the proposed programming languages. I chose Java, but stuck in one exercise. Code:

public class Person { String name; public Person(String personName) { name = personName; } public String greet(String yourName) { return String.format("Hi %s, my name is %s", yourName, name); } } 

It says: "Correct this code so that the greet function returns the expected value." The fact is that I do not see an error, and in fact I copied the code in Eclipse and after changing the Java compiler and using version 1.6, the code works, there is no error, and if you try it using the main method, it returns the expected value.

If only they tell you what the expected value is ... When sending, no matter what I try, I always get "The code does not work as expected."

Any ideas?

+6
source share
2 answers

You're right; this code is correct as written, and the Codewars guys are boobies. They probably want you to change names differently.

The OO metaphor for calling the function of an object and passing an argument is to "inform the object about the execution of an action on this object." In other words, if the object is Jim , then Jim.greet("Joe") tells Jim to greet Joe, and "Hello Joe, my name is Jim" is the right thing.

Actually, since a greeting is really a message between two people, the real right thing is not to pass the string name to greet , but to pass Person and have a greet call that Person beGreeted() .

+11
source

Answer: Justyou need to swap the variables in String.format ().

Question:

return String.format ("Hello, ss, my name is% s", name, your name);

Answer:

return String.format ("Hello, ss, my name is% s", yourName, name);

  public class Person { String name; public Person(String personName){ name = personName; } public String greet(String yourName) { return String.format("Hi %s, my name is %s", yourName,name); } } 
+2
source

Source: https://habr.com/ru/post/983393/


All Articles