Greetings to all experts,

I am a newbie here in Java OOP and is struggling in practicing how to code a OO program, using Java. I hope anyone can give me a head start to enable me to write the main method to test the 2 methods.

Here's the question:

The following two methods changeName1 and changeName2 attempt to update the value of the variable name. Write a main method to test which of them successfully does its task. Explain the result.

public class Human {
String name;
public void setName(String string) {name = string;}
public String getName() {return name;}
static void changeName1(Human in,String newName) {
Human another = new Human();
in = another;
in.setName(newName);
}
static void changeName2(Human in,String newName) {
in.setName(newName);
}
}

Thank you so much.

What have you tried so far?
First write a main method. Make it simple with a println for testing. Compile and execute the program to see if it executes and prints out ok.
Then look at the first method you are to call.
Consider the following:
What arguments does it take?
What does it return?

commented: Good advice for a beginner on how to get started on a homework assignment. +5

Suppose you created an instance of the class Human, and gave them a name:

Human h = new Human();
		h.name = "Adam";
		System.out.println("Hi!  My name is " + h.getName());

If you then called one of the "change name" methods in your example, how could you see this person's name?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.