#Need some help in a java code
1 messages ยท Page 1 of 1 (latest)
Paste each class separately so they're easily accessible to helpers. Be clear about what isn't working.
to be honest this looks like homework, and you have nothing in
public void displayDetails() {
}
Detected code, here are some useful tools:
public void displayDetails() {
}
so how would you approach that?
oh no its meant to be like this
public void displayDetails() {
for (Animal animal : animals) {
animal.DisplayDetails();
}
i want to display the informations of the animal that the user has entered
It's not really the responsibility of an animal to display it's own details, at most a nice toString method()
and you can "display" it using a System.out.println
I uploaded your attachments as Gist.
i get an error at line 356 it says : No enclosing instance of type Animal is accessible. Must qualify the allocation with an enclosing instance of type Animal (e.g. x.new A() where x is an instance of Animal).Java(16777237)
i didn know how to fix it
still need to see the entire gist, but be careful on thing that are not suppose to change
your putting data in the constructor of animal, and then your making setters aswell
Some variables might not have the desire to be changed
and can u post the actual code section it is on, as u merged all the code in 1 file, the numbers are most likely not correct
u mean this ?
also to make sure i get the error here
normally all those classes your posting are suppose to be in each unique file
so that line number, is that correct or not ?
yes ive just checked it
it in :
return new Mammal(name, sex, age, diet, living, furColor);
is it wrong to put em in one file cuz it worked earlier
technicly u can dump all u want in 1 file. Shouldnt really do it though
its okey m not facing an issue with it anyway
have u tried creating another animal ?
just incase its a problem with the static
ah right. U got a typo :p
is your Mammal an inner class of Animal ?
extended from animal
count your brackets at line 76 then
your closing the displayDetails ( and yes your naming is wrong cased ) and then not closing the class, meaning that Mammal is an inner class at that point
a child class is an extened one
( trying to find a good info page on inner class example )
your code problem however is that your class animal is not closed at the point where u think it is. If u look down a bit on Mammal, that one your closing correct
when i closed animal it said that i should make mammal in another file
yes your ide is correct :p
but u cannot extend inner class with as base the outer class
and when i make the class zoo in its own file should i make it non static
yep
now that i moved each type of animals to seperate files what should i change in the add animal function
tried running it yet ?
can u share the newer version ?
I uploaded your attachments as Gist.
right , if that is your current code, addAnimal is in which class ?
zoo class
ok. Just to verify, can u yourself hit the Gist button from the post and see where it is ?
okey wait
it is quite hard to see code one way , and the poster saying its in another way ๐
ok, that's still the same. If your addAnimal is inside the zoo class, drop the static for starters
( on the gist that your posting the method is no where inside a class )
wait i think i fixed it
just post the zoo class, that should fix it
lol i got an error in the running
when i run it and i give information of anytype but mammal type i get this
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The declared package "animalss" does not match the expected package "animals"
Syntax error on token "}", delete this token
Syntax error on token "}", delete this token
Syntax error on token "}", delete this token
Syntax error on token "}", delete this token
Syntax error on token "}", delete this token
Syntax error on token "}", delete this token
Syntax error on token "}", delete this token
at animals.Amphibian.<init>(Amphibian.java:1)
at animals.Animal.addAnimal(Animal.java:129)
after gving the infos
what IDE are u using @vague sage ?
actually i fixed the double s and still get it
as long as your code has 1 wrongly placed bracket it will make no sense to the compiler
code might be correct but due to the wrong brackets it might get confused
m using vscode btw
so fix all the typo's and brackets in there. If your using intellij, bottom right should be a tab called Problems and they should be listed there
do yourself 1 favor. Drop VSC
I WILL
VSC aint made for java coding, 1 second it might work and the next your project is doomed
idk where it problem with brackets cuz theres no error
You can code Java just fine in VScode with the right setup, but as a beginner Intellij CE is a bit friendlier
there wont be, as its written correct. So isolate all your classes in seperate files and it should indicate in which file it is