Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 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

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

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

$5.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

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
-
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 tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

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

Student with book image

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

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions