210k views
0 votes
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.

Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied.

In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit.


Consider the following Thing class. Each Thing object has a name attribute, which can be set in the constructor or by using the setName method. The name of a Thing object can be returned by the getName method.


public class Thing


{


// attributes not shown




/** Constructs a new Thing named myName


*/


public Thing(String myName)


{ /* implementation not shown */ }




/** Returns this Thing’s name


*/


public String getName()


{ /* implementation not shown */ }




/** Sets this Thing’s name to newName


*/


public void setName(String newName)


{ /* implementation not shown */ }




/** Returns a message as described in part (b)


*/


public void printMessage()


{ /* implementation not shown */ }


}


(a) Write a statement to create a new Thing object snack that has the name "potato chip".


Write the statement below.


(b) The Thing method printMessage prints a string consisting of the name of the object followed by "_is_great". Suppose the name of the Thing object favFood is "pizza". Write a statement that uses the printMessage method to print the string "pizza_is_great".


Write the statement below.


(c) Write a code segment to change the name of the Thing object something such that the new name consists of the old name with one character removed at random. For example, if something has name "ABCD", its new name could be set to "ACD".


Write the code segment below

User Rushikesh
by
8.1k points

1 Answer

7 votes

Final answer:

To create a new Thing object named snack with "potato chip" as its name, use Thing snack = new Thing("potato chip");. The printMessage method for an object named favFood prints "pizza_is_great" when called. Changing the name of something by removing a random character involves using the Random class and string manipulation methods.

Step-by-step explanation:

To create a new Thing object named snack with the name "potato chip", you would write the following statement:

Thing snack = new Thing("potato chip");

To use the printMessage method to print "pizza_is_great" for an object named favFood, the statement would be:

favFood.printMessage();

Lastly, to change the name of a Thing object named something by removing one character at random, you could write a code segment like this:

import java.util.Random;
//Assuming 'something' is an existing Thing object
Random rand = new Random();
String originalName = something.getName();
int indexToRemove = rand.nextInt(originalName.length());
String newName = originalName.substring(0, indexToRemove) + originalName.substring(indexToRemove + 1);
something.setName(newName);
User Zasaz
by
8.8k points