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:
- Static initialization;
Class name object array name [] = new class name [] {object, object...};
- //Create objects directly when creating an object array
- Person person[] = new Person[]{new Person(),new Person(),new Person()}
-
Dynamic initialization;
- //Reference an object array first
- Person person[];
- //Specifies the size of the array object
- person = new Person[3];
- //Loop instantiates each object in the array
- for(int i=0;i
- {
- person[i] = new Person();
- }
-
The array element points to the defined object;
- //Reference an array object
- Person person[];
- //Open up a space of the specified size for the array
- person = new Person[3];
- //Instantiate each object of the array
- person[0] = new Person();
- person[1] = new Person();
- person[2] = new Person();
-
Create an array of objects and allocate object space.
- //Specify the space size when creating an array of objects
- Person person[] = new Person[3];
- //Instantiate each object of the array
- person[0] = new Person();
- person[1] = new Person();
- 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 **********/ } }