• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Head First Java Page 62 quick question about arrays lead me to a mess.

 
Ranch Hand
Posts: 971
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm looking at page 62 and had a quick question about how a method knows the value of a property of an object.   The example in the book did not show the class for Dog.  The book is talking about putting objects into an array and does not want to get into providing the Dog code.  They want the reader at this point to know the concept.  I became interested to complete it.

I didn't  enter all of the lines of code from the book.  I wanted to only show enough working code to ask my question.   It seemed simple so I created the code and it has many errors.   I tinkered around trying to make the errors go away and it either got worse or stayed the same.  

I'd like to get the code working.  Maybe the answer will show up as I get this to work.  If not, I'll ask the question later.

It did not complain when I created the array of myDogs.

I want to give each Dog in the array a name.  The errors start here.   Maybe I need getters and setters in the Dog class.

Later on it does not like the while loop.  

Please don't give me the full code or I won't learn from it.  Please give me a bit of advice at a time and let me see how far I get.

Thanks,

Kevin

 
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
       myDogs[x].bark;
myDogs[x] is a reference to a Dog object
bark is a method of a HF_62 object

       x = X + 1;
typo
 
Master Rancher
Posts: 5109
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the array is created on line 5, it does not contain any Dog objects.  There needs to be a new Dog object assigned to each slot in the array:
Continue for all slots in the array. You could use a loop.

Also the standard way to add 1 to a variable is the ++ operator:  x++
 
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You don't have any Java errors in that code, but as a designer I would want to assign a value to the Dog's name. Having all of your Dogs named null isn't very dog-friendly. Java would do that with a constructor: when you construct the Dog object, that would be a good time to give the Dog a name.
 
kevin Abel
Ranch Hand
Posts: 971
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 

Cary wrote:    myDogs[x].bark;
myDogs[x] is a reference to a Dog object
bark is a method of a HF_62 object

      x = X + 1;
typo



1)  I didn't notice the difference in case in the typo line.  I fixed it.

2) Would it be OK to put the bark method in the Dog Class?

3) Is my Dog Class OK?   It doesn't have much in it.  

Thanks,

Kevin
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:  

Cary wrote:    myDogs[x].bark;
myDogs[x] is a reference to a Dog object
bark is a method of a HF_62 object

      x = X + 1;
typo



1)  I didn't notice the difference in case in the typo line.  I fixed it.

2) Would it be OK to put the bark method in the Dog Class?

3) Is my Dog Class OK?   It doesn't have much in it.


2) An HF_62 object doesn't bark, but a Dog does.

3) Dog is "ok" but as has been suggested, make a constructor that takes a name as a parameter.

4) Another suggestion would be to make a toString() method that returns the name.
 
kevin Abel
Ranch Hand
Posts: 971
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Norm,

I added the lines to put Dogs into the array.  It is complaining.   I'll post the updated code once after I make the updated from everyone's input.

I used the ++ incrementer.

Thanks,

Kevin
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
   myDogs[x].bark;
myDogs[x] is a reference to a Dog object
bark is a method of a HF_62 object
The two are incompatible.

However, if bark() was added to Dog then things would be different, wouldn't they?
 
kevin Abel
Ranch Hand
Posts: 971
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

I gave a shot at using a constructor.

It will name all of the dogs Ralphy which maybe is better than null.

I added the setter but I forgot how to code a getter and I'm looking for an example.

Thanks,

Kevin



 
Paul Clapham
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
kevin Abel
Ranch Hand
Posts: 971
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the updated code.   It's still has errors.

 
Paul Clapham
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:It will name all of the dogs Ralphy which maybe is better than null.



In real life you would do something like getting a dog's name from the console or somewhere, but that's a good start.
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are missing an open brace.

...and it's closing partner.
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm afraid your "++" is wrong. You just wrote yourself an infinite loop.
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think if you were a little more OCD about your indentation you might have caught the missing braces.
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you create a constructor that takes a "name" parameter you can write:
This eliminates the lines with
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does
do?

And while we at it, what does
do?
 
Paul Clapham
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I'm afraid your "++" is wrong. You just wrote yourself an infinite loop.


My personal advice: You'll see a lot of things online about ++ and how x++ is different from ++x and what happens if you assign the result to a variable and so on. The ++ operator (and also --) is just a big pain that way. Save yourself a lot of hassle: if you want to add 1 to x then write "x += 1".
 
kevin Abel
Ranch Hand
Posts: 971
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that I balanced the brackets correctly.  I had trouble seeing them in C.  It's still not easy for me in Java.

I don't know how to fix this error:
myDogs[0] = new Dog("Fred");  //HF_62.this cannot be referenced from a static context

I used the index in the book to find an example of getters and setters.  I'm not sure if I have it correct.

I don't know if I am setting the name of each dog in the constructor correctly.



 
Norm Radder
Master Rancher
Posts: 5109
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

cannot be referenced from a static context


The Dog class as declared belongs to an instance of the HF_62 class.  With the right syntax you can access it to create an instance.
I'll let the experts show you how.
Meanwhile if you declare the Dog class as static, then the main method will be able to see it properly
 
kevin Abel
Ranch Hand
Posts: 971
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Norm,

I added the keyword static in the Dog Class Signature.



There is a red line error under the
 
The hover bubble says:

Dog in Dog() cannot be applied

I removed static.

I have the original error

Experts please advise.

Thanks,

Kevin

 
Norm Radder
Master Rancher
Posts: 5109
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dog in Dog() cannot be applied


Did you add a constructor to the Dog class that takes a String as argument?

Does your IDE high-light or color the source code?  Look at the above code that is formatted. Compare the coloring of lines 20 and 24.  Notice some is blue and some is black.  Blue is for java keywords.  Black is just text.
 
kevin Abel
Ranch Hand
Posts: 971
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Norm,


Norm wrote:Did you add a constructor to the Dog class that takes a String as argument?



I did this.  Is it what you mean?



Thanks,

Kevin
 
Norm Radder
Master Rancher
Posts: 5109
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you miss this:


Does your IDE high-light or color the source code?  Look at the above code that is formatted. Compare the coloring of lines 20 and 24.  Notice some is blue and some is black.  Blue is for java keywords.  Black is just text.



Look at the line of code you just entered.  What color is Public?  
 
kevin Abel
Ranch Hand
Posts: 971
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used a capital p by mistake on Public.

I changed it to lower case.   The red went away on the keyword public.

I still have the other errors.

Thanks,

Kevin
 
Norm Radder
Master Rancher
Posts: 5109
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I still have the other errors.


If you need help with them,  Copy the full text of the error messages and paste it here.
 
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:I think that I balanced the brackets correctly.  I had trouble seeing them in C. . . .

Make your machine help you. Get a text editor that supports bracket matching. Enable bracket matching on your IDE. Choose a colour that you will see easily.
You may find Allman indentation easier to follow.

. . . HF_62.this cannot be referenced from a static context . . .

Where did that error occur? I can't see it. I think that error message misleads many people because it suggests there is something “normal” about things static.
I always like to use an array initialiser for arrays; it is much faster than writing individual assignments, and there is no chance of miscounting the elements:-
The following is my favourite way to write a constructor; you can use the same syntax with this.xxx in setXXX() methods. “Public” won't compile because it contains a capital letter.
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:. . . The Dog class as declared belongs to an instance of the HF_62 class. . . .

I didn't notice it was an inner class; I am sure it should have been a top‑level class and that is an indentation/bracket matching problem.
 
kevin Abel
Ranch Hand
Posts: 971
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I put static in front of the Dog Class.  I'd rather be using the class as an object.  Hopefully someone shows me how to do this.

I get this error at runtime:

         System.out.printlin(name + "says Ruff!");  //Java cannot find symbol
                                                       //symbol:  method printlin(java.lang.String)
                                                       // location variable out of type java.io.PrintStream



I
 
Norm Radder
Master Rancher
Posts: 5109
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

/Java cannot find symbol


The compiler can not find something that you entered.  It is probably a misspelling.  Check the spelling on that statement
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I mentioned the missing braces all you needed to do was put them around the main() method body. Now you've also included the Dog class making it an inner class and requiring that it be made static. If you want to undo that move the closing brace from  line 38 to line 16 and then remove the word "static" from in front of Dog on line 17.

The "getName()" method will need to be made public.
 
kevin Abel
Ranch Hand
Posts: 971
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Norm, Cary,

I have it working now.   Next I will work on using the ForEach syntax to traverse the array.   I have seen it before and like it more than the while loop.    I will look for an example of it.


 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:. . . I'd rather be using the class as an object. . . .

You can't use classes as objects; you can however make object from classes, which is called instantiating the class.
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:I will look for an example of it.


Read as: FOR-EACH Dog in array myDogs.
Also works with Lists and other Collections as well as any Iterable class.
 
kevin Abel
Ranch Hand
Posts: 971
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,

Is it OK to say:??
I want to instantiate the Dog Class to make it an object.   The object will reside inside an element of the myDogs array.

Thanks,

Kevin
 
kevin Abel
Ranch Hand
Posts: 971
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey,

I wrote this before seeing your post.  It worked.



Thanks,

Kevin
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:. . . I want to instantiate the Dog Class to make it an object.   The object will reside inside an element of the myDogs array. . . .

I would prefer, “I want to instantiate the Dog class to make [one word removed] an object.” Class with a capital C might mean this.
And, “There will be a reference to that object as an element of an array referred to by myDogs.”
You can have that object in memory and it can be referred to in several places. Unfortunately you can get into trouble by having the same object referred to in multiple references

Sorry for delay; I wrote this post last night and for some reason it wasn't uploaded (again ‍)
 
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:
It worked.


I've noticed in your original post and in subsequent posts, that you are not paying attention to code formatting. In other words, are not disciplined with that. Having you mentioning you got into mess, formatting issues lead to that as well.

Those two lines of code should be formatted the following way:

Compare them.

When you see a house with broken windows, without the doors, lots of rubbish on the floor, basically an abandoned house, when you step in, you don't want to take your shoes off, because it is already a big mess inside, you don't see the point in doing somehow differently.

Now, go into the house where everything is shiny, nice and clean carpets, immaculate condition. What you do when you step in? You take your shoes off first.

And so you have to keep your code like that, in immaculate condition - always!
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic