The Dog
class has a size 3 array with four string values. However, in the subclass GreatDane
, I have to add one more element to this same array. I must do this without using an arraylist (my Java course has not covered those yet).
Here is the class:
abstract class Dog {
String mSize [] = {"tiny", "small", "average", "large"};
int mFeedCounter;
int dogSize = 0;
String getSize() {
return mSize[dogSize];
}
/*
* setSize
* Sets the size of the Dog
* @param size the new size of the Dog, a String
* @return nothing
*/
void setSize(String size) {
mSize[dogSize] = size;
}
And here is the sub class:
Define the GreatDane class below
*
* Great Danes have an extra size category, "huge".
* After growing to a "large" size, they may grow
* to an additional, "huge" size after 3 meals.
/************************************************/
class GreatDane extends Dog{
String mSize[] = {"tiny","small","average","large","huge"};
@Override
void feed(){
if(++mFeedCounter == 3){
dogSize ++;
getSize();
mFeedCounter = 0;}
}
}
I tried reassigning the array reference with mSize = new String []{"tiny","small","average","large","huge"};
but this just gave me an identifier expected error.
Anyways, I don't know why GreatDane is not moving to mSize[4]
.
For reference, here is another class with a similar method that worked:
class Chihuahua extends Dog{
@Override
void feed(){
if(++mFeedCounter ==5){
dogSize++;
getSize();
mFeedCounter = 0;}
}
}