CS 161 Homework 1 - Chattybot
Due Fri Jan 30 at 11:59pm
Overview
In this first assignment, you will be programming some behavior for a chatty virtual robot. You will be filling in methods that give the robot the ability to "speak" by printing text to the BlueJ Terminal. This will give you practice with the "code/compile/test" cycle for developing software, as well with printing and working with Strings of text.
This assignment should be completed individually (not in pairs). If you get stuck please ask me for help. You can also ask questions of your classmates, but remember the Gilligan's Island rule!
Objectives
- To practice writing source code
- To practice with printing and Strings
- to practice with the BlueJ IDE
Necessary Files
You will need to download the Hwk1.zip file, which contains the BlueJ project for you to complete. Remember to unzip the contents from the file!. You should save the unzipped project to your Alexandria drive for safe-keeping.
The project contains one class: Chattybot
. The class has a number of methods declared, but they currently are empty and don't do anything. You will need to fill in their functionality.
If you haven't yet, you will also need to download and install the BlueJ IDE
Assignment Details
Again, as we're just getting started I'll help walk you through the steps to complete this assignment. Be sure to read the instructions carefully--they contain all kinds of good advice!
Setting up the BlueJ Terminal
Before you begin, you'll want to make sure that BlueJ is configured to work properly for this and future assignments. In particular, you'll want to make sure that the Terminal is behaving correctly.
- Launch BlueJ and open up the Chattybot project (double-click on the
package.bluej
file inside the folder). - From the View menu, select "Show Terminal". This will make the terminal window appear. Note that it will also show up the first time you try and print something.
-
Make sure the Terminal has "focus" (it is the currently selected window) and access the Options menu. You will want to make sure a couple of settings are correct:
- "Clear screen at method call" should be checked for now. This will let you see just the results of a single method. However, if you want to have a full conversation it will make sense to uncheck this.
- "Record method calls" should be unchecked. This will list what methods you called--but you should already know that (and so just adds extraneous information).
- "Unlimited buffering" should definitely be checked. This will let you see LOTs of printed text, and will be very useful for later assignments.
Writing Code
The rest of the assignment involves filling in methods in the Chattybot class. Double-click on the orange Chattybot box to view the source code. You will be writing Java code in here.
- For each method, find the block (the braces {}) and put your new source code in there.
- Be sure and indent your new code to keep things readable. If you go to Edit > "Auto-layout" in the BlueJ menu, BlueJ will automatically fix your indentation!
Remember: write one method at a time, testing as you go. Complete a method, then run your program to make sure it does what you expect! You can run your program by following the below algorithm:
- Click the "Compile" button to compile your source code
- If there are any compile errors (and there often are), you will need to fix them. Go back to Step 1
- Right-click on the orange Chattybot class and select
new Chattybot()
. This will create a new Chattybot for you to interact with. - In the workbench, right-click on the red Chattybox object to select the method you wish to call.
- Call the method and look at the output. Does it do what you expect?
- If it doesn't, there is a logical error--go back to the source code and modify it make the code do what you want. Then return to Step 1.
- When everything works, go on to the next method
Get used to this "debugging cycle"; we'll be doing it a lot throughout the class! Remember that each step is important--particularly Step 6 (checking that it works as you expected).
Chattybot's Methods
You will need to add functionally to the following methods in the Chattybot class:
-
public void sayHello()
When this method is called, Chattybot should print out "Hello there!". Your output should look exactly like the below example:
Hello there!
You can use the
System.out.println()
method we discussed in class. -
public void sayCatchphrase()
When this method is called, Chattybot should print out its catchphrase:
My catchphrase is: 'Computer science is for everyone!'
You are welcome to substitute in your own catchphrase if you wish :)
-
public void discuss(String topic)
When this method is called, Chattybot will print out a generic question regarding the topic specified in the parameter. For example, if this method is called with
"programming"
as the parameter, then it should print out:Tell me more about programming!
In practice, the word "programming" should be replaced with whatever the method caller typed into BlueJ. Note that the caller can type in more than one word (e.g., try "computer science").
- This method will require you to work with the method parameter. Remember that the parameter is just a variable containing whatever value the caller typed into BlueJ--you can use that variable inside your code, such as printing it out.
-
In order to print multiple things on one line, you can use the
System.out.print()
method. This method works exactly likeSystem.out.println()
but it doesn't put a line break at the end (imagine that you didn't hit "enter" after typing in the String). - Don't forget the exclamation point at the end! Chattybot is very enthusiastic.
- Note that this kind of technique was actually used for early chat programs, such as the classic ELIZA
-
public void sayHelloTo(String firstName, String lastName)
This method will allow Chattybot to greet a specific person, for example:
Hello Joel Ross, it's great to meet you!
Once again, you will need to use the method parameters--but now there are two of them!
- Be careful about the spaces and the comma. Remember, your output should be formatted exactly like the above example.
-
public void shoutName()
When this method is called, Chattybot will shout out your (the programmer's) name, written in big starry letters:
*** * * * ***** ***** * * * * * * * * * * * * ***** * * * * * * * * ***** * * * *** * * * * * * *
(This would be if Chatty wrote its own source code!)
- This can be a challenge! You should plan out your approach before you start coding. Try drawing out what your name will look like on a sheet on graph paper, or otherwise counting how many stars you will need to use. You can also try printing out examples and then using the "code/compile/test" cycle to make sure you get the right output.
-
public void sing(int bottles)
When this method is called, Chattybot will sing a verse of the classic children's song, "99 bottles of beer":
99 bottles of beer on the wall, 99 bottles of beer. Take one down, pass it around 98 bottles of beer on the wall.
- You will need to use the parameter yet again--remember that you can print out a number (an integer) the same way you do a String!
- You may also consider using concatenation between the number and the String--joining them together using the
+
operator. - Finally, you will need to use (gasp) subtraction for this method! You should make a new local variable called
nextNumber
that stores the number of bottles after one has been taken down. Then use that variable instead of the parameter in the final line.
There are quite a few methods to write for this assignment! However, each should only be a few lines of code--try to do one or two each day and you should be fine.
- If you get stuck with a syntax or compile error, please let me know! Don't worry about if things don't work the first time--this is a foreign language, an no one gets it right straight off the bat.
Submitting Your Assignment
There are a couple of details to keep in mind before you submit your assignment.
-
Make sure your Chattybot program works--you should be able to call all the methods and get the desired output. Make sure you can call them one after another and everything works.
- If your program doesn't compile, I can't give you credit for it!
-
You will also need to fill out the README.txt file. You can open this file in BlueJ by double-clicking on the white paper icon in the upper left corner of the class window. You should place the answer to each question below the question box, replacing the "<Replace this with your response!>". Remember to save your changes (select "Class > Save" from the menu).
-
You should compress the entire Chattybot folder into a single zip file (like you did for Lab A), and then submit it to the Hwk1 Submission page on Moodle.
- Be sure an upload the ENTIRE Chattybot folder--that is what includes all your work! The "package.bluej" file is deceptive--it isn't actually the project or package, just a file that is used to open BlueJ! If you just give me that file, I won't be able to see your work.
- This assignment is due at midnight on Fri, Jan 30.
Grading
This assignment will be graded out of 12 points:
- [1pt] Chattybot can say hello
- [1pt] Chattybot can say its catchphrase
- [1pt] Chattybot can discuss a provided topic
- [1pt] Chattybot's discussion is formatted correctly (e.g., with an exclamation point at the end)
- [1pt] Chattybot can say hello to a particular person
- [1pt] Chattybot's individual greeting uses both names and is formatted correctly
- [2pt] Chattybot can shout the name of its creator
- [1pt] Chattybot can single a verse of '99 bottles'
- [1pt] Chattybot can sing a verse based on the number provided by the caller
- [1pt] You calculate the number of remaining bottles using a new variable
- [1pt] You have completed the included README.txt file