coding in java need help 1)Assume that a class called Student exists, and that i
ID: 3740238 • Letter: C
Question
coding in java need help
1)Assume that a class called Student exists, and that its constructor takes a String student name and a double grade point average as arguments. Write a Java code fragment that declares and initializes an array of three Student objects. (You can instantiate the objects using the data of your choice.)
2)Assume that the following ArrayList has been declared:
ArrayList<Animal> zooAnimals = new ArrayList<>();
The Animal class is an abstract superclass with a number of concrete subclasses, such as Elephant, Tiger, Bear, etc. An unknown number of objects of those subclasses have been added to the ArrayList. Write a Java code fragment to count the number of Tiger objects in zooAnimals and print the final count to the console.
Explanation / Answer
1)
Student[] students = new Student[3];
Student s1 = new Student("Pravesh", 5.6);
Student s2 = new Student("Mahesh", 4.6);
Student s3 = new Student("Alex", 8.6);
students[0] = s1;
students[1] = s2;
students[2] = s3;
2)
ArrayList<Animal> zooAnimals = new ArrayList<>();
int count = 0;
for(Animal a : zooAnimals){
if(a instanceOf Tiger)
count++;
}
System.out.println(count);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.