Question
Some superheros have superpowers, and some have cool gadgets. Answer the following questions using the Superhero class and the SuperheroWithPowers subclass. a. Write a toString
Some superheros have superpowers, and some have cool gadgets. Answer the following questions using the Superhero class and the SuperheroWithPowers subclass.
a. Write a toString method for the Superhero class and the SuperheroWithPowers subclass that prints the superclass name and the values of its fields like in the examples below. Only print the real name field if the superheros identity is not secret. SuperheroWithPowers[name=Spiderman][superpowers=Superhuman strength, cling to surfaces] SuperheroWithPowers[name=Thor,realName=Thor Odinson][superpowers=Superhuman strength, Superhuman endurance, Highly resistant to injury]
b. Implement an equals method for the Superhero class and the SuperheroWithPowers class.
c. Assuming you have an array list of Superheros called theAvengers, write statements to serialize the array list. You can assume the array has already been initialized. What needs to be modified in the Superhero class definition if you dont want realName to be serialzed?
import java.io.Serializable;
/** * Represents a superhero. */
public class Superhero implements Serializable {
private final String superheroName; private final String realName; private final boolean keepIdentitySecret;
/** * Construct a new superhero with the specified values. * @param superheroName the superhero name of the superhero * @param realName the real name of the superhero * @param secret whether the superhero's identity is secret */
public Superhero(String superheroName, String realName, boolean secret) {
this.superheroName = superheroName; this.realName = realName; this.keepIdentitySecret = secret;
}
/** * Return the name of the superhero. * @return the superhero's name */
public String getSuperheroName() { return superheroName;
}
/** * Return this superhero's real name, as long as their identity * is not secret. * @return the superhero's real name */
public String getRealName() { if (keepIdentitySecret)
return superheroName; else
return realName;
}}
import java.util.ArrayList;
/** * This class represents those superheros who have superpowers, * as opposed to superheros who have cool gadgets. */
public class SuperheroWithPowers extends Superhero {
private ArrayListsuperpowers;
/** * Construct a new superhero. * @param superheroName the name of the superhero * @param realName the superhero's real name * @param identitySecret whether the superhero's identity is secret */
public SuperheroWithPowers(String superheroName, String realName,
boolean identitySecret) {
super(superheroName, realName, identitySecret);
this.superpowers = new ArrayList<>(); }
/** * Add a superpower to this superhero's list of superpowers. * @param superpower */
public void addSuperpower(String superpower) { superpowers.add(superpower);
}
/** * Get a list of this superhero's superpowers. * @return the superheros superpowers */
public ArrayListgetSuperpowers() { return (ArrayList ) superpowers.clone();
} }
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