Create an Alien class. Include at least three protected data members of your cho
ID: 3580341 • Letter: C
Question
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.
b. Create two classes—Martian and Jupiterian—that descend from Alien. Supply
each with a constructor that sets the Alien data fields with values you choose.
For example, you can decide that a Martian has four eyes but a Jupiterian has
only two. Save the files as Martian.java and Jupiterian.java.
c. Create an application that instantiates one Martian and one Jupiterian. Call the
toString() method with each object and display the results. Save the application
as CreateAliens.java.
Explanation / Answer
Note: Here in th requirement as mentioned The Alien class has atleast 3 data members.So I took noOfEyes,noOfHands,noOfLegs.If you want me to change Just intimate,I will modify.
__________________________
Alien.java
public class Alien {
//Declaring variables
protected int noOfEyes;
protected int noOfHands;
protected int noOfLegs;
//Parameterized constructor
public Alien(int noOfEyes, int noOfHands, int noOfLegs) {
super();
this.noOfEyes = noOfEyes;
this.noOfHands = noOfHands;
this.noOfLegs = noOfLegs;
}
//toString() method displays the contents of an object inside it
@Override
public String toString() {
return "Alien# NoOfEyes=" + noOfEyes + " NoOfHands=" + noOfHands
+ " NoOfLegs=" + noOfLegs;
}
}
_____________________
Martian.java
public class Martian extends Alien {
//Parameterized constructor
public Martian(int noOfEyes, int noOfHands, int noOfLegs) {
super(noOfEyes, noOfHands, noOfLegs);
}
//toString() method displays the contents of an object inside it
@Override
public String toString() {
return " Martian# NoOfEyes=" + noOfEyes + " NoOfHands=" + noOfHands
+ " NoOfLegs=" + noOfLegs;
}
}
______________________
Jupiterian.java
public class Jupiterian extends Alien {
//Parameterized constructor
public Jupiterian(int noOfEyes, int noOfHands, int noOfLegs) {
super(noOfEyes, noOfHands, noOfLegs);
}
//toString() method displays the contents of an object inside it
@Override
public String toString() {
return " Jupiterian# NoOfEyes=" + noOfEyes + " NoOfHands=" + noOfHands
+ " NoOfLegs=" + noOfLegs;
}
}
_____________________
CreateAliens.java
public class CreateAliens {
public static void main(String[] args) {
//Creating an Martian class object by passing the arguments
Martian m=new Martian(4, 2, 2);
/* Calling the toString() method on the Martian
* class object to display the contents of it
*/
System.out.println(m.toString());
//Creating an Jupiterian class object by passing the arguments
Jupiterian j=new Jupiterian(2, 2, 2);
/* Calling the toString() method on the Martian
* class object to display the contents of it
*/
System.out.println(j.toString());
}
}
________________________
Output:
Martian#
NoOfEyes=4
NoOfHands=2
NoOfLegs=2
Jupiterian#
NoOfEyes=2
NoOfHands=2
NoOfLegs=2
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.