0% found this document useful (0 votes)
344 views8 pages

Homework 5 - Bag Interface Implementation (ADT)

The document describes the implementation of a Bag data structure using arrays in Java. It includes the interface and class definitions for the Bag, with method implementations like add(), remove(), contains(), and clear(). The main method tests the Bag functionality by creating a Bag of size 6, adding fruit strings, checking if it is full/empty, removing an element, and verifying the removal.

Uploaded by

Adnan Skulic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
344 views8 pages

Homework 5 - Bag Interface Implementation (ADT)

The document describes the implementation of a Bag data structure using arrays in Java. It includes the interface and class definitions for the Bag, with method implementations like add(), remove(), contains(), and clear(). The main method tests the Bag functionality by creating a Bag of size 6, adding fruit strings, checking if it is full/empty, removing an element, and verifying the removal.

Uploaded by

Adnan Skulic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

|CS204| - Advanced Programming May, the 5 th, 2016

Homework 5 (3.33%)
Prof. Karadžuzović-Hadžiabdić Kanita
Student: Sladić Nedim
ID: 150302003

Implementation of Bag that uses Arrays – Data Structure and Abstract Methods (ADT)

Code:

package baginterface;

interface BagInterface<T> {

int getCurrentSize( );
int getFrequencyOf(T anEntry);
boolean isArrayFull( );
boolean isArrayEmpty( );
boolean add(T newEntry);
boolean remove(T anEntry);
boolean contains(T anEntry);
T remove( );
void clear( );
T[] toArray( );
}

package baginterface;

/**
* Name of the class, implementing BagInterface.
* In the first definition of my class, instance variables are enlisted.
* @param <T> means generic type.
*/

public class Bag <T> implements BagInterface <T>


{
private T[] myArray;
private int size;
private static final int DEFAULT_CAPACITY = 50;

/**
* Class Bag that holds a no-argument constructor.
* In public class Bag, we created a constructor that will initialise
* our instance variables.
*/

public Bag( )
{
this(DEFAULT_CAPACITY);
size = 0;
}

/**
* Initialisation of the constructor.
* @param maximaCapacity - determines a maxima number that an array can
* take (its capacity), initialised by user.
*/

public Bag(int maximaCapacity)


{
myArray = (T[])new Object[maximaCapacity];
size = 0;
}

/**
* Method called getCurrentSize( ) that will return the size an array
* currently has.
* @return an integer size, since the return type is int.
*/

@Override
public int getCurrentSize( )
{
return size;
}

/**
* Method called getFrequencyOf( ) that determines if some element inside of
* an array and how many times an element repeats inside our array.
* @param anEntry
* @return integer of counter by which we determine the frequency of an
* specific element, since the return type is int.
*/

2
@Override
public int getFrequencyOf(T anEntry)
{
int counter = 0;
for (int i = 0; i < myArray.length; i++){
if (anEntry.equals(myArray[i])){
counter++;
}
}
return counter;
}

/**
* Method called isArrayFull( ) that determines if an array is completely
* grouted with the elements from start until end position.
* Thus, if the array is grouted, size and length of an array must be the
* same.
* @return boolean value, either true (if size and length are satisfied) or false.
*/

@Override
public boolean isArrayFull( )
{
return size == myArray.length;
}

/**
* Method called isArrayEmpty( ) that determines if an array is completely
* empty from start until end position.
* If the array is completely empty, size must be 0, otherwise it is not empty.
* @return boolean value, either true (if the size of the array is 0) or false.
*/

@Override
public boolean isArrayEmpty( )
{
return getCurrentSize( ) == 0;
}

/**
* Method called add( ) that will add the new elements inside our array via
* parameter called newEntry. Parameter newEntry is enlisted in the parameter

3
* list of the name of our method.
* @param newEntry - a parameter that we will use to enlarge the array
* by adding more elements to our array.
* @return boolean value, either true or false.
*/

@Override
public boolean add(T newEntry)
{
boolean upstroke = true;
if (newEntry == null){
throw new IllegalArgumentException("Your entry must not be null. " );
}

else if (isArrayFull( )){


upstroke = false;
}

else {
myArray[size] = newEntry;
size++;
upstroke = true;
}
return upstroke;
}

/**
* Method called remove( ) that will remove the element from the array via
* parameter called anEntry. Parameter newEntry is enlisted in parameter list
* of the name of our method.
* Due to remove process, it will decrement and reduce the size of our array
* by 1, that position will remain blank.
* @param anEntry - parameter that will decrement and reduce the size of array.
* @return boolean value, either true or false.
*/

@Override
public boolean remove(T anEntry)
{
for (int i = 0; i < myArray.length; i++){
if (myArray[i] == null && myArray[i].equals(anEntry)){
myArray[size - 1] = myArray[i];

4
myArray[i] = myArray[size - 1];
myArray[size - 1] = null;
size--;
return true;
}
}
return false;
}

/**
* Method called contains( ) that will check via parameter newEntry
* if our array contains an element. Parameter newEntry is enlisted in
* parameter list of the name of our method.
* @param anEntry - parameter that will check the existence of the element.
* @return boolean value, either true or false.
*/

@Override
public boolean contains(T anEntry)
{
for (int i = 0; i < myArray.length; i++){
if (myArray[i] != null && myArray[i].equals(anEntry)){
return true;
}
}
return false;
}

/**
* Method called remove( ) that removes the element and reduces the size
* of an array.
* @return generic return type, in this case, a variable called upstroke.
*/

@Override
public T remove( ){
T upstroke = null;
if (size > 0){
upstroke = myArray[size - 1];
myArray[size - 1] = null;
size--;

5
}
return upstroke;
}

/**
* Method called clear( ) that will erase the elements from our array.
*/

@Override
public void clear( )
{
for (int i = 0; i < myArray.length; i++){
myArray[i] = null;
}
}

/**
* Method called toArray( ) which returns the sequence in a proper way from
* the first to last element.
* @return generic return type.
*/

@Override
public T[] toArray( )
{
T[] upstroke = (T[])new Object[size];
for (int i = 0; i < myArray.length; i++){
upstroke[i] = myArray[i];
}
return upstroke;
}
}

Main method:

package baginterface;
import java.util.Scanner;
public class BagTester
{
public static void main (String[] args)
{
Scanner kb = new Scanner(System.in);

6
// Create a Bag named B1.

System.out.print("Please enter the size for your bag: ");


int mySize = kb.nextInt( );
Bag B1 = new Bag(mySize);
kb.nextLine( );

// Read the item in strings. Then it adds them to B1 and print out B1.

String myFruit;
for (int i = 0; i < mySize; i++) {
System.out.print("Fruit number " + i + ": ");
myFruit = kb.nextLine( );
B1.add(myFruit);
}
System.out.println( );

// Iterate over the objects in B1 and then printing them //

Object[] myFruits = B1.toArray( );


for (int i = 0; i < myFruits.length; i++){
System.out.print(myFruits[i] + "|");
}
System.out.println( );

// Checks if the bag is empty or not. //

if (B1.isArrayEmpty( ))
{
System.out.println("Our bag is empty.");
System.out.println( );
}
else
{
System.out.println("Our bag is not empty.");
System.out.println( );
}

// Checks if the bag is full or not. //

if (B1.isArrayFull( ))

7
{
System.out.println("Our bag is full.");
System.out.println( );
}
else
{
System.out.println("Our bag is not full.");
System.out.println( );
}

// Get an item to remove from bag, remove it, and reprint the bag.

System.out.print("Which fruit you want to remove ? : ");


myFruit = kb.nextLine();
if (B1.contains(myFruit))
B1.remove(myFruit);
System.out.println("Removed element ? " + B1.contains(myFruit));
System.out.println( );
}
}

Output:
Please enter the size for your bag: 6
Fruit number 0: Raspberries
Fruit number 1: Strawberries
Fruit number 2: Blackcurrants
Fruit number 3: Siberian Aronia
Fruit number 4: Blueberries
Fruit number 5: Blackberries

Raspberries | Strawberries | Blackcurrants | Siberian Aronia | Blueberries | Blackberries |

Our bag is not empty.

Our bag is full.

Which fruit you want to remove ? : Strawberries


Removed element ? true
BUILD SUCCESSFUL (total time: 44 seconds)

You might also like