Create an Alien class. Include at least three protected data members of your
choice, such as the number of eyes the Alien has. Include a constructor that
requires a value for each data field and a toString() method that returns a
String containing a complete description of the Alien. Save the file as
Alien.java.
public class Alien
{
protected int no_of_eyes;
protected int no_of_legs;
protected int no_of_heads;
public Alien(int e,int l, int h){
no_of_eyes=e;
no_of_legs=l;
no_of_heads=h;
}
public String toString(){
System.out.println("The Alien has:\n "+no_of_eyes+" eyes\n"+no_of_legs+" legs\n"+no_of_heads+" heads\n");
return "";
}
public static void main(String[] args) {
Alien a=new Alien(5,3,3);
a.toString();
}
}
Comments