LinkedBag: Add three Circle objects into the LinkedBag

elvancedonzy :

I need to add three circle object into the Linked don't know where to start have basic knowledge of C++ new to java for about a month also basis in a data structure class in which we were giving this and modified it a little now we were told to add circles. I am thinking of creating an object class and injecting it into the LinkedBag, but how do i even go about it and will it display the circle or what?


Interface File


   An interface that describes the operations of a bag of objects.

public interface BagInterface<T>
{
     Gets the current number of entries in this bag.
         @return  The integer number of entries currently in the bag.
    public int getCurrentSize();

     Sees whether this bag is empty.
         @return  True if the bag is empty, or false if not. 
    public boolean isEmpty();

 Adds a new entry to this bag.
        @param newEntry  The object to be added as a new entry.
        @return  True if the addition is successful, or false if not.
    public boolean add(T newEntry);

     Removes one unspecified entry from this bag, if possible.
       @return  Either the removed entry, if the removal.
                was successful, or null.
    public T remove();

     Removes one occurrence of a given entry from this bag, if possible.
       @param anEntry  The entry to be removed.
       @return  True if the removal was successful, or false if not. 
   public boolean remove(T anEntry);

     Removes all entries from this bag.
    public void clear();

     Counts the number of times a given entry appears in this bag.
         @param anEntry  The entry to be counted.
         @return  The number of times anEntry appears in the bag.
    public int getFrequencyOf(T anEntry);

     Tests whether this bag contains a given entry.
         @param anEntry  The entry to find.
         @return  True if the bag contains anEntry, or false if not. 
    public boolean contains(T anEntry);

     Retrieves all entries that are in this bag.
         @return  A newly allocated array of all the entries in the bag.
                Note: If the bag is empty, the returned array is empty. 
    public T[] toArray();
} // end BagInterface


______________________________________________________________________________________________________
//Class file


    A class of bags whose entries are stored in a chain of linked nodes.
    The bag is never full.
    INCOMPLETE DEFINITION; includes definitions for the methods add,
    toArray, isEmpty, and getCurrentSize.


public final class LinkedBag1<T> implements BagInterface<T>
{
    private Node firstNode;       // Reference to first node
    private int numberOfEntries;

    public LinkedBag1()
    {
        firstNode = null;
      numberOfEntries = 0;
    } // end default constructor

     Adds a new entry to this bag.
        @param newEntry  The object to be added as a new entry.
        @return  True. 
    public boolean add(T newEntry) // OutOfMemoryError possible
    {
      // Add to beginning of chain:
        Node newNode = new Node(newEntry);
        newNode.next = firstNode;  // Make new node reference rest of chain
                                 // (firstNode is null if chain is empty)
      firstNode = newNode;       // New node is at beginning of chain
        numberOfEntries++;

        return true;
    } // end add

     Retrieves all entries that are in this bag.
       @return  A newly allocated array of all the entries in this bag. 
    public T[] toArray()
    {
      // The cast is safe because the new array contains null entries.
      @SuppressWarnings("unchecked")
      T[] result = (T[])new Object[numberOfEntries]; // Unchecked cast

      int index = 0;
      Node currentNode = firstNode;
      while ((index < numberOfEntries) && (currentNode != null))
      {
         result[index] = currentNode.data;
         index++;
         currentNode = currentNode.next;
      } // end while

      return result;
      // Note: The body of this method could consist of one return statement,
      // if you call Arrays.copyOf
    } // end toArray

     Sees whether this bag is empty.
       @return  True if the bag is empty, or false if not.
    public boolean isEmpty()
    {
        return numberOfEntries == 0;
    } // end isEmpty

     Gets the number of entries currently in this bag.
       @return  The integer number of entries currently in the bag. 
    public int getCurrentSize()
    {
        return numberOfEntries;
    } // end getCurrentSize

// STUBS:

     Removes one unspecified entry from this bag, if possible.
       @return  Either the removed entry, if the removal
                was successful, or null. 
    public T remove()
   {
      return null; // STUB
   } // end remove

     Removes one occurrence of a given entry from this bag.
       @param anEntry  The entry to be removed.
       @return  True if the removal was successful, or false otherwise. 
   public boolean remove(T anEntry)
   {
      return false; // STUB
   } // end remove

     Removes all entries from this bag. 
    public void clear()
   {
      // STUB
   } // end clear

     Counts the number of times a given entry appears in this bag.
         @param anEntry  The entry to be counted.
         @return  The number of times anEntry appears in the bag. 
    public int getFrequencyOf(T anEntry)
   {
      return 0; // STUB
   } // end getFrequencyOf

     Tests whether this bag contains a given entry.
         @param anEntry  The entry to locate.
         @return  True if the bag contains anEntry, or false otherwise. 
    public boolean contains(T anEntry)
   {
      return false; // STUB
   } // end contains

    private class Node
    {
      private T    data; // Entry in bag
      private Node next; // Link to next node

        private Node(T dataPortion)
        {
            this(dataPortion, null);    
        } // end constructor

        private Node(T dataPortion, Node nextNode)
        {
            data = dataPortion;
            next = nextNode;    
        } // end constructor
    } // end Node
} // end LinkedBag1



_____________________________________________________________________________________________________

Other Class File

 A test of the methods add, toArray, isEmpty, and getCurrentSize, 
        as defined in the first draft of the class LinkedBag.




public class LinkedBagDemo1
{
    public static void main(String[] args) 
    {
      System.out.println("Creating an empty bag.");
      BagInterface<String> aBag = new LinkedBag1<>();
      testIsEmpty(aBag, true);
        displayBag(aBag);



      String[] contentsOfBag = {"John Vex","Madam Teni", "Uncle Fireboy"};
        testAdd(aBag, contentsOfBag);
        testIsEmpty(aBag, false);
    } // end main

   // Tests the method isEmpty.
   // Precondition: If the bag is empty, the parameter empty should be true;
   // otherwise, it should be false.
    private static void testIsEmpty(BagInterface<String> bag, boolean empty)
   {
      System.out.print("\nTesting isEmpty with ");
      if (empty)
         System.out.println("an empty bag:");
      else
         System.out.println("a bag that is not empty:");

      System.out.print("isEmpty finds the bag ");
      if (empty && bag.isEmpty())
            System.out.println("empty: OK.");
        else if (empty)
            System.out.println("not empty, but it is: ERROR.");
        else if (!empty && bag.isEmpty())
            System.out.println("empty, but it is not empty: ERROR.");
        else
            System.out.println("not empty: OK.");      
    } // end testIsEmpty

   // Tests the method add.
   private static void testAdd(BagInterface<String> aBag, String[] content)
   {
      System.out.print("Adding the following strings to the bag: ");
      for (int index = 0; index < content.length; index++)
      {
         if (aBag.add(content[index]))
            System.out.print(content[index] + " ");
         else
            System.out.print("\nUnable to add " + content[index] +
                             " to the bag.");
      } // end for
      System.out.println();

      displayBag(aBag);
   } // end testAdd

   // Tests the method toArray while displaying the bag.
   private static void displayBag(BagInterface<String> aBag)
   {
      System.out.println("The bag contains the following string(s):");
      Object[] bagArray = aBag.toArray();
      for (int index = 0; index < bagArray.length; index++)
      {
         System.out.print(bagArray[index] + " ");
      } // end for

      System.out.println();
   } // end displayBag

} // end LinkedBagDemo1
Abra :

Here is the Cirkle class that you need to create (as I wrote in my comment to your question). I named it Cirkle so as not to confuse it with other, existing classes named Circle that are part of the JDK library of classes. The class contains the radius of a circle and the X and Y coordinates of its center.

public class Cirkle {
    private int centerX;
    private int centerY;
    private int radius;

    public Cirkle() {
        this(0);
    }

    public Cirkle(int radius) {
        this(radius, radius);
    }

    public Cirkle(int radius, int center) {
        this(radius, center, center);
    }

    public Cirkle(int radius, int centerX, int centerY) {
        this.radius = radius;
        this.centerX = centerX;
        this.centerY = centerY;
    }

    public int getCenterX() {
        return centerX;
    }

    public int getCenterY() {
        return centerY;
    }

    public int getRadius() {
        return radius;
    }

    public boolean equals(Object obj) {
        boolean equal = false;
        if (obj != null) {
            Class<?> objClass = obj.getClass();
            if (objClass.equals(getClass())) {
                Cirkle other = (Cirkle) obj;
                equal = radius == other.radius    &&
                        centerX == other.centerX  &&
                        centerY == other.centerY;
            }
        }
        return equal;
    }

    public int hashCode() {
        return (radius * 100) + (centerX * 10) + centerY;
    }

    public String toString() {
        return String.format("Cirkle: radius = %d, center(%d, %d)", radius, centerX, centerY);
    }
}
  • Method toString() is used to "display" a Cirkle object.
  • Method equals() determines that two Cirkle objects are equal if they both have the same radius and center.
  • Method hashCode() is required when overriding method equals().

Now you just need to replace the "bag" of strings in class LinkedBagDemo1 with a bag of Cirkles, as shown below in my modified version of the class that appears in your question.

public class LinkedBagDemo1 {
    public static void main(String[] args) {
        System.out.println("Creating an empty bag.");
        BagInterface<Cirkle> aBag = new LinkedBag1<>();
        testIsEmpty(aBag, true);
        displayBag(aBag);

        Cirkle[] contentsOfBag = new Cirkle[]{new Cirkle(5),
                                              new Cirkle(5, 20),
                                              new Cirkle(5, 100, 50)};
        testAdd(aBag, contentsOfBag);
        testIsEmpty(aBag, false);
    }

    private static void testIsEmpty(BagInterface<Cirkle> bag, boolean empty) {
        System.out.print("\nTesting isEmpty with ");
        if (empty)
            System.out.println("an empty bag:");
        else
            System.out.println("a bag that is not empty:");
        System.out.print("isEmpty finds the bag ");
        if (empty && bag.isEmpty())
            System.out.println("empty: OK.");
        else if (empty)
            System.out.println("not empty, but it is: ERROR.");
        else if (!empty && bag.isEmpty())
            System.out.println("empty, but it is not empty: ERROR.");
        else
            System.out.println("not empty: OK.");
    }

    private static void testAdd(BagInterface<Cirkle> aBag, Cirkle[] content) {
        System.out.print("Adding the following Cirkles to the bag: ");
        for (int index = 0; index < content.length; index++) {
            if (aBag.add(content[index]))
                System.out.print(content[index] + " ");
            else
                System.out.print("\nUnable to add " + content[index] + " to the bag.");
        }
        System.out.println();
        displayBag(aBag);
    }

    private static void displayBag(BagInterface<Cirkle> aBag) {
        System.out.println("The bag contains the following Cirkle(s):");
        Object[] bagArray = aBag.toArray();
        for (int index = 0; index < bagArray.length; index++) {
            System.out.print(bagArray[index] + " ");
        }
        System.out.println();
    }
}

And here is the output when I run class LinkedBagDemo1

Creating an empty bag.

Testing isEmpty with an empty bag:
isEmpty finds the bag empty: OK.
The bag contains the following string(s):

Adding the following strings to the bag: Cirkle: radius = 5, center(5, 5) Cirkle: radius = 5, center(20, 20) Cirkle: radius = 5, center(100, 50) 
The bag contains the following string(s):
Cirkle: radius = 5, center(100, 50) Cirkle: radius = 5, center(20, 20) Cirkle: radius = 5, center(5, 5) 

Testing isEmpty with a bag that is not empty:
isEmpty finds the bag not empty: OK.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=20703&siteId=1