Aditya Herlambang

Greenhorn
+ Follow
since Feb 01, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Aditya Herlambang

So I have this function which is already kind of recursive. The function is desired to print the char between the parentheses recursively. The code is:




Say I have a simple string " (a+(x) * (6 + 14) - q7) then if I call decompose it will produce this result

(x)
(6+14)
(a + (x) * (6 + 14) - q7)

I would like to remove the while loop, so it will just be fully recursion.. however the case is when the current char is not a '(' and a ')' then I have to increment the index of the char I am looking at and then what's next?? How do I check what's the char of that incremented index without having to make a recursive call??
16 years ago
Sorry I just figure out that I can do that by using data[i]
17 years ago
Okay let's say I have a public method called:

public Set<E> union(Set<E> other){
}

if say inside that method I have declared an object such as:

Set<E> result = new ArraySet<E>(some int, some int)

how do I access the result array elements??
17 years ago
I think it's right data[n] because I declare another variable called n which will count the number of data in an array, so there will be no skipping the array by using this.
17 years ago
does this also means that I also have to implement the iterator in the set and put the iterator methods in the set class??
17 years ago
I am asked to write a method of union and intersection which the method header is like this:




It says that I have to return a Set<E> method, and get a Set<E> variable from the constructor. How am I suppose to do that?? It says that I have to implement the iterator interface, do I have to use that here? I need some hint's here. What I understand throughout now is that by using the union and intersection method, that means that I have to have two objects which is compared to each other whether they have the same value (intersection) and the value of both of them combined (union).. One more thing, whenever I try to call the hasNext() method which is on the iterator method, it gives me an error the method hasNext() is undefined for Set<E>, how is this suppose to be right? For your info I am writing this class inside the ArraySet class that implements the Set<E> and the Set<E> extends the Iterator
17 years ago
okay here's what I got for the add method:




is this right according to the rules above?? thanks
17 years ago
I am asked to create an add method with the following rules:
When adding new elements, check to ensure there is room in the array data. If there is no unused array index, grow the array by delta elements. When removing existing elements, shrink the array by delta elements whenever there is room to add more than delta elements. In other words, leave no more than delta empty array locations at any time. The array must have from 0 through delta empty indexes at all times. The following assertions must pass.

This add method must work for any data type. And so far the method that I have create is:

The problem with this code is that when I try to add an interger it always gives me a null point exception because all the value in the array are set as null the very first time, how do I get this method to work the way it's suppose to be??>

EDIT by mw: Added Code Tags.
[ February 03, 2007: Message edited by: marc weber ]
17 years ago
Implement class ArraySet<E> class that implements the Set<E> interface below. It must allow any type of element in the collection. The add method must return true if the element was successfully added or false if that element already equals an element in the set. Because Set<E> extends Iterable<E>, your ArraySet<E> class will need to implement Iterator<E> iterator() and an inner class. Each method should run as specified in Big O notatation. Include a unit test that makes assertions on all methods.

/**
* This interface specifies the methods for a Set ADT. Since Set<E> * extendsIterable<E> you must also add the unseen method Iterator<E> * iterator() to the *collection class.
*/

public interface Set<E> extends Iterable<E> {

// Return true if there are 0 elements in the bag.
// O(1)
public boolean isEmpty();
// Add element to this Set only if element does not equal
// another element already in this Set. Return false if
// element is in this Set, in which case, this Set does not change
// O(n)
public boolean add(E element);
// Return the number of elements in this set
// O(1)
public int size();
// Return the array capacity (the array's length)
// O(1)
public int capacity();
// Return true if element is in this set, or false if it is not
// O(n)
public boolean contains(E element);
// Remove element if it is in this set and return true,
// otherwise return false.
// O(n)
public boolean remove(E element);
// Return the union of this set and other
// O(n)
public Set<E> union(Set<E> other);
// Return the intersection of this set and other
// O(n)
public Set<E> intersection(Set<E> other);
}

Your ArrayList<E> class must have a constructor with two integer arguments. The first is the initial size of the 1D array. The second is the grow-by and shrink-by size in the add an remove methods respectively. Here is the beginning of the class.

public class ArraySet<E> implements Set<E> {
private Object[] data;
private int n;
private int delta;

// Construct an empty bag that can store any type of element.
// O(1)
public ArraySet(int initialCapacity, int growShrinkSize) {
data = new Object[initialCapacity];
n = 0;
delta = growShrinkSize;
}

// Add other methods here ...

}



When adding new elements, check to ensure there is room in the array data. If there is no unused array index, grow the array by delta elements. When removing existing elements, shrink the array by delta elements whenever there is room to add more than delta elements. In other words, leave no more than delta empty array locations at any time. The array must have from 0 through delta empty indexes at all times. The following assertions must pass.




This is a code/project that I have to develop on my own, but I don't even know where to start. I am using eclipse in developing this code and let's say I have a folder called project 1 then how many class do I have to create inside that project?? I know one class must exist for the junit test, and what else do I need? I am predicting that I will need 2 other classes one is for the Set that extends the iteration. And one other else is for the ArrayList class that implements the set<E>, am I right?? I need further guidance to help me with this project. Thanks to those of you who are willing to help me out. Appreciate it.
17 years ago
and what is the relationship between ADT and class?
17 years ago
Hey guys I just learned about this what is called ADT in java, but still I don't quite understand what it is. I tried reading the books several times but still I can't get the concept of what it is.
There is also a keyword 'extend', what is that actually?? There is also the terminology of collection, Set<E>, bag, and List ADT. What is all that about?? Can someone explain it to me very briefly?? Many thanks to those who are willing to help me
17 years ago