You'll work in pairs on a program to do "Mad Libs". Sometimes you'll be working together at a single computer, sometimes separately on different parts of the same project. At all times, you'll be constantly testing and debugging as you go. When you're done, you should be well-positioned to start learning the core of this class (starting Monday!): Objected-Oriented Programming.
Find a partner now! One of you will be partner "Blue", the other will be "Gold". Decide now who is who!
lab02ic211"on their github account.
So, only the gold partner will do the following:
lab03ic211
Both Partners: On your separate lab machines pull up a shell and
go to your home directory, i.e. cd ~. You must not
be in the ic210 or ic211 directory! give the command:
git clone git@github.com:GoldPartnerGithubUsername/lab03ic211.gitThis should create a directory
lab03ic211. Please
cd into it and ls. You should see a file README.md.
lab03ic211 directory.|
|
$ g++ -o ltest main.cpp stringlist.cpp $ ./ltest Testing stringlist functions ... [expecting 3 : hickory dickory dock] 3 : hickory dickory dockYour job ...
StringNode.java and
StringList.java that work like this:
$ javac StringNode.java StringList.java $ java StringList Testing stringlist functions ... [expecting 3 : hickory dickory dock] 3 : hickory dickory dock
Note1: the point of this is not to produce the
output which, after all is just there for testing. The point is
to port the addtofront, length and listtoarray to Java for you to
use in the next part of the lab!
Note2: Your code should be commented, and
you must include both of your names and alphas at the
top of both Java files!
Note3:
creating a singular class Object is "new Foo()"
as opposed to creating an array which is "new Foo[n]".
[This goes back to that distinction between, builtin types,a
array Object types and class Object types.]
git add *.java # set the .java files to be part of the next commit git commit -m "linked list code" # commit changes to your local copy of the repository git push # push newly committed changes to github's copy of the repo
git pullImportant! Use
ls and verify
that the .java files from the work you did on your partner's lab
machine are actually here, like they are supposed to be. Flag
down your instructor if not!
|
Gold Partner You will create a class WordRead defining a
function
public static String[] get(String fname) { ... }
that takes a filename fname and reads all the strings in
that file, returning them (in order) in an array. Since you
don't know how many strings are in the file, you're going to
have to use a linked list (good thing you have one!) to read
and store, then copy into an array. Unfortunately, you
really need to have things stored in order!
Of course you'll have to thoroughly test this by
writing a "main" for your WordRead class.
Copy file nouns.txt
and have your "main" call $ java WordRead
booger
cat
tennis-racket
souffle
apartment-building
doorknob
whoopee-cushion
iPad
dork
ninja-turtle
One thing you'll need to be able to do is create a Scanner that reads from a file. Without understanding what's going on, here's how to do that:
Scanner sc = null;
try { sc = new Scanner(new FileReader(fname)); }
catch(IOException e) { e.printStackTrace(); System.exit(1); }
You'll need a "import java.io.*;" for that.
I also suggest you take a look at the Scanner class's
hasNext() (see the
Scanner API documentation). The gist is that
sc.hasNext() returns true if there's a "next"
string to be read, and false otherwise. This will tell
you when to stop reading from the input file.
|
Blue Partner You'll create a class named Formatter
and define in it a function
public static void writeInColumns(String[] sta, int cols) { .. }
that
takes an array sta of strings and a positive int cols and
prints the strings from sta out, in order, separated by spaces, but
never using more than cols characters (including spaces!) in a line. So you
have to periodically insert newlines as you go.
Of course you'll have to thoroughly test this by writing a "main" for
your Formatter class that has a hardcoded array with
values
|
git add WordRead.java nouns.txt git commit -m "WordRead class added" git push
git pull git add Formatter.java git commit -m "Formatter class added" git push
git pull
MidLibs defining a program that takes a
filename (as a command line argument) and prints the
words in that file within 35 columns.
If there isn't a filename on the command line (i.e. args has
length 0), you should print the usage message shown below and exit
(System.exit(0) will do this for you).
Download the files
madin01.txt and
madin02.txt
so you have something to run on.
~/$ java MidLibs
usage: java MidLibs <filename> |
~/$ java MidLibs madin01.txt
One of my @nounp came to see me in
my office. He was @adjective with
his @nounp . He asked if there were
any optional assignments he could
do to @verb things. I told him that
I don't believe in @nounp , but
that I was happy to help him @verb
for the final. |
~/$ java MidLibs madin02.txt
A group of @nounp ran into the
@adjective room. I tried to @verb
under the @noun but it was too hard
to @verb. So I gave them my
@adjective @noun asking only that
they @verb somewhere else. |
Important! Any file you have created or
modified during this step should be committed to the local
repository, >pushed the github, and pulled
into the other partner's local copy of the repository.
Assuming that the only file you've added/modified in this step is:
MidLibs.java, you would do this
| Gold partner: | git add MidLibs.java git commit -m "Did Step 5" git push |
Note: If you had to go back and fix a bug or something
in one of the other .java files, you would add that to the
"git add" line. So if, for example, you fixed
a bug in Formatter.java while working on this
step, you would do:
git add MidLibs.java Formatter.java |
| Blue partner: | git pull |
MidLibsFull.java
that extends the previous MidLibs by
replacing each @noun, @verb, @adjective and
@nounp (plural noun) with
a randomly selected word. You read the candidate words in from
the files
nouns.txt,
nounps.txt,
verbs.txt,
adjectives.txt.
A and B are
Strings, you test for equality with
A.equals(B)
... which returns true if the strings are equal, and false
otherwise.
Important! Both partners will submit, and both are expected to keep their repositories sync'd so they commit the same thing! This means you need to follow the same steps as shown in the previous Step to sync your two repositories, but adapt them to the particular files you added/modified in this Step.
submit -c=IC211 -p=lab02 *.java