Post a New Question

Programming

posted by .

Here is the source code for the Kitty class:
public class Kitty {

String name;
int age;

public Kitty(){
}

public void setName(String n){
name = n;
}

public String getName(){
return name;
}

public void setAge(int a){
age = a;
}

public int getAge(){
return age;
}
}

1) The array myCats is an 8 element array of type Kitty that has already been declared and initialized. Write the expression(s) in the box below that increases the age of the fifth Kitty by one year:

(Remember: the first element of an array is at index position 0).

2)The array myCats is an 8 element array of type Kitty that has already been declared and initialized. Write the expression(s) in the box below that prints to the console the name of the fourth Kitty if its age is strictly greater than the age of the fifth Kitty, and prints the name of the fifth Kitty otherwise:

(Remember: the first element of an array is at index position 0).

  • Programming -

    The fifth Kitty is myCats[4], since the index starts from zero.
    Each member of the array myCats is a Kitty object.

    1)
    To increase the age, you need to get the age of myCats[4], add one to it, and put back the increased age. This is how you can do it:
    myCats[4].setAge(myCats[4].getAge()+1);

    2)
    int age4=myCats[3].getAge();
    int age5=myCats[4].getAge();
    if(age4>age5)
    {
    System.out.print(myCats[3].getName());
    } else
    {
    ... to be completed by you ...
    }

  • Programming -

    System.out.print(myCats[4].getName());

Answer This Question

First Name
School Subject
Your Answer

Related Questions

More Related Questions

Post a New Question