• Post Reply Bookmark Topic Watch Topic
  • New Topic

Assigning values to arguments? (Modules and Methods?)  RSS feed

 
Aarden Axford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies if this question is confusing, as I am quite confused myself at the moment. I recently started working with Java class files, and I am still trying to get the hang of understanding how the code acts... here is my code (problem explained down below). Also, before you read the code, please note that I must use three different constructors. I understand that I could just use the same constructor over again, but I have to use three different ones.



That is the class file. Here is the main file...



**EDIT** Sorry, I accidentally submitted...

Anyway, as is, the car2 and car3 methods are returning null for everything. Not sure how to fix this? Do they need to return a string? How exactly do I do this? If you're interested, this is a homework question, but I feel like at this point the more I struggle the more confused I get...
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Car2 and Car3 aren't constructors, they're methods that return void. You can overload the constructor if you have different argument signatures, which you have. So remove the void from Car2 and rename it Car. The same for Car3.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To further emphasize the point...

A constructor has NO return type - not even void. And, a constructor must be named exactly the same as the class. So no "Car2" or "Car3". All constructors for your "Car" class must be named "Car".
 
Aarden Axford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here is the new class file. Thank you for clearing that up! I am determined to understand this, as it is obviously very important...


The only question I have is about the second Car() constructor and the third. In my main program, they return null for nearly every single value. How should these constructors be set up? The program should allow the user to "create" 3 cars, each using a different constructor...
If you don't want to give the answer, hints or useful links are always appreciated!
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Axford,
Your code is pretty ok. Try to use this. keyword. It will resolve the null problem. Additionally, you want to have a look at this.
This will minimize unnecessary line of code. You can call the constructors what you already have for the same class within another constructor.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using the same main() that is posted? To get the overloaded constructors, you have to pass the correct number and type of arguments. For instance:



Maybe post your new main() if you can't get it working.
 
Aarden Axford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all your help everybody - got it working. I was able to use the correct keywords in that main(), thank you all for you help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!