Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Some superheros have superpowers, and some have cool gadgets. Answer the followi

ID: 3720867 • Letter: S

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 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 superhero’s 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 don’t want realName to be serialzed?

}

}

}}

}

} }

Explanation / Answer

modified code is in bold.

please do upvote.If you have any problem do comment and i will be happy to help you.Thanks for using chegg.

----------

answer for a,b:

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;

}

  

public String toString()

{

String s="";

if(keepIdentitySecret)

{

s=String.format("%s[name=%s]",this.getClass().getName(),this.superheroName);

}

else

{

s=String.format("%s[name=%s,realName=%s]",this.getClass().getName(),this.superheroName,this.realName);

}

return s;

}

@Override

public boolean equals(Object o)

{

Superhero other=(Superhero)o;

if(realName.equalsIgnoreCase(other.realName) && this.superheroName.equalsIgnoreCase(other.superheroName))

{

return true;

}

return false;

}

}

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

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<String> 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 superhero’s superpowers

*/

public ArrayList<String> getSuperpowers() {

return (ArrayList<String>) superpowers.clone();

}

  

public String toString()

{

String s=super.toString();

s=s+"[superpowers=";

for(String power : superpowers)

{

s = s+power;

}

s = s + "]" ;

return s;

}

  

//equals method is same for both classes because superowers are private i cannot use

//it in equals method

}

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

answer for c:

i)

for serializing an array list, the Class of arrayList i.e SuperHeroes must implement serializable and it already does so you you just write the arrayList directly to desired file

e.g

ii)

make realName as transient ,which will prevent it to be serialized ,as follows:

private transient final String realName;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote