Fall 2013 CSCI 202 in-class lab 2
This is a directed lab in which you will follow along with the instructor to perform several task.
- Follow the directions in the NetBeans FaqChangeUserGlobal so that your user name will
look something like
George Washington <gwashing@unca.edu>
in your programs. - If you have an USB stick, plug it in your computer and we’ll see if we can use it.
- Use NetBeans to create a new project. The main class
for the project must
be stored in the package
edu.unca.cs.csci202
and be namedLab02
. This means that you must useedu.unca.cs.csci202.Lab02
as the name of the main class in the Name and Location window. Because the instructor uses scripts to test your programs, he is very particular about the package and class name. Always use the ones specified in the homework description. -
Let’s set ourselves up to use the
PrintStream
andScanner
classes for input and output. First, add the following two imports to your program, right after thepackage
statement.import java.util.Scanner ; import java.io.PrintStream ;
Now add the following statements at the beginning ofmain
.Scanner inStream = new Scanner(System.in) ; PrintStream outStream = System.out ;
To make sure your I/O is working, put this loop in the body ofmain
while (inStream.hasNext()) { String word = inStream.next() ; outStream.println(word) ; }
At this point you should be able to type in words and see them echoed, one word per line. When you are tired of running your program, be sure to terminate it properly. Otherwise you may find yourself running dozens of copies of your program at the same time. -
Eventually, your program will read commands, consisting of several words,
and executing them using an
ArrayList
. For now change the innards of yourwhile
to look for a command called addatend. Look at this closely and make sure you understand what is happening.String command = inStream.next() ; if (command.equals("addatend")) { outStream.println("addatend") ; } else { outStream.println("BAD COMMAND: " + command) ; }
-
You need to start working a bit on your own now.
Your task is to declare and initialize an
ArrayList
for holdingString
s into your program. They will require animport
, declaration, and initialization. Useitems
as the variable name for your so that it will work well with some of our examples. -
Now we are going to work on completing the implementation of the
addatend command by augmenting the
if
body insidemain
.-
The “user” will enter a new item after the command
addatend, something like the following:
addatend fred
Within theif
of your program add the following statement to store the new item into aString
variable calledvalue
.String value = inStream.next() ;
You should be able to figure our where this line needs to go. -
Follow this with a single Java statement to add
value
at the end ofitems
. -
You can run your program now, but it won’t tell you
anything useful. You need to add the following loop after
the
if
—else
to print the values ofitems
.outStream.printf("List has %d items\n", items.size()) ; for (String e : items) {outStream.printf(" %s\n", e) ; }
Take a look at that call to theprintf
method. You need to masterprintf
to produce beautiful output.
-
The “user” will enter a new item after the command
addatend, something like the following:
-
Now add an addatposition command.
In this command our “user”
will enter both a new value and a position for the value, as in:
addatposition 2 wilma
This means you ll need something like the following inside yourelse if
statement.int position = inStream.nextInt() ; String value = inStream.next() ;
- Add a command getatend that expects no arguments from the “user”. This command should print the value at the end of the list.
- Add a command removeatend that removes the last value of the list. This command should also print the value at the end of the list. This code will be very useful for completing Homework 2 and its implementation is very similar to that for removeatend
- If there is any time left, add commands getatposition and getatend. These will mostly be cut-and-paste from the remove operations.