nus-cs2030 / 2122-s2

CS2030 repository and wiki for AY 2021/2022 Sem 2
MIT License
0 stars 0 forks source link

Use of restricted API detected #83

Open spicyseaweed opened 2 years ago

spicyseaweed commented 2 years ago

Summary

Hi everyone, CodeCrunch failed my submission cuz I imported java.util.Collection But I thought we need the collection class for values() method?? Can anyone help me with this issue?

Description:

Clarifications or details with respect to the issue you are facing

Code Snippets

Attach code snippets (if any)

Screenshots

image

Yang990121 commented 2 years ago

I think you level 5, you loop through the collection instead of looping through the dictionary?

Yang990121 commented 2 years ago

or you can try to submit code crunch level by level to see which level is causing the issue

huangrz0702 commented 2 years ago

U can send ur immutablelist class first and see whether there is error

spicyseaweed commented 2 years ago

Thanks alot for the reply!

huangrz0702 commented 2 years ago

if not it might be ur level 4/5 problem?

spicyseaweed commented 2 years ago

Thank you Yang990121! Thank you huangrz0702!

spicyseaweed commented 2 years ago

Thanks I passed !!!!!!

shotnothing commented 2 years ago

I had the exact same issue but fixed it after replacing this method:

@Override 
  public String toString() {
      String out = key + ": {";
      boolean flag = false;

      for (Map.Entry<String, Assessment> e : map) {
          flag = true;
          out += e.getValue() + ", ";
      }

      return (flag ? out.substring(0, out.length() - 2) : out) + "}"; 
  }

with one that does not depend on map or list.

I suspect its because (assuming you implemented your ImmutableMap::iterator with return map.entrySet().iterator()) map.entrySet() returns a collection so in the enhanced for-loop, Collection.iterator() is indirectly called.

tygq13 commented 2 years ago

Hi, I also have this error when I use java.util.Set. So we are not supposed to use either “Set" or "Collection" or "ArrayList" to solve this lab? If so what's supposed to be the return type for keySet(), values() and entrySet()?

shotnothing commented 2 years ago

Hi, I also have this error when I use java.util.Set. So we are not supposed to use either “Set" or "Collection" or "ArrayList" to solve this lab? If so what's supposed to be the return type for keySet(), values() and entrySet()?

List, List.copyOf(whatever) works. Also are you sure ArrayList is not allowed? If I'm not wrong, Set and Collection have never been allowed in any of the labs, while ArrayList is okay.

spicyseaweed commented 2 years ago

hi, i think we cannot use ArrayList! but we need set and collection! I realized the problem was not because of my import java. util.collection, but the problem is I shouldn't use it in my level5(module class toString())

spicyseaweed commented 2 years ago

hi i think we cant use arraylist anymore, there's a ImList class for us to use it in place of arraylist.

eesung00 commented 2 years ago

Hi, I also have this error when I use java.util.Set. So we are not supposed to use either “Set" or "Collection" or "ArrayList" to solve this lab? If so what's supposed to be the return type for keySet(), values() and entrySet()?

"Set", "Collection" are allowed to use in the ImmutableMap class but not other classes in level5. For me I defined additional method in ImmutableMap that will be used in my level5 but I think there might be a better way to solve level5.

lukasleeyo commented 2 years ago

Hi, just to add that I also encountered this issue. I tried using the code below: String result = String.format("%s: %s", this.title, this.assessments.values()); return result.replace('[', '{').replace(']', '}');

But I think it will trigger this issue because when I use .values() method, it is indirectly using the java.util.Collection.

So like @shotnothing said, you need to loop through the iterator of the ImmutableMap and get each Assessment value individually.

szeying02 commented 2 years ago

In general, I believe that we aren't allowed to call any methods from the Set and Collection object / class (even toString()), but we are allowed to return it.

YeoShenKai commented 2 years ago

In general, I believe that we aren't allowed to call any methods from the Set and Collection object / class (even toString()), but we are allowed to return it.

Yes, I believe this is the case. The isPresent() and get() methods for Optional cannot be used. This means that you cannot create an Optional class until the return statement.

shotnothing commented 2 years ago

Hi, just to add that I also encountered this issue. I tried using the code below: String result = String.format("%s: %s", this.title, this.assessments.values()); return result.replace('[', '{').replace(']', '}');

But I think it will trigger this issue because when I use .values() method, it is indirectly using the java.util.Collection.

So like @shotnothing said, you need to loop through the iterator of the ImmutableMap and get each Assessment value individually.

Ohh thats weird, because my final Module.java had String out = map.values().toString(); as part of toString(), and it got thru codecrunch.