Lengkap

In this lab you will build (or finish building) a class StrList, which is supposed to work a lot like ArrayList<String>. After you get this version working, you are to implement the generic version TList<T>, in which the type is supplied as a parameter.

1)Make add() work even when elements[] is full

The trick here is to allocate a new array, say newelements, of larger size (doubling works, or increasing by 10, or increasing by 1.5 although that's trickier because of the floating-point conversions):

string [] newelements = new string[newsize];

Next you copy from elements[i] to newelements[i], for i<currsize. This is done with a straightforward for or while loop. Finally, you replace elements by newelements:

elements = newelements

does it. Note that in Java there is no need to do anything with the "old" elements. In C++ you should call delete[] on it; see the destructor ~StrList for an example.

At this point, you now have space to add the new item, so you go ahead and do it. The overall add structure looks like:

if (currsize == [url removed, login to view]) {

// do the space expansion as above to ensure room

}

// now add the usual way; one way or another we know there is space!

elements[currsize] = y;

currsize += 1;

Once you do this, the program should behave quite differently; you shouldn't get those "no more room to add ...." messages, and the entire list should print out.

You can ignore the equals() and hashCode() methods. For now.

2. Add a fill() method

Create a method void fill(string val) that is like Add(val) until the current capacity is reached. That is, element[i] is set to val provided i>currsize and i<[url removed, login to view] (or i<capacity in the C++ version). I suppose calling it Fill() rather than fill() would be more consistent.

With this, the following combination would create a list of 10 empty strings:

StrList sl = new StrList(10); // currsize = 0 but potential capacity = 10

[url removed, login to view]("");

3. The generic version: this should be pretty straightforward. Here I encourage you to write an expand() method; you might want to do this for the StrList version as well. Note that get(n) returns null if n is out of range; this works because null is compatible with any object type.

Note that in the generic version you cannot create an array T[] elements. Arrays in Java have to have a "specific" type. It is not clear if there is a good reason for this. What we do instead is create an array Object[] elements. Assigning a value of type T to a component of this array (as in set()) is legal; Object variables can be assigned values of type T. But get() is a little trickier; we take the value out of the array and have to cast it to type T.

Kemahiran: Java

Lihat lagi: what does the statement: system.out.println(false) output, advantages of generics in java, Hey, I am in need of 400 t-shirt designs in the time period of 2-3 months. Your task will be pretty basic, I will send you ideas, A stduy on work safety for employers in lab , write an article for publication in dailygraphic and the harmed that ignorance is causing the slow growth in ghana and suggest t, we are continuing to expand our list of freelance premiere pro & after effects editors who can work in central london with our c, laberint made in lab view, in need of a t shirt designer, in freelance what type of it projects expected, cost for website design in india amazone type, tndge recruitment 2015 www tndge in lab assistant posts, demo assignment for data entry in info data type, pcb layout in circuitmaker free version of altium for makers, pcb layout in circuitmaker (free version of altium for makers), i have a list of 67 eminent medical people involved in sports and i would like their work email addresses they are all speakers , code class implement working android app

Tentang Majikan:
( 0 ulasan ) United States

ID Projek: #16496597

Dianugerahkan kepada:

$15 USD dalam sehari
(0 Ulasan)
0.0

3 pekerja bebas membida secara purata $23 untuk pekerjaan ini

alioke29

Good Project....Ready to join My experiences range from building responsive websites that work well cross-browser to building scalable sites for the multi company services industry. I like working on projects with Lagi

$25 USD dalam sehari
(0 Ulasan)
0.0
khoddam

Hello, I have got A in data structure course and I am ready to do your lab or homework. Mehrzad

$30 USD dalam sehari
(0 Ulasan)
0.0