[RESOLVED] How to use these Constructor parameters?
I am having difficulty trying to get passed this bump in the road in a program I am trying to build, mainly regarding an overloaded Constructor and an objects creation. Here it is:
Whale w = new Whale("Erwin", 37, "Blue Whale", "Ocean-fish");
There is a compiler error stating that I need a String, int, String, String, but I need them to be String, int, String[], String[]. Is this possible? Can anyone offer any guidance to this, it's been a frustrating few hours!
Whale w = new Whale("Erwin", 37, "Blue Whale", "Ocean-fish");
Here is the compiler error:
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: constructor Fish(java.lang.String,int,java.lang.String,java.lang.String)
location: class Fish
at TestAqualife.main(TestAqualife.java:3)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
I am trying to pass a String, int, String array, and String array. The parameter has a String, int, and String Array taken from a parent class and a single String array taken from this class being passed into the constructor. I need these arrays to be used in the overloaded constructor to be able to assign certain values to an Object from the constructor. Sorry if I am being confusing! Thanks!
The error message shows the constructor for the Fish class. Your previous code was the Whale class.
Where did that come from?
A Whale is NOT a fish. A whale is a mammal.
I cant make any sense from what you are showing. You have single items here and arrays there????
Can you show your whole code? Or at least the constructors and the calls to the constructors.
Given the constructor:
Whale(String name, int age, String[] whaleType, String[] fishOrigin){
this call to the constructor is wrong:
..new Whale("Erwin", 37, "Blue Whale", "Ocean-fish");
The 3rd and 4th arguments are NOT arrays.
You can create an array in your call this way:
..new Whale ("asdf",33, new String[] {"item 1", "item 2"}, new String[] {"item 1", "item 2"});
These two calls to the Constructors look wrong:
Fish f = new Fish("Harold", 13, "Carnivore", "Lake-fish");
Whale w = new Whale("Erwin", 37, "Blue Whale", "Ocean-fish");
Neither of them have arrays.
Both of the constructors have arrays:
Fish(String name, int age, String[] eatPreference, String[] fishOrigin){
Whale(String name, int age, String[] whaleType, String[] fishOrigin){
You MUST change your calls to the constructors to use the same data types as are used in their definitions. You MUST use arrays where there are arrays.
Look at post #4. I gave you the syntax for creating arrays when calling a constructor/method.
Bookmarks