• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

objects state and behavior

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What shall i do to run this code and get desired output:-
(Head First java)

The Dog class has an instance variable size, that the bark() method uses to decide what kind of bark sound to make.




Expected Output:-
%java DogTestDrive

Woof! Woof!
Yip! Yip!
Ruff! Ruff!


My Output:-
DogTestDrive.java:19: error: cannot find symbol
                       one.size = 70;
                          ^
 symbol:   variable size
 location: variable one of type Dog
DogTestDrive.java:21: error: cannot find symbol
                       two.size = 8;
                          ^
 symbol:   variable size
 location: variable two of type Dog
DogTestDrive.java:23: error: cannot find symbol
                       three.size = 35;
                            ^
 symbol:   variable size
 location: variable three of type Dog
3 errors

Thanks!
 
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a lot of things that aren't really wrong, but just bad practise. Like you should mark your local variables private and access them with a getter and setter or initialize them in the constructor.
You also could give the value of size with the method call.

so like:



or


or



Main:


The last one would eliminate the need for the size variable and get rid of some boilerplatecode in your main.
That being said, for the rest I don't really see anything wrong with your code. I even tried to run it and for me it gives the expected result....
 
Marshal
Posts: 6846
470
Mac OS X VI Editor BSD Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I see, you have a class Dog1, while in the DogTestDrive class you are trying to initialize class Dog. So, such code shouldn't even compile.
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah lol yeah, I thought it was a weird name, so when I took over the code I just called it dog, so never even noticed. So there's your answer...
 
Marshal
Posts: 64618
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:There are a lot of things that aren't really wrong, but just bad practise.

Agree.

Like you should mark your local variables private

What local variables? The size variable is a field. You are right that it should be marked private, but you can only mark fields private, not local variables.

and access them with a getter and setter or initialize them in the constructor.

Agree. I am dubious about setXXX methods: is it normal for a dog suddenly to change size. I prefer the solution with the constructor, which will prevent you having a Dog instance with size 0.

You also could give the value of size with the method call. . . .

I don't like that solution; it means you have two sizes, one a field and the other a parameter; they might be different. It also means tthe bark() method if not using the informatioon from the object.

The last one would eliminate the need for the size variable . . .

When you stop storing and using information in a object, you are moving outwith the realms of object‑oriented programming. The whole idea of the Dog object is that it contains the size and that size influences its behaviour.

Challenge: write the bark() method without the keywords if and else. Hint: Use the ?: operator.
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I misspoke I ment instance variable... And I was always told to eliminate the need for a variable where possible, if there wouldn't be an instance variable size, there would also be no confussion about which one to use...
Who was that chalenge actually for?
 
Sheriff
Posts: 13510
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:... And I was always told to eliminate the need for a variable where possible, if there wouldn't be an instance variable size, there would also be no confussion about which one to use...


Always consider context. There are times when a variable isn't needed, there are times when it helps clarify the code. It just depends on the situation so don't apply a rule just because "that's what you were told to do." Know the reasons why and why not.

Who was that chalenge actually for?


I believe that was an example from a book. Examples tend to focus on a certain concept. This one didn't care so much about the other things you pointed out. Again, it's context-based. Should you do this in a real-world program? No. Is it enough to demonstrate a concept? Probably. Is it good way to do that? Maybe, maybe not. It depends on who you ask. (There it is again: context)
 
Campbell Ritchie
Marshal
Posts: 64618
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:. . . I was always told to eliminate the need for a variable where possible

Good point; we often see fields which ought to be local variables ,or variables which are never used.

if there wouldn't be an instance variable size, . . .

But without the size and name variables, what is the point in there being an object at all? The idea is to show that dog objects store information about size and name. If you remove the size frrom the dog, you are going to have to store it elsewhere, dissociated from the object. So goodbye, object orientation.

Who was that chalenge actually for?

Anybody who wants to take it up.
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So something like this?:

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

Daniel Demesmaecker wrote:So something like this?  . . .



But let's try improving the formatting:-Some of the () are redundant; remember the precedence and associativity of ?:
 
Hang a left on main. Then read this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!