Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this is java problem. ---------------------------------------------------------------------------- import java.util.ArrayList; import java.util.Arrays; public class MartianSortTester { public static void main(String[] args){ Martian r1 = new RedMartian(1); Martian r2

this is java problem.

image text in transcribed

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

import java.util.ArrayList;

import java.util.Arrays;

public class MartianSortTester {

public static void main(String[] args){

Martian r1 = new RedMartian(1);

Martian r2 = new RedMartian(2);

Martian r3 = new RedMartian(3);

Martian r4 = new RedMartian(4);

Martian r5 = new RedMartian(5);

Martian r6 = new RedMartian(6);

Martian r7 = new RedMartian(7);

Martian g1 = new GreenMartian(1);

Martian g2 = new GreenMartian(2);

Martian g3 = new GreenMartian(3);

Martian g4 = new GreenMartian(4);

Martian g5 = new GreenMartian(5);

Martian g6 = new GreenMartian(6);

Martian g7 = new GreenMartian(7);

ArrayList martians = new ArrayList(Arrays.asList(

g5, g3, r2, g2, r3, g7, r5, r1, r7, g1, g4, r4, r6, g6 ));

System.out.println("Original Order:");

printMartianList(martians);

selectionSortRecursive(martians);

System.out.println("Sorted:");

printMartianList(martians);

martians = new ArrayList(Arrays.asList(

r5, r1, r4, r3, r7, r2, r6, g1, g2, g3, g4, g5, g6, g7 ));

System.out.println("Original Order:");

printMartianList(martians);

selectionSortRecursive(martians);

System.out.println("Sorted:");

printMartianList(martians);

}

public static void selectionSortRecursive(ArrayList martians){

// ---> WRITE THIS METHOD

}

private static void selectionSortRecursive(ArrayList martians, int endPos){

// ---> WRITE THIS METHOD

}

// Used for testing

private static void printMartianList(ArrayList martians) {

StringBuilder sb = new StringBuilder();

for(Martian m : martians) {

if(m instanceof RedMartian)

sb.append("R=");

else

sb.append("G=");

sb.append(m.getId() + ", ");

}

sb.delete(sb.length()-2, sb.length());

System.out.println(sb.toString());

}

}

25 points) You will write a recursive selection sort method that accepts an ArrayList of Martians. The Martians are Comparable so you will use the compareTo method to sort them. However, your method should only sort the RedMartians, leaving the GreenMartians in their original location. Two examples are shown below: Original Order: G-5, G-3, R-1, G-2, R-2, G7, R-3,R-4, R-5, G-1, G 4, R-6, R-7, G6 Original Order: R-5, R-1, R-4, R-3, R-7, R-2, R-6, G-5, G Sorted: 64, G-1, G6,3 Do the following: Get out the implementation of selection sort in the notes or book. Sketch a list of Martians,say 10 or so, a mix of red and green. Go through the algorithm figuring out what you need to change. Hint: the first and last elements of the unsorted portion of the list must be RedMartians. Thus, you'll need to add code to ensure this. Open the class MartianSortTester in the prob3 package and write code in the two indicated places. Test code is provided in main. 1. 2. 25 points) You will write a recursive selection sort method that accepts an ArrayList of Martians. The Martians are Comparable so you will use the compareTo method to sort them. However, your method should only sort the RedMartians, leaving the GreenMartians in their original location. Two examples are shown below: Original Order: G-5, G-3, R-1, G-2, R-2, G7, R-3,R-4, R-5, G-1, G 4, R-6, R-7, G6 Original Order: R-5, R-1, R-4, R-3, R-7, R-2, R-6, G-5, G Sorted: 64, G-1, G6,3 Do the following: Get out the implementation of selection sort in the notes or book. Sketch a list of Martians,say 10 or so, a mix of red and green. Go through the algorithm figuring out what you need to change. Hint: the first and last elements of the unsorted portion of the list must be RedMartians. Thus, you'll need to add code to ensure this. Open the class MartianSortTester in the prob3 package and write code in the two indicated places. Test code is provided in main. 1. 2

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

Step: 3

blur-text-image

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

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions