Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

------------------------------------ public class RedMartian extends Martian { public RedMartian(int id, int volume) { super(id,volume); } public RedMartian(int id) { super(id); } public String speak() {

image text in transcribed

------------------------------------

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 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();

}

}

------------------------------------

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 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();

}

--------------------------------------------

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

public class GenericMartianManagerDriver {

public static void main(String[] args) {

GenericMartianManager mm = new 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 = new ArrayList();

redMarts.add(r1);

// Uncomment and should not compile.

// mm.mergeMartians(redMarts);

ArrayList limeMarts = new 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 marts1 = new ArrayList(Arrays.asList(g1,g2,g3,lime1,lime2));

// Uncomment and should not compile.

// ArrayList smallGreens = getSmallest(marts1,3);

ArrayList greens = new ArrayList(Arrays.asList(g1,g2,g3,lime1,lime2));

// Uncomment and should work correctly

// ArrayList smallGreens = getSmallest(greens,3);

// System.out.println(smallGreens);

}

}

you will find martian classes: Martian, GreenMartian, RedMartian, and LimeMartian, The GreenMartion class has been changed from previous assignments and no longer implements Teleporter. You will write a generic matian manager class named GenericMortionManager. The generic type should be bound so that it is Martian or a subclass. The class has these 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 matigos only when it doesn't already_exist in the list of martians. Remember, martians override eguols and implement Comparoble. 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 martigns. The returrn type should be of the generic type for this class. Hint use indexOf, Return null if not found etNumMartians:int EetMartianim):. mergeMartians (Arravistmarts 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 morts 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 mgctigos If the remove is successful, then return true, otherwise return false 2. GenericMartignMonagerDriveClass The moin for this class is written class is the test code. . Run the test code and fix as necessary. Note that there are a number of places where you should uncomment a line of code and verify that your code generates a compile error on that line (then comment it back out). Also note that the very last line of main calls test5 which is commented out. You will uncomment it when you complete b below.. a. b. Write a static method, getSmalilest that accepts a list of Martian (or a list of any subclass of Martian) and and integer, num. The method should return a list of the same type as the input list that contains the Qum smallest elements without modifying the order of the input list. Hint: this is a static generic method that utilizes a bounded generic type parameter.. you will find martian classes: Martian, GreenMartian, RedMartian, and LimeMartian, The GreenMartion class has been changed from previous assignments and no longer implements Teleporter. You will write a generic matian manager class named GenericMortionManager. The generic type should be bound so that it is Martian or a subclass. The class has these 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 matigos only when it doesn't already_exist in the list of martians. Remember, martians override eguols and implement Comparoble. 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 martigns. The returrn type should be of the generic type for this class. Hint use indexOf, Return null if not found etNumMartians:int EetMartianim):. mergeMartians (Arravistmarts 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 morts 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 mgctigos If the remove is successful, then return true, otherwise return false 2. GenericMartignMonagerDriveClass The moin for this class is written class is the test code. . Run the test code and fix as necessary. Note that there are a number of places where you should uncomment a line of code and verify that your code generates a compile error on that line (then comment it back out). Also note that the very last line of main calls test5 which is commented out. You will uncomment it when you complete b below.. a. b. Write a static method, getSmalilest that accepts a list of Martian (or a list of any subclass of Martian) and and integer, num. The method should return a list of the same type as the input list that contains the Qum smallest elements without modifying the order of the input list. Hint: this is a static generic method that utilizes a bounded generic type parameter

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

2nd Edition

0470624701, 978-0470624708

More Books

Students also viewed these Databases questions