Question

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<=50)

          health= health1;

      else if(health1<0||health1>50)

          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<=50){

          health= health1;

      }

      else if(health1<0||health1>50){

          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<0)

          health=0;

//Health not returned

  }

  public int fight(){

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

      return randomint;

//Health not returned

  }

}



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 heros 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.util.Random;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Vamsi
*/

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<=50)

health= health1;

else if(health1<0||health1>50)

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<=50){

health= health1;

}

else if(health1<0||health1>50){

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<0)

health=0;

//Health not returned

}

public int fight(){

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

return randomint;

//Health not returned

}

}


public class MyHeroes {
  
//method to find super hero with highest health
static void Highest(Superhero a[])
{
  
int i=0,m=0;
for(i=0;i<a.length;i++)
{
if(i==0)m=0;
else
{
if(a[m].gethealth()<=a[i].gethealth())
m=i;
  
}
}
  
System.out.println("Super hero with highest health is:"+a[m].toString());
}
public static void main(String argv[])
{
//creating array of super heroes with 3 elements
Superhero s[]= new Superhero[3];
  
  
Random r = new Random();//to generate random health
  
  
//initializing array
  
String n,p;
int h;
h = (r.nextInt()%50)+1;
n="Hulk";
p="Smash";
s[0]=new Superhero(n,p,h);//initializing
  
h = (r.nextInt()%50)+1;
n="SuperMan";
p="SuperSpeed";
s[1]=new Superhero(n,p,h);//initializing
  
h = (r.nextInt()%50)+1;
n="BatMan";
p="Rich";
s[2]=new Superhero(n,p,h);//initializing
  
  
  
//now displaying all heroes
  
  
  
System.out.println("------Super heroes-------");
int i=0;
for(i=0;i<3;i++)
{
  
if(s[i]==null)break;//checking whether correctly initialized or not
System.out.println(s[i].toString());
}
  
if(i==3)//means all are correctly intialized
{
Highest(s);
  
}
}
}

output:

run:
------Super heroes-------
Alias: Hulk Superpower: Smash Health: 4
Alias: SuperMan Superpower: SuperSpeed Health: 30
Alias: BatMan Superpower: Rich Health: 25
Super hero with highest health is:Alias: SuperMan Superpower: SuperSpeed Health: 30
BUILD SUCCESSFUL (total time: 5 seconds)

Add a comment
Know the answer?
Add Answer to:
Please follow the instructions carefully. Thank you! For the second activity, I outputted the superhero class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please complete the lab following the guidelines using JAVA Activity 1 Write a program called AnalyzeNumbers....

    Please complete the lab following the guidelines using JAVA 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,...

  • I am having trouble understanding the concept of polymorphism. I know that I only need to...

    I am having trouble understanding the concept of polymorphism. I know that I only need to edit the MathDriver.java (driver) class, but I am not entirely sure how. Here is my MathDriver.java driver class, the one that I need to finish: Here is the parent class, Math.java: Lastly, here are the child classes, Addition, Subtraction, and Multiplication Given the following program, finish the driver class to demonstrate polymorphism. Program: PolyMath Copy and paste your driver class into the textbox for...

  • I need help asking the user to half the value of the displayed random number as...

    I need help asking the user to half the value of the displayed random number as well as storing each number generated by the program into another array list and displayed after the game is over at the end java.util.*; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in);    ArrayList<Integer> data = new ArrayList<Integer>(); int count = 0; while (!choice.equals("No")) { int randomInt =...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {   super();   System.out.println(“B() called”); } public...

  • I’m giving you code for a Class called GenericArray, which is an array that takes a...

    I’m giving you code for a Class called GenericArray, which is an array that takes a generic object type. As usual this is a C# program, make a new console application, and for now you can stick all of this in one big file. And a program that will do some basic stuff with it Generic Array List What I want you to do today: Create methods for the GenericArray, Easy(ish): public void Append (T, value) { // this should...

  • Java: Please help with my Tester. I'm having a hard time figuring out my error. Thank...

    Java: Please help with my Tester. I'm having a hard time figuring out my error. Thank you. What the tester requires: The AccountTester This class should consists of a main method that tests all the methods in each class, either directly by calling the method from the Tester or indirectly by having another method call a method. User and Bot information will be read from a file. A sample file is provided. Use this file format to aid in grading....

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT