Question: write in java 1. Implement the code for a class called Animal with the following: Attributes: String Nutrition, String Shape, Boolean pet. A method called

write in java

1. Implement the code for a class called Animal with the following:

Attributes: String Nutrition, String Shape, Boolean pet.

A method called "PrintInfo" that prints all the information of the Animal class.

Reuse class Animal to create class called Horse

o Additional attributes: string colour, int age.

o Override the method "PrintInfo" by printing all the information of horse instances.

Write the code to show example of instances of Animal class:

Create an instance for a horse class by calling a constructer has (Grass,Big,false,Black,3)

Print the information of the class through PrintInfo()

2. Implement the code for:

a. A static method called getLastNumber that takes an integer array and return the last the number.

class Main {

public static void main(String[] args) {

int[] array_one = {12, 4, 5, 2, 5};

int last_array1 = getLastNumber(array_one);

int[] array_two = {6, 3, 11, 46, 78};

int[] array_three = {2, 9, 1, 7, 97}; } }

b. A static method called getTotal_lastIndex that takes 3 integer arrays and return the sum of the last the number of each array. getTotal_lastIndex should reuses getLastNumber.

class Main {

public static void main(String[] args) {

int[] array_one = {12, 4, 5, 2, 5};

int[] array_two = {6, 3, 11, 46, 78};

int[] array_three = {2, 9, 1, 7, 97};

int myTotal = getTotal_lastIndex(array_one,array_two,array_three); } }

3. Run the following code in Java compiler:

public class Main {

public static void main(String[ ] args) {

int[] myNumbers = {1, 2, 3};

System.out.println(myNumbers[10]); } }

Then use try catch statement to solve the error caused by calling the array with index not found in the array.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!