Learning - object array of Java classes and objects

Posted by erock on Mon, 24 Jan 2022 16:18:11 +0100

Level 1: Learning - object array of Java classes and objects

Task description

This related task: define a Dog class and create three Dog objects by using object array.

Relevant knowledge

The so-called object array means that it contains a group of related objects. When using an object array, you must be clear: you must first open up space for the array. Because it is a reference data type, every object in the array is null. When used, each object in the array must be instantiated separately.

There are four ways to define an object array:

  1. Static initialization;

Class name object array name [] = new class name [] {object, object...};

 
  1. //Create objects directly when creating an object array
  2. Person person[] = new Person[]{new Person(),new Person(),new Person()}
  1. Dynamic initialization;

     
      
    1. //Reference an object array first
    2. Person person[];
    3. //Specifies the size of the array object
    4. person = new Person[3];
    5. //Loop instantiates each object in the array
    6. for(int i=0;i
    7. {
    8. person[i] = new Person();
    9. }
  2. The array element points to the defined object;

     
      
    1. //Reference an array object
    2. Person person[];
    3. //Open up a space of the specified size for the array
    4. person = new Person[3];
    5. //Instantiate each object of the array
    6. person[0] = new Person();
    7. person[1] = new Person();
    8. person[2] = new Person();
  3. Create an array of objects and allocate object space.

     
      
    1. //Specify the space size when creating an array of objects
    2. Person person[] = new Person[3];
    3. //Instantiate each object of the array
    4. person[0] = new Person();
    5. person[1] = new Person();
    6. person[2] = new Person();

Programming requirements

Carefully read the code framework and comments given in the editing area on the right, and write the program code according to the prompts.

Test description

The platform will use the test set to run the program code you write. If all the running results are correct, you will pass the customs. You can view the specific test set details in the "test results" area on the right.

Start your mission. I wish you success!

/**
 * Task: create 3 Dog objects by using object array
 * Class name: Dog
 * This class is the basic attribute of Dog
 */
public class Dog {
    private String name;     // Dog name
    private String type;     // Dog breed
    private int age;     // Puppy age
    private String hobby;    //Puppy hobby

    public Dog(){

    }
    public Dog(String name, String type, int age, String hobby) {
        this.name = name;
        this.type = type;
        this.age = age;
        this.hobby = hobby;
    }
	
    // Get Dog name
    public String getName() {
        return name;
    }

    // Set Dog name
    public void setName(String name) {
        this.name = name;
    }
	// Get Dog type
    public String getType() {
        return type;
    }
	// Set Dog type
    public void setType(String type) {
        this.type = type;
    }
	// Get Dog age
    public int getAge() {
        return age;
    }
	// Set Dog age
    public void setAge(int age) {
        this.age = age;
    }
	
    // Acquire hobbies
    public String getHobby() {
        return hobby;
    }
	// Set hobbies
    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
	// Details of Dog
    public void info(){
        System.out.printf("Dog name:%s\n varieties:%s\n Puppy age:%d\n Puppy hobbies:%s\n",name,type,age,hobby);
    }



    public static void main(String[] args) {
        Dog d1 = new Dog("Tom", "Siberian Husky",2,"Demolition");
        Dog d2 = new Dog("jerry", "Chinese garden dog",3,"Protect the family");
        Dog d3 = new Dog("Wangcai","Koki",2,"Eat, drink and play");
        // Please write the correct code between the begin end below according to the tips given in the comments
        /********** Begin **********/
        // Put the objects of the three dogs into the object array and call the info method of the object in turn
        Dog dog[]=new Dog[3];
        dog[0]=new Dog();
        d1.info();
        dog[1]=new Dog();
        d2.info();
        dog[2]=new Dog();
        d3.info();
        
        
        /********** End **********/
    }
}

Topics: Java educoder