how to write code include a try-catch statement to handle exception of this code. In the catch() statement, will provide a proper alert to the user on the error occurred.
import java.io.*;
import javax.swing.JOptionPane;
class TestArray
{
public static void main (String []args)
{
double a;
double w;
double h;
Mammal mamalia[]=new Mammal[2];
Human people[] = new Human[3];
for(int i=0;i<2;i++)
{
a =Double.parseDouble(JOptionPane.showInputDialog(null,"please enter the weight(kg) of mammalia "+(i+1)));
mamalia[i]=new Mammal(a);
}
System.out.println("properties of mamalia 1 =");
mamalia[0].display();
System.out.println("species of mamalia 1 is ="+mamalia[0].setSpecies("Homo sapiens"));
System.out.println("properties of mamalia 2 =");
mamalia[1].display();
System.out.println("species of mamalia 2 is = "+mamalia[1].setSpecies("Felis catus"));
for(int i=0;i<3;i++)
{
h =Double.parseDouble(JOptionPane.showInputDialog(null,"Enter height(m) of people "+(i+1)));
w =Double.parseDouble(JOptionPane.showInputDialog(null,"Enter weight(kg) of people "+(i+1)));
people[i] = new Human(h,w);
}
String b = JOptionPane.showInputDialog(null,"please enter name of people 1 ");
people[1].setName(b);
System.out.println("name of people 1 is "+people[1].setName(b));
for(int i=0;i<3;i++)
{
people[i].display();
System.out.println("BMI of people "+(i+1)+(" is ")+people[i].getBMI());
}
}
}