Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎notes‎ > ‎

2017-10-25 HW

posted Oct 24, 2017, 5:50 PM by Konstantinovich Samuel   [ updated Oct 29, 2017, 8:46 AM ]

You will be writing the SuperArray class.  (2 day lab WED + THURS)

BEFORE you decide to go head first into the code, you should:

-Read all of the methods below and what they do. 
-Note the designation [SANTIY CHECK REQUIRED], this means you must do the following:
    Draw the object on paper, and go through each operation BEFORE you code it. 
    Show a neighbor (or 2) what you THINK the operation is doing inside the object. 
    DO NOT JUST WRITE CODE BEFORE YOU DO A SANITY CHECK ON THE ALGORITHMS!
-I do not ask you to do this for truly simple methods, just the tricky ones!


SuperArray contains the fields:
-private String[] data
-private int size


Public Methods Description
void clear() Removes all of the elements from this list.
int size() Returns the number of elements in this list.
boolean isEmpty() Returns true if this list contains no elements.
boolean add(String element) Appends the specified element to the end of this list. Returns true.
String get(int index) Returns the element at the specified position in this list.
String set(int index, String element) Replaces the element at the specified position in this list with the specified element, returns the old element.
String toString() Returns the data in the format: "[A, B, FISH, cat, Dog]" Without double quotes around the data elements.
boolean contains(String element) Returns true if this list contains the specified element.
void add(int index, String element) All elements at the index or to the right, are shifted to the right to make space, then insert the specified element at the specified position in this list. 
int indexOf(String element) Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
int lastIndexOf(String element) Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
String remove(int index) Removes the element at the specified position in this list and shifts all subsequent elements to the left. Return the element removed.
boolean remove(String element) Removes the first occurrence of the specified element from this list if it is present. Shift all the subsequent elements to the left. 

PHASE 1
Start with the basic ones first:

0 constructor - just make an empty list, start the array with a maximum capacity of 10 by default.

1.
void clear() 

2.
int size() 
boolean isEmpty()

3.
boolean add(String)   [SANTIY CHECK REQUIRED]

4.
String toString()

5.
String get(int )
Print an Error - if the index is out of range (index < 0 || index >= size())
return null when an error happens

String set(int, String) - For now, if the index is not valid, print an error, and do not modify the list.  We will see better ways to do this later.
Print an Error - if the index is out of range (index < 0 || index >= size())
return null when an error happens

Before you write more methods, Write a main with a series of tests (use loops and predict the outcomes)
Post your testing ideas on the mailing list!

PHASE 2: 
7.
private void resize() - [SANITY CHECK REQUIRED] 
This is a private method that you call when you need more space. 
It will increase the capacity by making a new Array of Strings and copying the old one over. 

This is a critical method to test thoroughly.


PHASE 3: 
(Assuming you completed and TESTED the previous methods, fill in the more challenging methods)

8.
boolean contains(String target)

9.
int indexOf(String target)
int lastIndexOf(String target)

10. 
void add(int,String) [SANITY CHECK REQUIRED]
Print an Error - if the index is out of range (index < 0 || index > size())

11. 
String remove(int) [SANITY CHECK REQUIRED]
Print an Error - if the index is out of range (index < 0 || index >= size())
return null when an error happens

12. 
boolean remove(String) [SANITY CHECK REQUIRED]

Submit your SuperArray.java AND ONLY that file, on the homework server by Monday 8am.

Your password is your osis!
Comments