• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
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

 
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
       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
 
Rancher
Posts: 5008
38
  • Number of slices to send:
    Optional 'thank-you' note:
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++
 
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:


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 Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
 

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
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:

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 Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
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
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
   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 Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
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
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:
 
kevin Abel
Ranch Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
Here is the updated code.   It's still has errors.

 
Paul Clapham
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:

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
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
You are missing an open brace.

...and it's closing partner.
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
I'm afraid your "++" is wrong. You just wrote yourself an infinite loop.
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
I think if you were a little more OCD about your indentation you might have caught the missing braces.
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
When you create a constructor that takes a "name" parameter you can write:
This eliminates the lines with
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
What does
do?

And while we at it, what does
do?
 
Paul Clapham
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:

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 Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
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
Rancher
Posts: 5008
38
  • Number of slices to send:
    Optional 'thank-you' note:

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 Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
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
Rancher
Posts: 5008
38
  • Number of slices to send:
    Optional 'thank-you' note:

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 Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
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
Rancher
Posts: 5008
38
  • Number of slices to send:
    Optional 'thank-you' note:
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 Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
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
Rancher
Posts: 5008
38
  • Number of slices to send:
    Optional 'thank-you' note:

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: 79394
377
  • Number of slices to send:
    Optional 'thank-you' note:

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: 79394
377
  • Number of slices to send:
    Optional 'thank-you' note:

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 Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:


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
Rancher
Posts: 5008
38
  • Number of slices to send:
    Optional 'thank-you' note:

/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
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
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 Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
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
Saloon Keeper
Posts: 10779
86
 
Campbell Ritchie
Marshal
Posts: 79394
377
  • Number of slices to send:
    Optional 'thank-you' note:

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
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:

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 Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
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 Foreman
Posts: 906
8
  • Number of slices to send:
    Optional 'thank-you' note:
Carey,

I wrote this before seeing your post.  It worked.



Thanks,

Kevin
 
Campbell Ritchie
Marshal
Posts: 79394
377
  • Number of slices to send:
    Optional 'thank-you' note:

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 ‍)
 
Marshal
Posts: 8880
638
  • 2
  • Number of slices to send:
    Optional 'thank-you' note:

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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |