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());

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. Computer Science-Java

    Hi, I have to write a method that returns my first name, middle initial, and last name as a single String and call the method in java. So far, I have: public class ReturnName { static String fullname(String firstname, String middleinitial, …
  2. Programming

    The code below shows some of the text for a class called City. Now write a complete public constructor with two formal parameters: a String parameter called cityName, and an int parameter called cityPop. Your code should initialize …
  3. Java Panel Problem

    I am supose to draw a grid and center it with a 10 pix border all the way around it. I have done this but no matter what I do I cannot get a 10 pix border. Here is my code. You will notice i have added 20 to the setsize param to account …
  4. java programming

    Provide a class called Letter for authoring a simple letter. The letter class has three data fields, the name of the sender, the name of the recipient, a body of the letter. 1. Supply the class constructors (default , and overloaded …
  5. shabou

    Can someone please make a Binarytree of these methodes in Java?
  6. computer science

    Im trying to create an arraylist that's sorted but I have no idea how to sort the numbers. I cant use the array.sort method here is my code: public class Testing { private int [] data; private int count; private static int defCapacity …
  7. programming

    m trying to create an arraylist that's sorted but I have no idea how to sort the numbers. I cant use the array.sort method here is my code: public class Testing { private int [] data; private int count; private static int defCapacity …
  8. Java programming

    List all overloading methods (including constructors) in this coding.. public class Circle { private double radius; private String colour; public Circle() { radius = 0; colour = ""; } public Circle (double r) { radius = r; } double …
  9. Computer Programming

    I already posted this question but I gave it a try I'm not sure if I'm on the right track: Make a util class with three methods. First method's name is add, that returns the result by adding two numbers. Second method's name is multiply, …
  10. Java programming

    Hi, I'm trying to figure out how to implement toString() methods into the JavaTest and H2ClassB classes so that the output would be 4 3 7 5 99 3: import java.util.ArrayList; public class JavaTest { ArrayList <JavaTest> list = …

More Similar Questions