Question

In: Computer Science

Re-do Problem 1, but now implement the following method: i must use this code :- public...

Re-do Problem 1, but now implement the following method:

i must use this code :-

public static BagInterface intersection (BagInterface bagA, BagInterface bagB)

and here how my java program must work

This method must return a new bag which is the intersection of the two bags: bagA and bagB. An element appears in the intersection of two bags the minimum of the number of times it appears in either. For example, {1,1,2} ∩{1,1,2,2,3}= {1,1,2}. Do not forget to state the big O of your method implementation.

useing netbeans java

Solutions

Expert Solution

Stack Overflow

sign up log in

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.

  • Questions
  • Jobs
  • Tags
  • Users
  • Badges
  • Ask

Up vote0Down vote

Data Structures - Creating a Bag in Java

java data-structures blackjack bag

I am working on creating a bag of cards for a blackjack game in my CS course. This particular project requires that I create a bag to hold my 52 cards. Keep in mind that I am trying to assure that there are 4 types of each card, Queens, Kings, Jacks, and Aces all included. I keep getting an error in my main: Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer; at Main.main(Main.java:14)

If anyone can please help me to get this bag running properly, it would be greatly appreciated.

Here is my code:

public class Bag<T> 
{
    T[] cards;
    private final int DEFAULT_CAPACITY = 52;
    private int numberOfEntries;

    public Bag()
    {
        this.cards = (T[]) new Object[DEFAULT_CAPACITY];
        numberOfEntries = DEFAULT_CAPACITY;
    }

    public int getCurrentSize()
    {
        return numberOfEntries;
    }

    public boolean isFull()
    {
        return numberOfEntries == DEFAULT_CAPACITY;
    }

    public boolean isEmpty()
    {
        return numberOfEntries == 0;
    }

    public boolean add(T newItem)
    {
        boolean result = true;
        if(isFull())
        {
            result = false;
        }

        else
        {
            cards[numberOfEntries] = newItem;
            numberOfEntries++;
        }

        return result;
    }

    public boolean remove()
    {
        boolean result = true;
        if(numberOfEntries > 0)
        {
            numberOfEntries--;
        }
        else
            result = false;

        return result;
    }

    public void clear()
    {
        numberOfEntries = 0;
    }

    public int getNumOf(T anItem)
    {
        int count = 0;

        for(int i = 0; i < cards.length; i++)
        {
            if(anItem.equals(cards[i]))
            {
                count++;
            }
        }

        return count;
    }

    public boolean contains(T anItem)
    {
        boolean found = false;

        for (int i = 0; !found && (i < numberOfEntries); i++)
        {
            if(anItem.equals(cards[i]))
            {
                found = true;
            }
        }

        return found;
    }

    public T Grab()
    {
        int random = (int)(Math.random() * DEFAULT_CAPACITY);
        if(!isEmpty())
        {
            cards[random] = null;
            numberOfEntries--;
            return cards[random];
        }

        else
            return null;
    }

    public int getFrequencyOf(T anItem)
    {
        int counter = 0;

        for(int i = 0; i < numberOfEntries; i++)
        {
            if(anItem.equals(cards[i]))
            {
                counter++;
            }
        }

        return counter;
    }

}

public class Main {

    public static void main(String[] args)
    {
        //Accesses the Bag class
        Bag<Integer> bag = new Bag<Integer>();

        //Sets up 52 cards (13*4). 4 of each type
        for (int i = 1; i <= 13; i++) 
        {

            for (int j = 1; j <= 4; j++) {
                bag.cards[i*j] = i;
                //if the card is an ace and not equal to 1
                if(i == 1)
                    bag.cards[i*j] = 11;
                //handles the king, queen, and jack cards
                else if (i==11||i==12||i==13)
                    bag.cards[i*j] = 10;    
            }

            bag.add(1);
        }
    }
}

Related Solutions

Re-do Problem 2, but now implement the following method: public static void deleteEvenNumbers() This method must...
Re-do Problem 2, but now implement the following method: public static void deleteEvenNumbers() This method must delete all even numbers in the chain pointed to by aList, while preserving the original of the remaining numbers. For example, if aList is this list: 3, 9, 2, 10, 5, 6; the list becomes after calling the method as follows: 3, 9, 5. useing netbeans
1. Implement a method that meets the following requirements: (a) Do not reuse any code for...
1. Implement a method that meets the following requirements: (a) Do not reuse any code for the following: i. Try to write this method with as few lines of code as you can ii. Sorts a group of three integers, x,y and z, into decreasing order (they do not have to be in a sequence). iii. Assume the value in x is less than the value in z. You can also assume there are no duplicates among x, y and...
Using the following code write the following instructions in C++ Implementation: 1. Re-implement your String class...
Using the following code write the following instructions in C++ Implementation: 1. Re-implement your String class to use a dynamically allocated array for storage. It will be a NULL terminating charater array. 2. This dynamic version of the String will only allocate exactly the amount of memory necessary to store the characters. That is, the length will be the same as the capacity. However, the size of the dynamic array needs to have an extra char for the NULL terminator....
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you...
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you cannot give correct MATLAB
THE NOT REPLACE PROBLEM Must use loops. Please do not use sequence of if statements. I...
THE NOT REPLACE PROBLEM Must use loops. Please do not use sequence of if statements. I need it in C++. Given an input string, set result to a string where every appearance of the lowercase word "is" has been replaced with "is not". The word "is" should not be immediately preceded or followed by a letter -- so for example the "is" in "this" does not count.   • for input of "is test" → "is not test" • for input...
1. Implement a method called count with the following signature: public int count(); such that the...
1. Implement a method called count with the following signature: public int count(); such that the method returns the number of nodes currently contained in the list. The method must compute this number by traversing the entire list. 2. Implement a method called sndLast with the following signature: public E sndLast(); such that the method returns the data stored at the second-to-last node of the linked list. If the size of the list is less than 2, then the method...
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would...
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would be the Student.h file //--------------------------------------------- #include <iostream> #include <cassert> using namespace std; // each student have a name, an ID (100000000~999999999), and three grades class Student { private: public: Student(); Student(); setName(); setId(); setGrade (); getName(); getId(); getGrade() ; printAll() ; }; //--------------------------------------------- // This would be the Student.cpp file //--------------------------------------------- //====================== YOUR CODE STARTS HERE ====================== Student::Student() //default constructor { } Student::Student(string aName,...
How could I implement the contain and min method for a tree in this code. import...
How could I implement the contain and min method for a tree in this code. import java.util.List; import edu.princeton.cs.algs4.*; class ProgrammingAssignment1 { //////////////////// EXERCICE 1: TWO THREE TREES static public <Key extends Comparable<Key>> int size (NakedTree<Key> tree) { if (tree == null) return 0; int val = tree.getNKeys (); for (int i = 0; i <= tree.getNKeys (); ++i) val += size (tree.getChild (i)); return val; } //////// EXERCICE 1.1: contains AND min static public <Key extends Comparable<Key>> boolean contains...
Q2. Update the given code 2 to implement the following problem: Create a class triangle Use...
Q2. Update the given code 2 to implement the following problem: Create a class triangle Use dynamic memory allocation to create 10 objects of the class triangle Set default height and base of all created objects (triangle) to 5 and 10 respectively using a constructor Ask user to change only the height of the 5th triangle Print height and base of all (10) triangles to demonstrate that you did the task correctly Deallocate the 5th object Print height and base...
a) i) The following VHDL code contains erroneous syntax. Re write the code in its corrected...
a) i) The following VHDL code contains erroneous syntax. Re write the code in its corrected format onto your answer sheet. You may assume that din is a 16-bit vector and that the ld, lr and cl inputs are 1-bit wide. lp: process(clk) signal reg : std_logic_vector(15 downt begin if cl=’1’ then reg := (others:=’0’); else if clk=’1’ and clkevent then if ld=’1’ reg <= din; end if; if lr=’1’ then reg := reg(14 downto 0) & “0 else reg...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT