Question
this is java problem. --------------------------------------------------------------------------------------- public abstract class Martian implements Cloneable, Comparable { private int id; private int volume; protected boolean hasESP; protected boolean isVegetarian;
this is java problem.
---------------------------------------------------------------------------------------
public abstract class Martian implements Cloneable, Comparable
private int id;
private int volume;
protected boolean hasESP;
protected boolean isVegetarian;
public Martian(int id, int volume) {
this.id = id;
this.volume = volume;
}
public Martian(int id) {
this(id,1);
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public boolean equals(Object o) {
Martian m = (Martian)o;
return this.id == m.getId();
}
public int getId() {
return id;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
public abstract String speak();
public String toString() {
String msg = String.format("Martian id=%d vol=%d", id, volume);
return msg;
}
@Override
public int compareTo(Martian m) {
return this.getId() - m.getId() ;
}
}
------------------------------------------------------------------
public class RedMartian extends Martian {
public RedMartian(int id, int volume) {
super(id,volume);
}
public RedMartian(int id) {
super(id);
}
public String speak() {
return getId() + " Rubldy Rock";
}
@Override
public String toString() {
return "Red " + super.toString();
}
}
--------------------------------------------------------------------------------------
public class LimeMartian extends GreenMartian {
public LimeMartian(int id, int volume) {
super(id,volume);
}
public LimeMartian(int id) {
super(id);
}
@Override
public String speak() {
return getId() + " Limey Lock";
}
@Override
public String toString() {
return "Lime " + super.toString();
}
}
------------------------------------------------------------------------
public class GreenMartian extends Martian {
public GreenMartian(int id, int volume) {
super(id,volume);
}
public GreenMartian(int id) {
super(id);
}
@Override
public String speak() {
return getId() + " Grubldy Grock";
}
@Override
public String toString() {
return "Green " + super.toString();
}
}
----------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class GenericMartianManagerDriver {
public static void main(String[] args) {
GenericMartianManager
RedMartian r1 = new RedMartian(8);
GreenMartian g1 = new GreenMartian(11);
GreenMartian g2 = new GreenMartian(4);
GreenMartian g3 = new GreenMartian(10);
GreenMartian g4 = new GreenMartian(4);
GreenMartian lime1 = new LimeMartian(2);
LimeMartian lime2 = new LimeMartian(48);
Martian mart1 = new GreenMartian(91);
// Test 1 - MartianManager.addMartian()
System.out.println("***Test 1 - MartianManager.addMartian()");
System.out.print(mm.addMartian(g1) + ", ");
System.out.print(mm.addMartian(g2) + ", ");
System.out.print(mm.addMartian(g3) + ", ");
System.out.print(mm.addMartian(g4) + ", ");
// Uncomment and should not compile.
// System.out.print(mm.addMartian(mart1) + ", ");
// System.out.print(mm.addMartian(r1) + ", ");
System.out.print(mm.addMartian(lime2) + ", ");
System.out.println(mm.addMartian(lime1));
// Test 2 - MartianManager.getMartian()
System.out.println(" ***Test 2 - MartianManager.getMartian()");
Martian m = new GreenMartian(16);
// Uncomment and should not compile.
// GreenMartian m2 = mm.getMartian(m);
GreenMartian gm1 = new GreenMartian(11);
gm1.setVolume(999);
// Uncomment and should not compile.
// LimeMartian m0 = mm.getMartian(gm1);
GreenMartian gm2 = mm.getMartian(gm1);
System.out.println( gm2 );
System.out.println( mm.getMartian(new GreenMartian(888)) );
// Test 3 - MartianManager.mergeMartians()
System.out.println(" ***Test 3 - MartianManager.mergeMartians()");
ArrayList
redMarts.add(r1);
// Uncomment and should not compile.
// mm.mergeMartians(redMarts);
ArrayList
LimeMartian lime3 = new LimeMartian(33,20);
limeMarts.add(lime3);
mm.mergeMartians(limeMarts);
System.out.println( "Num=" + mm.getNumMartians() + ", " + mm.getMartian(lime3) );
// Test 4 - MartianManager.removeMartian()
System.out.println(" ***Test 4 - MartianManager.removeMartian()");
Martian m4 = new RedMartian(39,2);
// Uncomment and should not compile.
// System.out.print( mm.removeMartian(m4) + ", " );
LimeMartian m5 = new LimeMartian(10,2);
System.out.print( mm.removeMartian(m5) + ", " );
LimeMartian m6 = new LimeMartian(111,2);
System.out.print( mm.removeMartian(m6) + ", " );
// test5();
}
public static void test5() {
// Test 5 - getSmallest() static generic method
System.out.println(" ***Test 5 - getSmallest()");
GreenMartian g1 = new GreenMartian(11);
GreenMartian g2 = new GreenMartian(4);
GreenMartian g3 = new GreenMartian(10);
GreenMartian lime1 = new LimeMartian(2);
LimeMartian lime2 = new LimeMartian(48);
ArrayList
// Uncomment and should not compile.
// ArrayList
ArrayList
// Uncomment and should work correctly
// ArrayList
// System.out.println(smallGreens);
}
}
ou will find martian classes: Martian, GreenMartian, RedMartian, and LimeMartian. The GreenMartian class has been changed from previous assignments and no longer implements Teleporter. You will write a generic martian manager class named GenericMartianManager. The generic type should be bound so that it is Martian or a subclass. The class has these features: 1. GenericMartianManager Class: Member martians addMartian(m):boolean Description A list objects of the generic type. Adds m, which should be constrained to be of the generic type of this class, to martians, only when it doesn't already exist in the list of martians, Remember, martians override equals and implement Comparable. If the add is successful, then return true, otherwise return false. Return the total number of martians. m should be constrained to be of the generic type of this class. The method retrieves and returns the copy of this object found in martians, The return type should be of the generic type for this class. Hint: use indexOf. Return null if not found etNumMartiansO:int getMartian m mergeMartians (ArrayList marts This method should accept a list whose type is the generic type of this class or any subtype of this generic type. The method should add the objects in marts to martians. Hint: this is a generic method (but not static) and can utilize a bounded wildcard. Removes m, which should be constrained to be of the generic type of this class, from martians. If the remove is successful, then return true, otherwise return false, removeMartian m):boolean ou will find martian classes: Martian, GreenMartian, RedMartian, and LimeMartian. The GreenMartian class has been changed from previous assignments and no longer implements Teleporter. You will write a generic martian manager class named GenericMartianManager. The generic type should be bound so that it is Martian or a subclass. The class has these features: 1. GenericMartianManager Class: Member martians addMartian(m):boolean Description A list objects of the generic type. Adds m, which should be constrained to be of the generic type of this class, to martians, only when it doesn't already exist in the list of martians, Remember, martians override equals and implement Comparable. If the add is successful, then return true, otherwise return false. Return the total number of martians. m should be constrained to be of the generic type of this class. The method retrieves and returns the copy of this object found in martians, The return type should be of the generic type for this class. Hint: use indexOf. Return null if not found etNumMartiansO:int getMartian m mergeMartians (ArrayList marts This method should accept a list whose type is the generic type of this class or any subtype of this generic type. The method should add the objects in marts to martians. Hint: this is a generic method (but not static) and can utilize a bounded wildcard. Removes m, which should be constrained to be of the generic type of this class, from martians. If the remove is successful, then return true, otherwise return false, removeMartian m):booleanStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started