ulrail.gif

Lab Quiz 3

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Slides | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif spacer.gif

Pledged Lab Quiz

This lab quiz is pledged.  You may only use the following materials for this quiz:

This means you cannot use any files in your home directory, previous assignments, slides, etc.  However, you should still save your file for this lab quiz into your home directory.

The TAs cannot help you with Java-related issues on this quiz.  If you are having problems getting JCreator to work, can't log in to the machines, etc., then you are welcome to ask the TAs for assistance.  If you do not understand what the lab is asking, the TAs may be able to help you (meaning if it's a problem with our explanation, then they can help you -- if you've been skipping the lectures so far and have no idea what any of this means, you are on your own).

Advice

  • To maximize your points, make sure your program compiles 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.
     
  • There are four methods to complete in this lab quiz.  Do one at a time, make sure it works, then move on to the next one.  Some methods are worth more than others (the more difficult ones are worth more).  It will be better to have some of the methods working correctly than all the methods working partially.

Files

There are two files that you need to download for this lab quiz

  • MiniVector.java: The skeleton code for this lab quiz.  This file needs to be submitted.
  • Quiz3Test.java: This class contains a main() method that you can use to run your LabQuiz3 class.  It is not modified in this quiz, and thus you do not submit it.

MiniVector class

This class is similar to the Vector class discussed in lecture and in HW J2, with a few modifications listed below.  Remember that a Vector is a class that allows you to add and remove things to/from it, without having to worry about dealing with the size of the Vector -- it automatically expands as necessary.  Unlike the Vector class discussed in lecture, however, the MiniVector only deals only with int values.

Instance variables

Similar to the Vector class, the MiniVector class is to have two instance variables.  These variables are provided to you in the skeleton code.

  • int array[]: Holds the values in this MiniVector.  We will initially set this array to be of size 100.  This array can expand as needed -- this is done via the increaseCapacity() method, described below.
     
  • int size: Holds the number of elements that have been added to the MiniVector.

Methods provided

There are a few methods that are providedf in the lecture slides on arrays, and are already included in the skeleton code.  These methods have all been gone over in lecture.

  • public MiniVector(): The default (and only!) constructor, it creates the array of size 100.
     
  • public void add (int what): This method adds the passed value to the first empty spot in the array.  If there are not enough spots in the array, increaseCapacity() is called to make room.
     
  • public int remove (int where): This method removes (and returns) the int value at position where.  If there are elements after that position in the array, they must be shifted down so that there are no "holes" in the array.
     
  • public int size(): This method returns the number of elements that have been inserted into the MiniVector.
     
  • public int get(int which): This method returns the int value at position which in the array.  It does NOT remove that element from the array.
     
  • public String toString(): This method returns a String representation of the MiniVector.
     
  • private void increaseCapacity(): This method will double the size of the array, and copy all the values over.  It is called from the add() and addAt() methods

Methods you must provide

There are four methods have an empty body in the code (with the exception of a default return statement so that the code will compile -- you will need to remove this return statement).  You must provide the bodies of these four methods.

  • public int indexOf (int what): This method will return the index of the first occurrence of the element what in the MiniVector.  If the what is not found in the mini vector, -1 should be returned.
     
  • public int lastIndexOf (int what): This method will return the index of the last occurrence of the element what in the MiniVector.  If the what is not found in the mini vector, -1 should be returned.
     
  • public void addAt (int where, int what): This method inserts the int value what into the array at position where.
    • If there are not enough spots in the array, increaseCapacity() must be called to make room.
    • If where is greater than or equal to the number of elements inserted in the array, then the element what is inserted at the next available spot.  In other words, if there are 10 elements in the mini vector (elements indices 0 through 9), and where is 10 or higher, then this method just inserts it at position 10.  So calling addAt (100,200) when there are those 10 elements in the array will cause 200 to be added at index 10, as that is the next available spot.
    • Otherwise, if there is currently a value in the array at index where, then that value, and all values until the end of the array, must be shifted up one spot to make room for the new element what.  This is somewhat similar to the shifting done in the provided remove() method, but in reverse.
       
  • public void clear(): This method removes ALL elements in the mini vector, so that the Vector is completely empty.  How this is done is up to you.  The idea here is that after calling clear(), there are no more elements in the mini vector.

Sample Output

If you run the Quiz3Test.java file, it will test a few methods in your code.  You are welcome (and encouraged!) to add further testing functionality to the main() method in the Quiz3Test class.

Added the multiples of 10 to the mini vector: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
Removed the element at index 3: [0, 10, 20, 40, 50, 60, 70, 80, 90]
Added value 42 at index 4: [0, 10, 20, 40, 42, 50, 60, 70, 80, 90]
Added value 95 at index 30: [0, 10, 20, 40, 42, 50, 60, 70, 80, 90, 95]
First index of 42 in the array: 4
First index of 43 in the array: -1
Added a few more elements: [0, 10, 20, 40, 42, 50, 60, 70, 80, 90, 95, 80, 90, 70]
Last index of 80 in the array: 11
Last index of 84 in the array: -1
clear() called: []

Submission

When you are finished, submit the MiniVector.java file.

 

spacer.gif
spacer.gif footer-middle.gif spacer.gif