← Unit 3 · all exercises
Saved in this browser only

Changing a name

Write a Person class with a name. The constructor takes it. setName replaces it, and getName returns it. Also write greet, which returns "Hello, " followed by the name.

class Person { Person(String name); void setName(String name); String getName(); String greet(); }

Person.java
Loading editor…
Results

Examples

  • new Person("Ada").greet() → Hello, Ada
  • Person p = new Person("Ada"); p.setName("Grace"); p.getName() → Grace

There are also 3 hidden tests you can't see. Solve the problem, not the examples.