I am a second semester programming student struggling with an assignment and I c
ID: 3528756 • Letter: I
Question
I am a second semester programming student struggling with an assignment and I could really use some insight. I was provided with these two classes. import java.util.Collection; import java.util.AbstractCollection; import java.util.Iterator; public class BasicLinkedCollection extends AbstractCollection { private static final int DEFAULT_SIZE = 100; private E[] collection; private int size; protected transient int mod; *****constructors************************* public BasicLinkedCollection() { collection = ( E[] ) ( new Object[DEFAULT_SIZE] ); size = 0; mod = 0; } end BasicLinkedCollection public BasicLinkedCollection( Collection c ) { if ( c == null ) { throw new java.lang.IllegalArgumentException(); } int size = c.size() > DEFAULT_SIZE ( c.size() * 110 ) 100 : DEFAULT_SIZE; collection = ( E[] )new Object[size]; c.toArray( collection ); mod = 0; } end BasicLinkedCollection *****optional methods************** add public boolean add( E e ) { if ( e == null ) { throw new java.lang.IllegalArgumentException(); } collection[size] = e; size++; mod++; return true; } end add addAll public boolean addAll(Collection c) { boolean modified = false; Iterator e = c.iterator(); while(e.hasNext()) { if(add(e.next())) modified = true; } return modified; } end addAll remove public boolean remove( Object e ) { if ( e == null ) { throw new IllegalArgumentException(); } int p = getElement( e ); if ( p == -1 ) { return false; } for ( int i = p; i c) { if (isEmpty() || c.isEmpty()) { return false; } boolean modified = false; for (Iterator<> i = c.iterator(); i.hasNext(); ) { if (remove(i.next())) { modified = true; if (isEmpty()) { break; } } } return modified; } removeAll *****helper method**************** getElement public int getElement( Object e ) { for ( int i = 0; i iterator() { return new BasicIterator(); } *****BasicIterator Class*********************** *This code was taken and modified from the provided BasicCollection Class in the book* private class BasicIterator implements Iterator { *****private fields************ private int cursor; private int expectedModcount; private boolean okToRemove; *****constructor************ BasicIterator public BasicIterator() { cursor = 0; expectedModcount = mod; okToRemove = false; } end BasicIterator *****public methods********** hasNext public boolean hasNext() { return cursor = size; } end hasNext next public E next() { if ( expectedModcount = mod ) { throw new java.util.ConcurrentModificationException(); } if ( hasNext() ) { throw new java.util.NoSuchElementException(); } okToRemove = true; E e = collection[cursor]; cursor++; return e; } end next remove public void remove() { if ( expectedModcount = mod ) { throw new java.util.ConcurrentModificationException(); } if ( okToRemove ) { throw new IllegalStateException(); } okToRemove = false; --cursor; BasicLinkedCollection.this.remove( collection[cursor] ); expectedModcount++; } } } ...and ** * A simple singly-linked list introduced in the chapter * on Fundamental Data Structures. * public class SinglyLinkedList { private int length; # elements in the linked list private SLNode head; access point to the linked list private SLNode tail; ** * Create an empty SinglyLinkedList. * public SinglyLinkedList() { this.length = 0; this.tail = new SLNode (); the tail dummy node this.head = new SLNode ( null, this.tail ); the head dummy node } ** * Return the length of this singly linked list. * @return the number of elements in this singly linked list * public int getLength() { return this.length; } ** * Add a new element at the beginning of the linked list. * @param e the element to add * public void add( E e ) { SLNode newnode = new SLNode ( e, null ); newnode.setSuccessor( this.head.getSuccessor() ); this.head.setSuccessor( newnode ); this.length++; } ** * Add element e at position p in this * singly linked list. * @param e the element to add * @param p position to insert e; must be in the range * 0 to this.size(). * @throws IndexOutOfBoundsException if p is outside * the range 0 to this.length(). * public void add( E e, int p ) { verify that index p is valid if ( ( p this.length ) ) { throw new IndexOutOfBoundsException( "index " + p + " is out of range: 0 to " + this.length ); } SLNode newnode = new SLNode ( e, null ); SLNode cursor = this.head; for ( int i = 0; i p and return its element * field. * @param p the position whose element we are to return * @return the element in position p * @throws IndexOutOfBoundsException if p is outside * the range 0 to length() - 1 * public E remove( int p ) { if ( ( p = this.length ) ) { throw new IndexOutOfBoundsException( "index " + p + " is out of range: 0 to " + ( this.length - 1 ) ); } SLNode cursor = head; good for p == 0 if ( p > 0 ) { cursor = find( p - 1 ); get target's predecessor } SLNode target = cursor.getSuccessor(); get the node to remove link target to cursor's successor cursor.setSuccessor( target.getSuccessor() ); target.setSuccessor( null ); cursor.setElement( null ); this.length--; return target.getElement(); } ** * Return the element stored in the node at position p. * @param p the position whose element we want * @return the element from position p of this linked list * @throws IndexOutOfBoundsException if the index p is outside the range 0 * to length - 1. * public E getElementAt( int p ) { SLNode node = this.find( p ); return node.getElement(); } PRIVATE UTILITY METHODS * addAfter - add newnode after node p PRECONDITIONS NOT CHECKED pre: p and newnode are not null post: newnode is inserted after p * private void addAfter( SLNode p, SLNode newnode ) { newnode.setSuccessor( p.getSuccessor() ); p.setSuccessor( newnode ); } ** find - find the first node containing target, return null if target * is not found * private SLNode find( E target ) { SLNode cursor = head.getSuccessor(); while ( cursor = tail ) { if ( cursor.getElement().equals( target ) ) { return cursor; success } else { cursor = cursor.getSuccessor(); } } return null; failure } * find - find the node at position p * throw an exception if the index p is outside the range 0 to this.length - 1 * private SLNode find( int p ) { if ( ( p = this.length ) ) { throw new IndexOutOfBoundsException(); } SLNode cursor = head.getSuccessor(); int i = 0; while ( i = p ) { cursor = cursor.getSuccessor(); i++; } return cursor; } } The assignment is to create a class that shows examples of each the BasicLinkedCollection operations (i.e. add(), addAll(), size(), isEmpty(), remove(), etc) while changing the backing store of BasicLinkedCollection from an Array to a SinglyLinkList using the provided class. I got the operations class no problem, but I don't have any idea where to begin in the BasicLinkedCollection class. Any help, prodding, or insight would be greatly appreciated. I have also included the operations class BasicLinkedCollectionTest in case it helps. class BasicLinkedCollectionTest { public static void main(String[] args) { BasicLinkedCollection pet = new BasicLinkedCollection(); BasicLinkedCollection pet2 = new BasicLinkedCollection(); Display empty collections System.out.println("Displaying items in collection Pet and Pet2: n" + pet + pet2); System.out.println(); System.out.println("Get Pet collection size: "); System.out.println(pet.size()); System.out.println(); System.out.println("Get Pet2 collection size: "); System.out.println(pet2.size()); System.out.println(); populate pet collection pet.add("Dog"); pet.add("Cat"); pet.add("Turtle"); pet.add("Fish"); pet.add("Snake"); pet.add("Rat"); pet.add("Rabbit"); pet.add("Crab"); display populated pet collection System.out.println("Pet collection has now been populated using the add() method and contains: n" + pet); System.out.println(); pet2.add("Cat"); pet2.add("Rat"); pet2.add("Spider"); pet2.add("Pig"); System.out.println("Pet2 collection has now been populated using the add() method and contains: n" + pet2); System.out.println(); System.out.println("Check pet.contains( "Cat "): "); System.out.println(pet.contains("Cat")); System.out.println(); System.out.println("Check pet.containsAll(pet2): "); System.out.println(pet.containsAll(pet2)); System.out.println(); System.out.println("Check pet.remove( "Spider "): "); System.out.println(pet.remove("Spider")); System.out.println(); System.out.println("Check pet.removeAll(pet2): "); System.out.println(pet.removeAll(pet2)); System.out.println(); System.out.println("Pet collection now contains: "); System.out.println(pet); System.out.println(); System.out.println("Check pet.addAll(pet2): "); System.out.println(pet.addAll(pet2)); System.out.println(); System.out.println("Pet collection now contains: "); System.out.println(pet); System.out.println(); System.out.println("Check if Pet2 collection isEmpty(): "); System.out.println(pet2.isEmpty()); System.out.println(); } end main method } end classExplanation / Answer
I amport java.util.*;public class h1
{
public static void main (String[] args)
{
ArrayList alters = new ArrayList();
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
alters$PHX$93$PHX$in.nextInt());
}
in.close();
System.out.println(maxSum(alters));
} public static long maxSum(ArrayList A)
{
if (A.isEmpty())
return 0;
else
return maxSum(A 0, A.size() - 1);
} private static long maxSum(ArrayList A, int p, int r)
{
if (p == r)
return A.get(p);
else {
int q = (p + r) 2;
long a = maxSum(A, p, q);
long b = Long.MIN_VALUE;
long c = maxSum(A, q + 1, r);
b = maxCrossing(A, p, r);
System.out.println("Crossing: " + maxCrossing(A, p, r));
return Math.max(a, Math.max(maxCrossing(A, p, r), c));
}
}
public static long maxCrossing(ArrayList A, int p, int r)
{
int q = (p+r) 2;
long left_sum = Long.MIN_VALUE;
long sum = 0;
for (int i = q; i>p; i--)
{
sum = sum + A.get(i);
if (sum > left_sum)
left_sum = sum;
}
sum = 0;
long right_sum = Long.MIN_VALUE;
for (int j=q+1; j right_sum)
right_sum = sum;
}
return left_sum + right_sum;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.