Question
1) (5 points) The following class contains code to de?ne a Nintendo character - where characters have a name and a primary out?t color. The
1) (5 points) The following class contains code to de?ne a Nintendo character - where characters have a name and a primary out?t 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 Comparable{
private Color color;
private String name;
public NintendoCharacter( Color outfitColor, String characterName ){
this.color = outfitColor;
this.name = characterName; }
@Override
public int compareTo(Object o) {
return color.toString().compareTo( ((NintendoCharacter)o).color.toString() ); }
public String toString(){ return name + : + color.toString(); }
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);
2) .(10 points) Write a generic class called Bag. It should have three methods as follows: The add method should take in two generic values and save them into the theoretical bag, returning nothing from the method. The contains method should take the second type of generic parameter, and return whether or not that value is located in the theoretical bag. The isEmpty method should take no parameters and return true if the bag is empty, and false otherwise. For example, the class should support the following behavior:
Bag
Bag
Bag
bag1.add( 20, 92 );
bag2.add( 20, A- );
bag3.add( B", 84.5 );
// prints false
System.out.printf( Bag 1 is empty? %s , bag1.isEmpty() );
// prints false
System.out.printf( Found B in bag 2? %s , bag2.contains(B) );
// prints true
System.out.printf( Found 84.5 in bag 3? %s , bag3.contains(84.5) );
Step 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