VIl. (5 points) The following class contains code to define a Nintendo character
ID: 3919454 • Letter: V
Question
VIl. (5 points) The following class contains code to define a Nintendo character where characters have a name and a primary outfit color. The main method creates 3 instances of this, each different characters in the game Update the main method to add these three characters to a collection (of any type), then iterate through the collection and print each character. Next, sort the collection and print each character again public class NintendoCharacter implements Comparablet private Color color: private String name: public Nintendocharacter Color outfitcolor, string characterName) this.coloroutfitColor; this.namecharacterName eoverride public int compareTo (Object o) return color·tostring() .compa reto ( ((Nintendocharacter)?.color, tostring() ); public String tostring return name + ":" + color.tostring) public static void main Stringll args) NintendoCharacter m = new NintendoCharacter (Color. red, ?Ma rio"); NintendoCharacter new NintendoCharacter (Color.gree, "Luigi" NintendoCharacter pnew NintendoCharacter (Color.magenta, "Princess Peach" CS 3443-SAMPLE FINAL EXAM 8 of 11Explanation / Answer
import java.awt.*; import java.util.ArrayList; import java.util.Comparator; public class NintendoCharacter implements Comparable { private Color color; private String name; public NintendoCharacter(Color color, String name) { this.color = color; this.name = name; } @Override public int compareTo(Object o) { return 0; } @Override public String toString() { return "NintendoCharacter{" + "color=" + color + ", name='" + name + ''' + '}'; } public static void main(String[] args) { NintendoCharacter m = new NintendoCharacter(Color.red, "Mario"); NintendoCharacter l = new NintendoCharacter(Color.green, "Luigi"); NintendoCharacter p = new NintendoCharacter(Color.magenta, "Princess Peach"); ArrayList list = new ArrayList(); list.add(m); list.add(l); list.add(p); for(int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.