0

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.

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by JeffGrigg
1

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?

Votes + Comments
Good advice for a beginner on how to get started on a homework assignment.
0

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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.