CS 161 Lab C - The Ventriloquist
Due Fri Sep 20 at 9:00am
Overview
In this lab, you will be implementing a program to create a simple Ventriloquist routine. The routine will be performed in the console: that is, actions will be printed out as text. Your routine might look something like this:
Introducing: Jack and the amazing Alice Jack says 'Hello' Alice says 'Hello yourself!' Jack says 'That isn't very nice' Alice wiggles her ears Jack says 'What to see a trick?' Alice says 'Sure!' Jack starts chugging a glass of water Alice says 'And his lips aren't even moving!' Jack stops chugging a glass of water Jack says 'Ta da!' Jack puts down Alice and picks up Bob Jack says 'Hello' Bob says 'Hello yourself!' Jack says 'That isn't very nice' Bob says 'I don't care!' Bob sticks out his tongue Jack says 'You're mean, and I'm changing your name to mud' Mud says 'Phooey'
You are of course welcome to modify the exact details of the routine, as long as you meet the requirements described below. Your routine will utilize two new classes that you will create: a Ventriloquist class and a Dummy class (to represent the puppet).
Remember that this lab should be completed in pairs. You will need to find a different partner than you had for the last lab. Be sure to review the pair programming guidelines before you begin; remember to switch off who is driving and who is navigating!
Objectives
- To practice defining Java classes from scratch--including instance variables, constructors, getters/setters, and object methods.
- To practice calling methods you have defined.
- To practice passing Objects as parameters.
- To practice implementing a main() method as you program in order to demonstrate your program
Necessary Files
You will be creating your own program from scratch for this assignment---select "Project > New Project" from the BlueJ menu to create a new project.
Assignment Details
For this program, you will need to create three (yes, 3!) new classes: Ventriloquist
, Dummy
, and VentriloquistRoutine
. The first two of these are object classes--that is, they are classes intended to be instantiated as objects. The last is a main or tester class--that is, it just has a main()
method and runs a program that uses the other two classes.
The individual classes are detailed below; you should read through the whole assignment to get a sense for how things fit together. You will want to be developing your VentriloquistRoutine class at the same time you are developing the other two classes!
- Helpful hint! Be sure and review the Vocabulary list on Moodle. It contains the "code templates" we've talked about in class.
Dummy
The first class you should create is the Dummy
class. This is the easiest of the classes to make. As you read the requirements, think about what attributes and method signatures the class will have!
-
Each
Dummy
will need to have a name. - Each
Dummy
also has a button hidden at the bottom that performs a specialAction. In the example above, Alice's button "wiggles her ears", and Bob's button "sticks out his tongue." These actions are written as Strings, and should be remembered (hint: as an instance variable!) by each Dummy object. - Your
Dummy
class will need to have a constructor that takes in the name and special action (whenever you construct/instantiate a Dummy, you give it a name and specify what the button does!) - Your
Dummy
class should have a getter and setter for the name--that way you can ask the Dummy what its name is, and change the Dummy's name later! -
Your class should also have a
doSpecialAction()
method. This method "performs" the special action--it does this by printing out that the Dummy has done the action. For example:Alice wiggles her ears
Would show that the Dummy named "Alice" has performed the "wiggles her ears" action.
Be sure to test your Dummy class as you write it--make sure you can instantiate a Dummy, call methods on it, and that everything works the way you want!
Ventriloquist
The Ventriloquist
is more complex, but not by much (it just has more methods!) As you write the methods, think about their signature: do they need any parameters? Do they need to return any values?
- Each
Ventriloquist
will need to have a name. -
In addition each
Ventriloquist
will need to have aDummy
object--the puppet it is currently using! This Object should be stored as an instance variable; remember that Objects have a dataType that is the same as their class name (so apuppet
variable may have a dataType ofDummy
). -
Your
Ventriloquist
class will need to have a constructor. Your constructor should take in the name of the ventriloquist.-
Watch Out: Because Ventriloquists aren't created with a Dummy, it is possible that the box for the Ventriloquist's Dummy instance variable will be empty. In computer science, we say that an empty variable does contain a value, a special value called
null
.If you try and call a method on the value inside an empty box (so you try and tell the Dummy to do something, when there is no Dummy in the box), you will get what is called a
NullPointerException
--you've pointed atnull
and told it to do something, andnull
can't do anything (because it is nothing!). If you get this error, make sure that you've put a value in the variable before you call a method on that variable!This is a new concept and can be confusing. Please let me know if you have any problems or need help with this!
-
-
Your
Ventriloquist
class should have a getter for its name (but no setter is needed--we don't need to change the Ventriloquist's name)! -
It should also have a getter and setter for the
Dummy
instance variable--this will let you change what Dummy the ventriloquist is using. Remember thatDummy
is the dataType for aDummy
object! -
Your
Ventriloquist
should also have atoString()
method so you can print out the object. Printing the Ventriloquist should produce something like:Jack and the amazing Alice
(where Jack is the Ventriloquist and Alice is the Dummy).Remember, a
toString()
method does not actually print anything, it just returns a String that can be printed elsewhere (like from the VentriloquistRoutine)! -
Your
Ventriloquist
should have asay
method that has the ventriloquist speak. This method should take in the line that the ventriloquist will say, and then print out that it says that line. For example:Jack says 'Hello'
Note that you will need to print out the quotation marks the same way you printed out a $ in your Banking program. Hint: Print out single quotes (') not double-quotes---you need to do something special to print double quotes, which we'll talk about later!
-
Your
Ventriloquist
should also have aventriloquize
method that has the puppet speak a given line by printing it out. For example:Alice says 'Hello yourself!'
You will need to "ask" the puppet its name in order to print that out, but otherwise this method will work much like the
say
method. -
Your
Ventriloquist
should have auseSpecialAction
method that tells the puppet to perform its special action (that is, the Ventriloquist hits the secret button). This method should call the puppet'sdoSpecialAction
method. -
Your
Ventriloquist
should have 3 additional methods that will allow it to do the classic trick of having the puppet talk while the ventriloquist is drinking a glass of water.It should have a
startDrinking
method that prints out that the ventriloquist has started drinking:Jack starts chugging a glass of water
and a
stopDrinking
method that prints out that the ventriloquist has stopped drinking:Jack stops chugging a glass of water
Finally, the
Ventriloquist
should have adrinkAndSpeak
method that takes in a line to say. This method should have the ventriloquist start drinking, ventriloquize the line, and then stop drinking.-
This method should call the previous methods you wrote!
You can call a method on the current object by using
this.myMethod(<appropriate parameters>);
-
This method should call the previous methods you wrote!
You can call a method on the current object by using
That's the full list of methods! Make sure you get them all (you should have 11 in total, counting the constructor)
VentriloquistRoutine
This class is much simpler than the others: as a "tester" class, it only has a single method: the main
method (just like your BankingTester class did!)
- Your Routine should instantiate a
Ventriloquist
object ("Jack" in the above example), as well as twoDummy
objects ("Alice" and "Bob" in the above example). -
Your method should then work through a routine similar to the one in the example. You should be sure and have the Ventriloquist use both puppets (switching the middle of the show), and to do all its tricks: using the Dummys' special actions, doing the drinkAndSpeak trick, etc.
- You are welcome to be creative with this! You may be able to earn extra credit for an entertaining routine (as long as it shows off all the functionality).
- Printed content should almost always occur because you called one of the Ventriloquist's methods, not because you printed out the routine directly! The goal is to work with variables (so that you can easily mix up the routine later if a show doesn't go over well).
- Remember! The Dummy doesn't actually ever do anything--the Ventriloquist does all the work (it just looks like the dummy is doing it). In fact, you should almost never need to call a method on the Dummy objects (except possible to ask them for and change their names).
-
Helpful hint:
You can make a blank line appear in your routine by printing out an "empty String". This is text that has nothing inside it, not even a space! The empty String is written as
""
--a String with no actual characters in it! So printing out a blank line is done like:System.out.println(""); //blank space
Submission
Make sure both your names are on all the classes (Dummy
, Ventriloquist
, and VentriloquistRoutine
), and upload the entire project directory to the LabC submission folder on hedwig (see the previous lab if you need help). Only one partner needs to upload the code. Make sure you upload your work to the correct folder! The lab is due at the start of class the morning after lab.
After you have submitted your solution, log onto Moodle and submit the Lab C Partner Evaluation. Both partners need to submit evaluations.
Grading
This assignment will be graded on approximately the following criteria:
- The Dummy class implements the required methods [25%]
- The Ventriloquist class implements the required methods [50%]
- The VentriloquistRoutine class demonstrates all of the program's functionality [15%]
- Code is formatted correctly (good indentation, names on the files, reasonable comments, etc). Select "Edit > Autolayout" from the BlueJ menu to automatically fix the indentation! Remember to give variables sensible names, and to make sure that capitalization is correct! Include a few comments if you can. [5%]
- Submitted a lab partner evaluation [5%]
Remember that it is very easy to get partial credit, so turn in whatever you have before the lab's due date!