This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Constructors+ArrayList  RSS feed

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do I keep on getting the error that the compiler can't find the Hand ArrayList?
Hand.java:28: error: cannot find symbol
     return Hand.size();
                ^
 symbol:   method size()
 location: class Hand
Hand.java:57: error: cannot find symbol
     for(int i=0;i<Hand.size();i++)
                       ^
 symbol:   method size()
 location: class Hand
Hand.java:59: error: cannot find symbol
        if(Hand.get(i).getValue()!=value)
               ^
 symbol:   method get(int)
 location: class Hand
Hand.java:71: error: cannot find symbol
      for(int i=0;i<Hand.size();i++)
                        ^
 symbol:   method size()
 location: class Hand
Hand.java:73: error: cannot find symbol
        if(Hand.get(i).getSuit()!=suit)
               ^
 symbol:   method get(int)
 location: class Hand
Hand.java:97: error: cannot find symbol
        for(int i=0;i<Hand.size();i++)
                          ^
 symbol:   method size()
 location: class Hand
Hand.java:99: error: cannot find symbol
            if(Hand.get(i).getValue()==v && Hand.get(i).getSuit()==s)
                   ^
 symbol:   method get(int)
 location: class Hand
Hand.java:99: error: cannot find symbol
            if(Hand.get(i).getValue()==v && Hand.get(i).getSuit()==s)
                                                ^
 symbol:   method get(int)
 location: class Hand
Hand.java:104: error: cannot find symbol
              Hand.add(new Card(v,s));
                  ^
 symbol:   method add(Card)
 location: class Hand
9 errors

 
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have the class "Hand" and the member variable "hand". The "hand" is the ArrayList. Java is case sensitive.
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, for the defualt constructor do I need anything there?
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why did I get this error?
Hand.java:38: error: cannot find symbol
return Card.get(c);
          ^
 symbol:   method get(int)
 location: class Card
1 error


 
Carey Brown
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"ArrayList hand is set to null, so it is empty." This is not true. Null and empty are two entirely different things.

First, why do you think you need a default constructor. Often they are not necessary, and worse, they can leave an object in an unstable state (e.g. null) if not implemented properly.
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My teacher gave us the outline and it included the default constructor.
 
Carey Brown
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Card" is a class. You do not have a "card" object. What you do have is an ArrayList<Card>, so you'll want to get the nth Card in the list.
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you suggest I do that?
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would I do return hand.get(c) or card.(c)?
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it should be hand because it compiled.
 
Carey Brown
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same way you are doing it in this method.
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I print all the information about the Cards in the hand in toString?
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would I need a forloop @Carey Brown?
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are there these errors?


 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry the recent post about the errors are pointing at the wrong things.
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help plzzzzzz
 
Carey Brown
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you read errors messages always look at the first message first as often times one error will cause a cascade of other errors.

The error message says it can't find a symbol 'c'. You do not have a 'c' variable. What was it supposed to be?
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variables that don’t exist are the parameters of the methods. I don’t understand why they don’t exist.
 
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope I am not annoying you by duplicating what others have already told you.

Ana Yo wrote:. . .

Why are you creating a class called Hand with a field called hand? That is a good way to confuse yourself.
What does line 8 mean? I hope it means you have copied the instructions wrongly, because that postcondition doesn't make sense. If the List reference points, the list doesn't exist; it is neither full nor empty. You are also not satisfying the postcondition stated in line 16 (my line numbers). What about the precondition in line 24? You haven't done anything to enforce that. I also don't think that method will satisfy the postcondition either.

I think you are writing the code wrongly. You should write one method at a time and test that method with normal and abnormal inputs. Once you have that method working, put it into your version control repository (maybe installing Git on your PC) and start with the next method.
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I copied and pasted the shell of what our teacher gave us. Please go with it, I'm confused as you are.
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why did I get this error and how should I fix it?
Exception in thread "main" java.lang.NullPointerException
at Hand.numberOfCards(Hand.java:28)
at Hand.main(Hand.java:117)

 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:Why did I get this error . . .

Maybe because you have a constructor which leaves the List reference pointing to null, which is the default value for a field.

By the way: that isn't a default constructor.
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I fix it?
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only three methods I'm not sure about is:

Someone want to help me? I've been doing this code for two days.
   
 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not going to like this, but you could simply delete the entire constructor. Or initialise the List in that constructor. But you will have to correct the other constructor too.
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wish I could but my teacher gave us this outline and we have to follow it.
 
Ana Yo
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You're not going to like this, but you could simply delete the entire constructor. Or initialise the List in that constructor. But you will have to correct the other constructor too.


Can you help me fill it in and I will learn from it? That's how I usually learn especially since this is through messages. If it were one to one talking I would have more time.
 
Saloon Keeper
Posts: 9710
192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing to fill in. The post-condition requires that the parameterless constructor initializes the hand field to null. Since fields of reference types are null by default, there's nothing to do in that constructor. Please be careful of using the correct terminology, your class declares a parameterless constructor, NOT a default constructor. Classes only have a default constructor if you don't declare ANY constructors yourself.

Going by the invariants that your teacher wrote on the members of the class, they want you to treat the null list as an empty list. This is incredibly poor design, but let's just roll with it.

As I already mentioned, the parameterless constructor has nothing to do, because the list is already null when the Hand object is created. You must still declare it though, because otherwise you wouldn't be able to create an empty Hand at all.

The Hand constructor that takes a Card must add the Card to the list. You can only do that if the list has been initialized. You've already done that. Now you just need to add the card to that list.

The numberOfCards() method needs to return the size of the list. However, remember that it's possible to create an empty hand where the list is not empty, but null. You need to write extra logic to test if the list is null, and if so, return the value 0 to indicate that the Hand is empty.
 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:. . . The Hand constructor that takes a Card must add the Card to the list. You can only do that if the list has been initialized. You've already done that. Now you just need to add the card to that list. . . .

And that is inconsistent with the no‑args constructor. Since you can't call the constructor twice from anywhere outwith the constructor, you are stuck with having to write if (hand != null) ... everywhere hand is used. Agree with you: horrible design.

I firmly believe that every class must have a constructor. Although it is possible to have multiple constructors, the more constructors there are, the more opportunity that affords for something to go wrong. And I think that design is a really good example of an opportunity for things to go wrong
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!