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 ArrayList superpowers; 
 /** * 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 ArrayList getSuperpowers() { return (ArrayList) superpowers.clone(); 

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!