• Post Reply Bookmark Topic Watch Topic
  • New Topic

Output of this: going wrong  RSS feed

 
Aaron Boyse
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

I have this code here:



Where am I going wrong?
 
Tim Moores
Saloon Keeper
Posts: 4036
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends - what is the code doing, and what do you think it should be doing instead?
 
Aaron Boyse
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should output: Names, at the minute it is leaving it null
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do your Penguin and Elephant classes look like?
That'll be where the issue is.
 
Aaron Boyse
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The Elephant class looks exactly the same, so won't be posting it...
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so the Animal class then, as it's the getName you are having issues with.
 
Aaron Boyse
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is doing something, but it is not right. If that makes sense.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so post here what you see when you run the above code, and explain exactly what's wrong.
And also post the relevant piece(s) of code so we can possibly point you in the right direction.
 
Aaron Boyse
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:OK, so post here what you see when you run the above code, and explain exactly what's wrong.
And also post the relevant piece(s) of code so we can possibly point you in the right direction.




Here is the elephant class



Here is the Animal class
 
Aaron Boyse
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is printing out the message without printing the name of the Elephant or Penguin.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Animal constructor:


You are assigning the instance variable value (null, 0 and false respectively) to the parameter.
That's back to front.
 
Aaron Boyse
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was told to do that...
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaron Boyse wrote:I was told to do that...

Then rectify it as Dave suggested and show it to that person.
 
Aaron Boyse
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, then here I am doing an explicit override:

 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaron Boyse wrote:I was told to do that...
Probably you misunderstood that. I'm doubted you have been told to assign instance variable to parameter variable. Should be other way round - that is almost for sure. Can't think of any situation that would make sense, as after constructor is executed, values would be lost.

One again, look that below code, shouldn't be like that:
That should be other way round:
and since you have an animal object, you can avoid writing animalAge, I think it is sufficient to write age. And that would read then:
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaron Boyse wrote:
Are you intended to return 0 here?

Aaron Boyse wrote:
Start class names with an upper case. Lower case could help you loose some marks on coding style part. Even tho if that is not a case - keep it upper case.

p, p2, e, e2 - give your penguins proper names, so you could distinct them somehow when our code gets 100 of lines.
kingPenguin, gentooPenguin, littleBluePenguin, magellanicPenguin. You can find more subfamilies in Wikipedia article about penguins.
 
Aaron Boyse
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I take out the variables in the public Animal() expression?

 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaron Boyse wrote:Do I take out the variables in the public Animal() expression?

Only changes the body of constructor, not parameters list.
 
Aaron Boyse
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just remove the top variables and leave the bottom ones?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know, you need to read your requirements.

Let's assume I have provisional requirements listed below:
  • Class monitor should accept parameters vendor name, size in inches.
  • Also, factory sometimes do monitors which are unbranded until some company buys the technology, once they do that, factory puts buyers logo on the monitor as well as measure monitor's size and specify it on the back panel.

  • So my class would look similar to:

    And sorry for a poor example I was able to come up with.
     
    Aaron Boyse
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Did all of that, still null on the name...
     
    Tapas Chand
    Ranch Hand
    Posts: 614
    9
    BSD Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aaron Boyse wrote:Did all of that, still null on the name...

    Can you post your latest "Animal" class?
     
    Aaron Boyse
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tapas Chand wrote:
    Aaron Boyse wrote:Did all of that, still null on the name...

    Can you post your latest "Animal" class?


    Sure thing

     
    Liutauras Vilda
    Sheriff
    Posts: 4928
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aaron Boyse wrote:
    You need to pay more attention. It looks like you're guessing. Check what you assigning to what.
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aaron Boyse wrote:. . .
    Please indent the code correctly. Look at lines 10 and 14. You can cause yourself no end of confusion by that sort of poor indenting; you will not know whether you have your { and } matched up.
    That constructor is incorrect; it will not initialise your object in a consistent state. I noticed that you are not using the hungry parameter; if you are going to set the field to a default value (line 13) you should remove the parameter from the constructor signature. Don't use names like isAnimalHungry; simply say hungry. That should match get and set methods called isHungry and setHungry. Note the different naming convention for booleans.
    The other two assignments in the constructor (lines 11‑12) are also incorrect. I shall leave you to work out the error for yourself.
     
    Aaron Boyse
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:
    Aaron Boyse wrote:
    You need to pay more attention. It looks like you're guessing. Check what you assigning to what.


    I'm instantiating those to name, age and false, but I am overriding them in the main.
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aaron Boyse wrote:. . . I'm instantiating those to name, age and false, but I am overriding them in the main.
    No, you aren't. Please find out how to write a constructor; Liutauras has already told you.
     
    Nigel Browne
    Ranch Hand
    Posts: 704
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aaron,

    The variables in your constructors argument list are not being assigned to the instance created when the Animal object is instantiated. i.e. animalName, animalAge and isAnimalHungry are never used regardless what is passed in as a value to them.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!