Question
Language: Java Hal9000 class: /** * Hal9000 simulate an AI that interacts with the crew * of a space ship. * */ public class Hal9000
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:
public class Hal9000Tester { 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"); } }
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.
String cat = "Corky" String message = "My cat is " + cat +".";
This is called concatenation.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started