Objectives:
We'll use the following sample interaction with the artiste to explain its interface and function. In the sample below, the user's input is in green for clarity.
The ASCII Artiste is now inspired to create!
--------------------------------------------
What size card do you want? (0 or bigger): 9
Which card suit? (diamonds, hearts, spades): diamonds
*
***
*****
*******
*********
*******
*****
***
*
Play again? ("no" to quit) yes
What size card do you want? (0 or bigger): 3
Which card suit? (diamonds, hearts, spades): diamonds
*
***
*
Play again? ("no" to quit) yes
What size card do you want? (0 or bigger): 10
Which card suit? (diamonds, hearts, spades): spades
*
***
*****
*******
*********
***********
***** *****
*** ***
***
***
Play again? ("no" to quit) y
What size card do you want? (0 or bigger): 8
Which card suit? (diamonds, hearts, spades): hearts
*** ***
***** *****
***********
*********
*******
*****
***
*
Play again? ("no" to quit) sure
What size card do you want? (0 or bigger): -3
Humph. Size 10 for you, like it or not
Which card suit? (diamonds, hearts, spades): pinstriped
Humph. I'll just do diamonds
*
***
*****
*******
*********
*********
*******
*****
***
*
Play again? ("no" to quit) yeah
What size card do you want? (0 or bigger): 0
Which card suit? (diamonds, hearts, spades): spades
Play again? ("no" to quit) yup
What size card do you want? (0 or bigger): 6
Which card suit? (diamonds, hearts, spades): spades
*
***
*****
*******
*** ***
*
Play again? ("no" to quit) no
Goodbye!
Your interface should look as much as possible like the sample provided above. In particular, you must:
You are free to add any new shapes that you can figure out how to create algorithmically at an arbitrary size to the menu. For example, "boxes" would be quite easy, while "clubs" would be more tricky. You may also personalize the welcoming, prompting, and departing messages as long as they are clear, informative, and respectful. You should clearly document any extensions in your README.txt file, and always finish the core assignment first and ensure regularly that it still works correctly!
ASCII Artiste Algorithm
We will focus in this section on how the artiste generates images. The
artiste, like all good programmers, is as lazy as possible and creates
the shapes out of reusable pieces.
*** *** ***
********* ******* ***** *** * * *** *****
***** ***** ******* ******* ********* *********
ASCII Artiste Design/API
We have provided an API for the ASCIIArtiste class. You are required
to implement both the public and private fields, constructors, and
methods of this API. You may also add additional functionality in the
form of private or public methods and fields. All code that you
add, whether it is required implementation or optional
additions, must be clear, easily readable, and
documented with JavaDoc. Reusing the javadoc comments provided in the API is
OK, but acknowledge your use of that text at the top of each file.
Any significant or potentially confusing parts of methods should be
documented (such as non-trivial loops and conditionals); code that is
reused in different parts of a class should be abstracted into a
method; and methods that become overly long or complex should be
broken down into simpler methods. Finally, be careful to test that
the entire public interface for every class works correctly.
ASCII Artiste API:
We have provided you with starter code:File
menu select
New->Class
, and name it ASCIIArtiste. Copy the
text of the file that you downloaded into the resulting window.
Command line: Download
the file ASCIIArtiste.java to
whatever directory on your computer you'd like to work from.
To compile from the directory where you installed your code, use:
javac ASCIIArtiste.java
.
To run from the directory where you installed your code, use:
java ASCIIArtiste
.
Description
In this part of the assignment, you will create the "Professional
Magician" program. The professional magician supposedly reads
the "aether" to answer the user's questions.
In fact, your program will use some tricky Google searches to determine an anwser to the user's question without using the "aether" (whatever that is!) or even understanding the question.
We'll use the following sample interaction with the professional magician to explain its interface and function. In the sample below, the user's input is in green for clarity.
Welcome to the Professional Magician's Crystal Ball!
----------------------------------------------------
I am ready to read the aether in my crystal ball and answer your question.
I can answer questions of the following types:
1) What is...Enter the number corresponding to your question or 0 to quit: 1
2) Who is...
3) What was...
4) Who was...
Please complete the question: What is the tallest mountain in the world?
I have scried your answer: the tallest mountain in the world is mount everest.
I am ready to read the aether in my crystal ball and answer your question.
I can answer questions of the following types:
1) What is...Enter the number corresponding to your question or 0 to quit: 6
2) Who is...
3) What was...
4) Who was...
Please enter one of the question types (1-4) or 0. You entered: 6
I am ready to read the aether in my crystal ball and answer your question.
I can answer questions of the following types:
1) What is...Enter the number corresponding to your question or 0 to quit: two
2) Who is...
3) What was...
4) Who was...
Please enter one of the question types (1-4) or 0. You entered: two
I am ready to read the aether in my crystal ball and answer your question.
I can answer questions of the following types:
1) What is...Enter the number corresponding to your question or 0 to quit: 3
2) Who is...
3) What was...
4) Who was...
Please complete the question: What was the answer to problem #5 on last term's CPSC 111 Final Exam
I cannot scry the answer to your query about the answer to problem 5 on last terms cpsc 111 final exam.
I am ready to read the aether in my crystal ball and answer your question.
I can answer questions of the following types:
1) What is...Enter the number corresponding to your question or 0 to quit: 2
2) Who is...
3) What was...
4) Who was...
Please complete the question: Who is the new Prime Minister of Canada
I have scried your answer: the new prime minister of canada is stephen harper.
I am ready to read the aether in my crystal ball and answer your question.
I can answer questions of the following types:
1) What is...Enter the number corresponding to your question or 0 to quit: 0
2) Who is...
3) What was...
4) Who was...
Goodbye!
Professional Magician Interface
The professional magician works in three phases. First, it prints a
banner message to welcome its user. Next, it repeatedly
prompts the user for a question and attempts to answer that question.
Finally, when the user indicates that it's time to quit, the magician
bids the user goodbye.
The magician prompts the user for a question in two phases. First, it determines what type of question the user would like to ask. The magician provides a menu of question types, from which the user selects one. Notice that the magician handles erroneous input from the user without crashing! (In the example, the user enters 6 and then two as menu options, neither of which is legal.)
Once the magician has determined the question type, it asks the user to complete a question of that type. The magician then reads the user's question and attempts to answer it. If it can find the answer, the magician reports the answer (as with the Mount Everest and Stephen Harper answers above). Otherwise, the magician reports the uncooperativeness of the aether (as with the CPSC 111 Final Exam answer). Either way, the magician then prompts the user again for a question type.
Your interface should look as much as possible like the sample provided above. In particular, you must:
You are free to add new question types to the menu (such as "Who wrote..." questions, or an extra option that accesses a more flexible interface). You may also personalize the welcoming and departing messages as long as they are clear, informative, and respectful. Finally, you can improve on the answer reporting (which currently strips all punctuation, converts to lowercase, and so forth), but be careful if you choose to do this! Fixing the punctuation and capitalization of the reported answer should not cause your magician to fail to find answers that it would otherwise find. (If you don't understand what that means by the end of this document, do not attempt to improve on this aspect of the program!) You should clearly document any extensions in your README.txt file, and always finish the core assignment first and ensure regularly that it still works correctly!
Professional Magician Algorithm
We will focus in this section on the question type, question, and
answer portions of the magician.
The magician answers a question in the following steps:
The magician uses "phrase search" so that the search will find those exact words. In Google, text that is not surrounded by quotation marks is a word search: Google will find a page that contains each word, regardless of their order or proximity on the page. Text that is surrounded by quotation marks is a phrase search: Google will find a page that has exactly those words in their given order together on the page.
For our example, the search is for "the tallest mountain in the world is". Note that if you search on Google for "the tallest mountain in the world is" with the quotes, it's a phrase search, and Google will only return pages that include all of those words in the specified order together. If you search for the same phrase without the quotes, it's a word search, and Google will return any page that contains all the words, regardless of their order. (In fact, without the quotes, Google will ignore common words that it calls "stop" words such as "is" and "the".)
Spend a moment right now figuring out how to create a string in Java that looks like "the tallest mountain in the world is" with the quotes. Hint: just "the tallest mountain in the world is" creates the string without quotes. How do you put quotation marks into a string?
Create candidate tiles: For each snippet, the magician will create candidate tiles that are longer and longer word sequences from the string immediately after the query phrase. If our example brought back a first snippet of "the tallest mountain in the world is actually Mauna Kea in Hawaii...", the candidates would be groups of words following the phrase sought: "the tallest mountain in the world". In this case, the first three candidates would be: "actually", "actually mauna", and "actually mauna kea". If the second snippet was "...question anyway, the tallest mountain in the world is Mount Everest in the Himalayas...", its first three candidate tiles would be "mount", "mount everest", and "mount everest in".
The magician will verify each candidate tile with another Google search as follows:
Verify candidate tiles: For each candidate tile, the magician will verify the tile by testing it with Google. The magician will create a string combining candidates with the key word and original phrase, and check the number of hits returned by a Google search of that string. Again, the right kind of search to use is a "phrase search" so that all words in exactly that order together are found. For our example, the searches would be: "actually is the tallest mountain in the world", "actually mauna is the tallest mountain in the world", "actually mauna kea is the tallest mountain in the world", "mount is the tallest mountain in the world", "mount everest is the tallest mountain in the world", "mount everest in is the tallest mountain in the world", and so forth, all with the quotes. The reasoning for this is that, if some candidate x appears as "x is the tallest mountain in the world" and as "the tallest mountain in the world is x", it's likely to really be the tallest mountain in the world! (How often do you think "actually is the tallest mountain in the world" will appear compared to "mount everest is the tallest mountain in the world"?)
Read back over that step. How many loops are involved in sifting through each tile produced from each snippet produced from a user's question?
The APIs for the four classes in the Professional Magician design are:
We have also provided you with starter code (in the form of "stubs":
minimally functional versions of the files) for each of the four files
— see the section entitled Setting Up Your Project
below. However, if you would rather get extra practice, you may
certainly write these yourself from scratch.
Supporting API: the SimpleGoogleSearch
You will use the jar files
SimpleGoogleAPI.jar &mdash and
json.jar &mdash
to access Google. All of these files are
distributed inside ProfessionalMagician.zip. (For
more information on how to use these files, see the section entitled
Setting Up Your Project
below.) The file SimpleGoogleAPI.jar contains a simple API to
search Google using the json.jar JSON interface to the
AJAX
search API.
The class you'll use from SimpleGoogleSearch.api is SimpleGoogleSearch, described in the SimpleGoogleSearch API.
Here are some examples of queries you could try when testing your code:
A SimpleGoogleSearch will actually access
Google to perform searches. You thus need a network connection to test
your code.
Setting Up Your Project
This section includes instructions to help you get started with this
project in Eclipse and from the command line. You are free to use
other development environments for this assignment; however, we do not
have instructions for how to get set up in any other environment!
Setting up in Eclipse: Start by downloading the file
ProfessionalMagician.zip to
your computer. Next, open Eclipse, and make a new Project named
ProfessionalMagician. Under the File
menu
select Import...
. Select Archive file
as
the import source and browse to the ProfessionalMagician.zip
file that you just downloaded. For the Into folder
,
choose ProfessionalMagician and hit Finish
.
Now, your project contains all the necessary code, including the "stubs" discussed in the Hints below), but the
SimpleGoogleAPI.jar and json.jar files are not
on your classpath. Right-click on the
ProfessionalMagician
project and select
Properties
. In the menu on the left, select Java
Build Path
. Select the Libraries
tab on the
right. Click Add Jars...
. You should see
ProfessionalMagician
in the dialog box that pops up.
When you double-click on it, you should see the two jar files
SimpleGoogleAPI.jar
and json.jar
.
Select one of them, hit OK
, and then use Add
Jars...
again to add the second one. Finally, hit
OK
to leave the dialog.
You should now be ready to solve the assignment in Eclipse.
Setting up on the command line: Start by downloading the file ProfessionalMagician.zip to whatever directory on your computer you'd like to work from. Next, unzip the file to that directory. (The lab computers have FilZip for unzipping files; you may have another application.)
To compile from the directory where you installed your code, use:
javac -classpath .;json.jar;SimpleGoogleAPI.jar *.java
To run from the directory where you installed your code, use:
java -classpath .;json.jar;SimpleGoogleAPI.jar ProfessionalMagician
Note the dot at the start of the classpath arguments! That's crucial
so Java knows to look in the current directory for classes and files.
On some computers, for example Macs, you may need to use :
s rather than
;
s in the commands above.
Hints
This is a much larger program than any you've written before in 111.
However, you can simplify the problem by breaking it down into smaller
pieces, solving and testing those pieces separately, and then
putting the pieces together to make a complete program. Therefore,
two of the keys to solving this assignment will be to understand how
to break the problem into pieces and to understand how the pieces fit
together.
Here are some examples of how to break the problem into pieces:
You'll probably want a very simple ProfessionalMagician.java to use as a driver program during all of this. So, check out the next bullet..
Now, how do you put the pieces together? Well, consider the ProfessionalMagician class. In one sense, it is responsible for doing everything in the whole program. On the other hand, it can use the QuestionAnswerer class to do most of the work of actually answering users' questions! Check out the answer method in QuestionAnswerer and think about how much easier it will be to write ProfessionalMagician using that method.
Similarly, a QuestionAnswerer must implement the algorithm described in the Professional Magician Algorithm section above. That's a complex algorithm that requires, among other things, searching for strings on Google, cleaning up text inputted by the user or retrieved from Google, and finding the candidate "tiles" from a Google snippet. However, accomplishing each of those tasks in QuestionAnswerer is easy if it relies on the SimpleGoogleSearch, TextCleaner, and StringTiler classes to help. StringTiler, in turn, will rely on TextCleaner to clean input provided to it.
Read through that description a few times (and draw the relationships
on a sheet of paper!). Once you understand how the pieces fit
together and how to build the individual pieces, you're a big step
toward solving the entire problem!
Reflection Questions
Be sure to answer these reflection questions about Part 2 after you
finish the part. There are no right answers to these questions, and we
don't expect more than a couple of sentences for each
question. However, we will grade on your effort to answer
throughtfully.
/* Author: <insert your name here> Ugrad lab login ID: <insert your ugrad lab login ID here> Date: <insert date here> By submitting this file, I acknowledge that the person whose name appears above is the sole author of this file except as acknowledged in the file below. */