#Syntax Error HELP

15 messages · Page 1 of 1 (latest)

verbal plank
#

Almost done my Java project but I'm getting these syntax errors in my code and have no idea how to fix it.

The class tallies up the points for a game of cribbage here are the instructions:

Counter.java

This class will be used to calculate the number of points from a Cribbage hand.
The class must have the following private variables:

- PowerSet<Card> cardps
- Card starter

The class must have the following public methods:

** public Counter(Card[] hand, Card starter)**: constructor Initialize the starter and use the PowerSet constructor to generate the Power Set
of the cards from the hand (note that the starter card is already included in the
hand array so the Power Set will be based on all 5 cards.

public int countPoints()

  • Calculate the number of points for the hand that was sent into the constructor.
    Use the Power Set, cardps, to do the calculations so that all the combinations are checked. Refer to the scoring explanations in the Introduction when
    implementing this method.

- It is recommended that you implement one or more private helper methods to
help keep the code more organized and clean. For example, you may want
private helper methods for each of the scoring categories, i.e. one for checking if
a given Set sums to 15, another one to check if the given Set forms a run of 3 or
more consecutive cards, etc.

sly juncoBOT
#

This post has been reserved for your question.

Hey @verbal plank! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

verbal plank
#

Anywhere where you see cardps in my code is where the syntax error is (except for when it is initialized) and it reads foreach not applicable to type 'PowerSet<Card>' There are 2 supporting classes that have methods please let me know if you need to see them BOTH OF THOSE CLASSES ARE WORKING FINE AND CANNOT BE MODIFIED

rare shale
#

those aren't syntax errors, they're about types

#

can you show the header of PowerSet (where you have class PowerSet..., up until the {)

verbal plank
#

public class PowerSet<T> {

rare shale
#

yeah you can't foreach a class that doesn't implement Iterable<T>

#

does it have a public method that returns Iterator<T>?

verbal plank
#

    // Declare an array of Set objects
    private Set<T>[] set;

    // Define a constructor method that takes in an array of elements as a parameter and initializes 'set'
    public PowerSet(T[] elements) {
        // Calculate the number of sets possible from the given elements
        int numSets = (int) Math.pow(2, elements.length);
        // Initialize the 'set' array with the calculated number of sets
        set = new Set[numSets];

        // Loop through each set in the 'set' array
        for (int i = 0; i < numSets; i++) {
            // Initialize the current set with an empty Set object
            set[i] = new Set<>();
            // Convert the current set number to a binary string
            String binary = Integer.toBinaryString(i);
            // Pad the binary string with zeros to match the length of the elements array
            while (binary.length() < elements.length) {
                binary = "0" + binary;
            }
            // Loop through each digit in the binary string
            for (int j = 0; j < binary.length(); j++) {
                // If the digit is '1', add the corresponding element to the current set
                if (binary.charAt(j) == '1') {
                    set[i].add(elements[j]);
                }
            }
        }
    }

    // Define a method called 'getLength' that returns the number of sets in the 'set' array
    public int getLength() {
        return set.length;
    }

    // Define a method called 'getSet' that takes in an index as a parameter and returns the corresponding set from the 'set' array
    public Set<T> getSet(int i) {
        return set[i];
    }
}
rare shale
#

looks like you'll have to iterate with a normal for loop and use getSet

verbal plank
#

Can you show me how implement that into my code, sorry im in a rush to submit this

rare shale
#
for (T x: a) {
  // use x
}
// equivalent to
for (int i = 0; i < <sizeof a>; i++) {
  T x = <a at index i>;
  // use x
}
```figure out what the methods for doing `sizeof a` and `a at index i` are
#

for example, with a List<T> a, sizeof a would be a.size(), and a at index i would be a.get(i), so you'd have

for (int i = 0; i < a.size(); i++) {
  T x = a.get(i);
  // use x
}
#

(the equivalency only works for indexed structures btw, doesn't work for like Set or Map where you can't get by index; with that, you'd need some more stuff, but let's not get into that)