Question: Language: Java Modify both the constructor and the setName method as follows Restrict the name to 8 characters. If the name contains more than 8
Language: Java
Modify both the constructor and the setName method as follows
Restrict the name to 8 characters. If the name contains more than 8 characters, set the first 8 characters as the crewmember's name
If the parameter is Robert, set the name to Bob
Hal9000.java: /** * Hal9000 simulate an AI that interacts with the crew * of a space ship. * */ 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:
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"); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
