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

2018-10-23

posted Oct 23, 2018, 5:15 AM by Konstantinovich Samuel   [ updated Oct 25, 2018, 7:17 AM ]
Quiz Postponed 1day.

Error! There was an error in the grades that is now corrected. If they emailed or printed physical copies already, those are not accurate. I don't know what system you see your current report card grades on, but please check that for the updated grades. 

Homework: Work on this lab so that you can ask questions tomorrow in class. There are many parts that you can complete without help, but some will be tricky.

You will be writing the SuperArray class.  

Make a git repo MKS21X-SuperArray
Your repo should have your SuperArray.java and a separate driver file. 

This is a 2-day lab TUES + THURS, also work on this at home Tues/Wed night. 

We will have the quiz WED, and can discuss some of these problems after.


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

-Read all of the methods below and what they do. 

On a new sheet of paper in your notes: Draw out your algorithms that are labeled Sanity Check Required.
-This means you must do the following before writing code:
    a) Draw a sample list on paper with several elements
    b) Outline in words the exact steps you think will be happening.     
    c) Draw arrows or make new copies of the list to show what each operation is doing VISUALLY. 
        Make sure to draw different phases of the list as it progresses.
    d) Show a neighbor (or two) what you THINK the operation is doing inside the object. 
    I will be looking at what you wrote as part of this assignment.

-I do not ask you to do this for truly simple methods, just the tricky ones!


Public MethodsDescription
void clear()

Removes all of the elements from this list. You can just set size to 0 for this!
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. Should resize the list before adding if there is no more space.
String get(int index)

Returns the element at the specified position in this list. Prints an error when index is out of range.
String set(int index, String element)

Replaces the element at the specified position in this list with the specified element, returns the old element. Prints an error when index is out of range.
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. Prints an error when index is out of range.
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. Prints an error when index is out of range.
boolean remove(String element)

Removes the first occurrence of the specified element from this list and return true if it is present . Return false if it is nt in the list. Upon removal, shift all the subsequent elements to the left. 

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

The data.length is the capacity, or how many elements can fit.
The size is how many elements have been added. 

SA = new SuperArray();
now you have:
data null,null,null,null,null,null,null,null,null,null]
size: 0

SA.add("Fish")
SA.add("Hi")
now you have:
data [ "Fish" , "Hi" ,null,null,null,null,null,null,null,null]
size: 2

SA.remove(0)
now you have:
data "Hi" ,null,null,null,null,null,null,null,null,null]
size: 1  



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. [SANITY CHECK REQUIRED] 
boolean add(String) 
    Worry about resize later

4.
String toString() - format:  "[hi, ok, pf]"
String toStringDebug() - print the null values too! This can help you test

5.
String get(int )
return the value at the specified index. 
For now, if the index is not valid, print an error - if the index is out of range, that is when: 
    (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, that is when:
    (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.[SANITY CHECK REQUIRED] 
private void resize() - 
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!!! 
Your add methods must call this before you add to a list that is at capacity.


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

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

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

Comments