Pledged lab quiz
- This quiz is timed -- you have 90 minutes to complete it.
- The only "good programming practice" that is required for
this lab is to include your name and e-mail ID in the header
comments. You are welcome to include more, but make sure you
get the programs working first, as the quiz is timed.
- The quiz is open textbook. You may NOT use previous assignments.
- The activities of this week are not to be
discussed outside of lab. You are to produce one program, as described
below. A templates file is
available:
- LabQuiz3.java (right click on the
link, select "save as...", then save the file to your home directory)
Advice
- Remember neither the lab assistants nor other students may
help you design, write, or debug the programs. If you are
having a problem getting JCreator to work right (which does NOT
mean a compilation error), or an error with the submissions,
then you may ask a TA.
- To maximize your points, make sure your programs compile
successfully.
- If towards the end things are not going as well as you
would want (i.e., the program does not compile), it might be
best to try commenting out errant statements (i.e., put a //
in front of them)
- If you run out of time, submit what you have -- there will be partial
credit for this quiz. Also remember that you can submit a file as many
times as you want -- only the most recent submission is graded. So
once you have it working, submit what you have.
The LabQuiz3 class
Complete and
submit program
LabQuiz3.java (right click on the link, select
"save as...", then save the file to your home directory).
This class contains a single instance variable (named, conveniently,
array), which is an int array. The class will
contain a number of methods to manipulate that int array.
Your task is to complete the following methods. It is recommended that you first get the constructor working, followed by
toString(), followed by the rest of the methods in this class.
- public LabQuiz3 (int size): the constructor, it initializes
the array field to an int array of the
passed size. Note that
there is no default constructor in this class.
- public void setValue (int which, int towhat): sets a value in
array. The value at location which is set to towhat. You do
not have to check if which is out of bounds.
- public int getValue (int which): returns the value in array
at
location which. You do not have to check if which
is out of
bounds.
- public int search (int forwhat): searches array
for the passed
value. If the value is found, then the location of the value is
returned (not the value itself). If it is not found, then -1 is
returned. If the value occurs more than once, then the location of
the first occurrence of the value is returned.
- public String toString (): returns a String
representation of the
array. The returned String should start with a square bracket,
list each number followed by a comma and a space, and then a close
square bracket. See below for examples of what the toString()
method returns. Note that we don't care if you have a comma after
the last element or not.
Sample Output
Once working, your code should produce the following output. The
provided main() method helps the testing of your code.
Testing LabQuiz3(): the next line should be 20 0's
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]
(this also requires toString() to work properly for it to display correctly)
Testing setValue(): the next line should be the squares of 1 to 15, followed by 5 0's
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 0, 0, 0, 0, 0, ]
(this also requires toString() to work properly for it to display correctly)
Testing getValue(): should be 36: 36
Testing getValue(): should be 0: 0
Testing search(): should be 4: 4
Testing search(): should be -1: -1
Testing search(): should be 15: 15
Submission
Submit LabQuiz3.java
here.
If you finish early...
Please take some time to do lab 12 if you
finish early.