Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Please follow the instructions carefully. Thank you! For the second activity, I outputted the superhero class below. SUPERHERO CLASS public class Superhero{ private String alias;

Please follow the instructions carefully. Thank you! For the second activity, I outputted the superhero class below.

SUPERHERO CLASS

public class Superhero{

private String alias;

private String superpower;

private int health;

public Superhero(){

alias= "unkown";

superpower= "unknown";

health= 50;

//Realized I did not use default constructor while going through instructions of lab

}

public Superhero(String alias1, String superpower1,int health1 ){

alias=alias1;

superpower=superpower1;

if(health1>=0 && health1

health= health1;

else if(health150)

health=25;

}

public void setalias(String alias1){

alias=alias1;

}

public void setsuperpower(String superpower1){

superpower=superpower1;

}

public void sethealth(int health1){

if (health1>=0 && health1

health= health1;

}

else if(health150){

health=25;

}

}

public String getalias(){

return alias;

}

public String getsuperpower(){

return superpower;

}

public int gethealth(){

return health;

}

public String toString(){

String info = "Alias: " + alias + " " +

"Superpower: " + superpower+ " " +

"Health: " + health + " ";

return info;

}

public int numberofPeopleSaved(){

int numberpeoplesaved= (int) (101*Math.random());

return numberpeoplesaved;

}

public void harmtoHealth(){

health= health-5;

if (health

health=0;

//Health not returned

}

public int fight(){

int randomint= (int) (21*Math.random());

return randomint;

//Health not returned

}

}

image text in transcribed

Lab: Enhanced For Loops+ Methods Activity 1 Write a program called AnalyzeNumbers. This program will have array that holds 10 random integers from 1 to 5. Output the 10 random numbers to the screen using an enhanced for loop. This program will also have a method called frequency that takes an integer array as the parameter and it will return an array that holds the frequency of numbers. Index 0 will hold the frequency of 1, index 1 will hold the frequency of 2, and so on. Use this method in your program to create an array that holds the frequency of the random numbers in your array. Use an enhanced for loop to output the frequency of each number. Then display the number that appears the most in your array-you will need to account for numbers appearing the same amount for the mode. Activity 2 Write a program called MyHeroes. This program will use an initializer list to create an array of type SuperHero that has 3 elements. Make sure to give each Super Hero a name, superpower, and a random integer (1 to 50) for the health. This program will also have a method called thehealthiest which takes a SuperHero array as a parameter and returns the SuperHero that has the highest health value. If there is a tie, the last superhero compared will be the one with the highest health value. Output all of the super hero's names and health values. You will also need to check that each element in the array was instantiated so check to see that each element is not null to prevent run-time errors. Then output the super hero that has the highest health using the method you wrote. "You will need to have your SuperHero class in your project space

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions