This is the lab quiz from last semester
CS 101/101-E, Fall 2004
Lab quiz 3
Week of 6 Dec 2004
General notes
A couple of notes on the lab quiz:
- The activities of this week are not to be discussed outside of lab. The
quiz is open textbook AND you are allowed to use your third midterm as well.
- The lab assistants may not help you design, write, or debug the
programs.
- If the program does not compile, you will get a very, very low
grade. There is a compile button on the submitter - please use it
to make sure your submission compiles.
- To maximize your points, make sure your programs compile
successfully. If things are going bad near the end, you might want to
try commenting out errant statements (i.e., put a // in front of
them). It is better to have only part of the program working
rather than it not compiling.
- The program must follow the standard "good grading guidelines" as
discussed in lecture. In particular, the header must include your
name, e-mail ID, and whether you are in 101 or 101-E. The
commenting, whitespace, variable names, and line length should all be
appropriate. No legend or echoing of input is necessary for this
lab quiz.
Purpose
For this lab quiz, you are to submit
a single file named ArrayManipulation.java
which contains an ArrayManipulation class. No template is being
given
for
this lab quiz. The class must have only three methods, which are
described in more detail below: reverse(), has(), and print(). You are welcome to
put in other methods (in particular, a main() method), although they
will not affect your grade.
Methods
The class should have the following three methods. All methods
need to be public AND static.
- has(): this method
takes in two parameters, an int
array and an element to search for in that array. If the element
is found in the array, the index where the element is located is
returned (as an int).
If the element occurs multiple times in the array, only the index of the
first occurrence is to be returned. If the element is not found in the array, -1 is returned.
- reverse(): this method takes in an int array, and returns a
NEW array of type int[]
(i.e., the method return type is int[]). The returned array should have the same elements as the
passed array, but in reverse order.
- print(): this method takes in an int array as the parameter, and
returns no value. It prints the array on a SINGLE line, with
spaces separating the values.
Grading
Make sure each of the methods are named EXACTLY as specified.
The names of the other methods, are used by our test code to grade your
assignment. If they are not exactly as specified,
you will get a zero for pretty much all of the lab quiz.
main() method
There is no main()
method required in the ArrayManipulation class - you are welcome to include one
for testing purposes (it will not affect your grade one way or the other if it
is included). A sample main()
method you may want to put in your ArrayManipulation.java class is shown below with the corresponding output.
Sample usage
The following main()
method will produce the following output
Sample main()
method:
public static void main (String
args[]) {
int[] array1 = { 1, 2, 3, 4, 5 };
System.out.println ("Array has element 3: " +
has(array1, 3));
System.out.println ("Array has element 6: " +
has(array1, 6));
System.out.print ("Original array: ");
print(array1);
int[] array2 = reverse (array1);
System.out.print ("Reversed array: ");
print(array2);
}
Output:
Array has element 3: 2
Array has element 6: -1
Original array: 1 2 3 4 5
Reversed array: 5 4 3 2 1
Submission
Submit only the ArrayManipulation.java
file
here.