1

I am having a bit of trouble getting started on this Java assignment, and was hoping to get some guidance from you guys. My issue is pretty simple and straightforward... How do I take the tester method's input and start using "Dave" as the new name? How do I make Dave the instance variable and how do I start using that in the setName() and greetCrewMember() methods? After that's done, how would I assign Aruna? The main thing tripping me up is the instance variable and calling it in the methods. Thanks for any help given!

public class Hal9000
{
    private String name;
    public static void main(String[] args)
    {
    Hal9000 hal = new Hal9000("Dave");
    System.out.println(hal.greetCrewMember());
    System.out.println("Expected: Welcome, Dave");
    System.out.println(hal.doCommand("engage drive"));
    System.out.println("Expected: I am sorry, Dave. I can't engage drive");
    hal.setName("Aruna");
    System.out.println(hal.doCommand("power down"));
    System.out.println("Expected: I am sorry, Aruna. I can't power down");
    }

    public String getName()
    {

    }

    public void setName(String newName)
    {
        String name = newName;
        return name;
    }

    public String greetCrewMember()
    {
        String message = "Welcome," + name ;
        return message;
    }

    public String doCommand(String whatToDo)
    {

    }
}

3 Answers3

1

The only thing you have to do is to define a parameterised constructor-

Hal9000(String name)
{
this.name=name;
}

This will cause your statement-

Hal9000 hal = new Hal9000("Dave");

to execute correctly and set the name to Dave. After this you can set the name to anything else by your setname method.

You would also want to define something in your getname method.

RafatMunshi
  • 145
  • 9
  • Ok, that makes a lot more sense and it worked! Thank you so much! Do you mind quickly explaining the "this.name=name;" I don't believe we've learned about "this" so that's pretty new to me. What exactly is it doing? –  Sep 16 '17 at 05:12
  • this pointer is used to refer to the current instance we are talking about. Like here u can see the argument of the method is called name which i want to set to the name variable of the instance. To make that difference i use this.name to show that its to set the instance variable name with the name passed to the argument. – RafatMunshi Sep 16 '17 at 05:14
0

You should use the field name within the constructor and use this to instantiate it within the setter and constructor.

Hal9000(String name) {
    this.name = name;
}

further instantiate as

Hal9000 instanceForDave = new Hal9000("Dave"); // would set the 'name' for this instance as 'Dave'
Hal9000 instanceForAruna = new Hal9000("Aruna");

The setter implementation should ideally also make use of this name only, something similar to the constructor in your case as:

public void setName(String newName) {
    this.name = newName;
}

and then when you need to fetch the name attribute of the class, the getter would be helpful as

public String getName() {
    return this.name;
}
Naman
  • 27,789
  • 26
  • 218
  • 353
  • 1
    Thank you! It works and it's very clear now. I am still kind of lost on the "this" portion of the code. What exactly is it doing? –  Sep 16 '17 at 05:13
  • @HenFish It refers to the current object. Do read more about it - https://stackoverflow.com/questions/3728062/what-is-the-meaning-of-this-in-java – Naman Sep 16 '17 at 05:14
0
public class Hal9000
{
    private String name;

    public Hal9000(String[] stringArray) {
        name = stringArray[0];
    }

    public static void main(String[] args)
    {
    String[] stringArray = { "Dave"};
    Hal9000 hal = new Hal9000(stringArray);
    System.out.println(hal.greetCrewMember());
    System.out.println("Expected: Welcome, Dave");
    System.out.println(hal.doCommand("engage drive"));
    System.out.println("Expected: I am sorry, Dave. I can't engage drive");
    hal.setName("Aruna");
    System.out.println(hal.doCommand("power down"));
    System.out.println("Expected: I am sorry, Aruna. I can't power down");
    }

    public String getName()
    {
        return this.name;
    }

    public void setName(String newName)
    {
       this.name = name;
    }

    public String greetCrewMember()
    {
        String message = "Welcome," + getName() ;
        return message;
    }

  public String doCommand(String whatToDo) {
    return whatToDo;

  }
}
Dewey Banks
  • 323
  • 1
  • 7
  • 19