100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Summary

Summary Ch02_Bag_Implementations_That_Use_Arrays

Rating
-
Sold
-
Pages
38
Uploaded on
26-06-2021
Written in
2019/2020

Ch02_Bag_Implementations_That_Use_Arrays

Institution
Course











Whoops! We can’t load your doc right now. Try again or contact support.

Connected book

Written for

Institution
Course

Document information

Summarized whole book?
Yes
Uploaded on
June 26, 2021
Number of pages
38
Written in
2019/2020
Type
Summary

Subjects

Content preview

Data Structures and Abstractions with Java ™
5th Edition

Chapter 2


Bag
Implementations
That Use Arrays

Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved

,Fixed-Size Array to Implement the ADT Bag




FIGURE 2-1 A classroom that contains desks in fixed positions
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved

, UML for a fixed size ArrayBag
ArrayBag
-bag: T[]
-numberOfEntries: integer
-DEFAULT_CAPACITY: integer
+getCurrentSize(): integer
+isEmpty(): boolean
+add(newEntry: T): boolean
+remove(): T
+remove(anEntry: T): boolean
+clear(): void
+getFrequencyOf(anEntry: T):
integer
+contains(anEntry: T): boolean
+toArray(): T[]
–isArrayFull(): boolean


FIGURE 2-2 UML notation for the class ArrayBag, including the class’s data fields
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved

, The Class ArrayBag (Part 1)
/**A class of bags whose entries are stored in a fixed-size array.
INITIAL, INCOMPLETE DEFINITION; no security checks */
public final class ArrayBag<T> implements BagInterface<T>
{
private final T[] bag;
private int numberOfEntries;
private static final int DEFAULT_CAPACITY = 25;

/** Creates an empty bag whose initial capacity is 25. */
public ArrayBag()
{
this(DEFAULT_CAPACITY);
} // end default constructor

/** Creates an empty bag having a given initial capacity.
@param desiredCapacity The integer capacity desired. */
public ArrayBag(int desiredCapacity)
{
// The cast is safe because the new array contains null entries.
@SuppressWarnings("unchecked")
T[] tempBag = (T[])new Object[desiredCapacity]; // Unchecked cast
bag = tempBag;
numberOfEntries = 0;
} // end constructor

LISTING 2-1 An outline of the class ArrayBag
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
R95,32
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
SomaSan

Get to know the seller

Seller avatar
SomaSan IUG
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
4 year
Number of followers
0
Documents
4
Last sold
-

0,0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions