Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Language: Java Hal9000 class: /** * Hal9000 simulate an AI that interacts with t

ID: 3883751 • Letter: L

Question

Language: Java

Hal9000 class:

/**
* Hal9000 simulate an AI that interacts with the crew
* of a space ship.
*
*/
public class Hal9000
{
private String name;
}

Hal9000Tester class:

In this assignment you will finish the Hal9000 class. You are also provided with a tester that uses the class and calls its methods. Copy both the starter code and the tester.

The Hal9000 needs to remember the name of the crew member it is interacting with. This is the instance variable. The constructor takes a String parameter of the crew member's name and assigns it to the instance variable.

It has methods:

public String getName() which gets the name of the crew member

public void setName(String newName) which sets a new name for the crew member

public String greetCrewMember() which returns a string consisting of "Welcome, name" where name is the name supplied in the constructor.

public String doCommand(String whatToDo) which returns a string consisting of "I am sorry, name. I can't " + whatToDo where name is the name supplied in the constructor and whatToDo is the parameter for this method.

Supply Javadoc for the class, the constructor and all the methods

You can use the String concat method to join strings together, but I find the easiest thing to do is use the + operator. You will learn about that in lesson 4, but I can tell you now. Look at this example of combining two Strings to make a third. I want to combine the phrase "My cat is " with the variable containing my cat's name and then a period.

This is called concatenation.

Explanation / Answer

Tested on Eclipse

/********************************Hal9000.java*******************************/

/**

* The Class Hal9000.

*/

public class Hal9000 {

/** The name. */

private String name;

/**

* Instantiates a new hal 9000.

*

* @param name

* the name

*/

public Hal9000(String name) {

this.setName(name);

}

/**

* Gets the name.

*

* @return the name

*/

public String getName() {

return name;

}

/**

* Sets the name.

*

* @param name

* the name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* Greet crew member.

*

* @return the string

*/

public String greetCrewMember() {

return "Welcome, " + this.getName();

}

/**

* Do command.

*

* @param whatToDo the what to do

* @return the string

*/

public String doCommand(String whatToDo) {

return "I am sorry, " + this.getName() + ". I can't " + whatToDo;

}

}

/*********************************Hal9000Tester.java********************/

/**

* The Class Hal9000Tester.

*/

public class Hal9000Tester {

/**

* The main method.

*

* @param args the arguments

*/

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");

}

}

/***********************output***********************/

Welcome, Dave
Expected: Welcome, Dave
I am sorry, Dave. I can't engage drive
Expected: I am sorry, Dave. I can't engage drive
I am sorry, Aruna. I can't power down
Expected: I am sorry, Aruna. I can't power down

Thanks a lot. Please let me know if you have any doubt.