Question

public class Player { private String name; private int health; public Player(String name) { this.name =...

public class Player {

  private String name;
  private int health;
  
  public Player(String name) {
    this.name = name;
  }

}

Write a complete method using java to find a Player by name in an array of Player objects. Use a linear search algorithm. The method should either return the first Player object with the requested name, or null if no player with that name is found.

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

//PLease find below the java code for the above statement

import java.io.*;
import java.util.Scanner;

public class Player {

private String name;
private int health;
  
public Player(String name) {
this.name = name;
}
  
//function to obtain player name of an object
public String getPlayerName(){
return name;
}
}

//The Driver class having searchPlayer and main functions
class Driver {
  
//the function to search for player names in array of objects
static Player searchPlayer(Player []players, String name){
  
//looping through all objects and comparing the names by linear search
for(int i=0;i<players.length;i++){
  
if(name.equals(players[i].getPlayerName())){
return players[i];
}
  
}
  
//if object not found
return null;
}
  
//main function to test the functions
   public static void main (String[] args) {
      
       Player[] players=new Player[3]; // Creating array of objects
      
       //initializing some values
       players[0]=new Player("John");
       players[1]=new Player("Andy");
       players[2]=new Player("Smith");
  
//taking input
Scanner in=new Scanner(System.in);
String name=in.nextLine();
  
//Obtaining the Output
Player output1=searchPlayer(players,name);
  
//checking for null values
if(output1==null){
System.out.println(name+" Object not found: returned NULL");
}
else{
System.out.println(name+" PLayer name Object found. Output of getPlayerName: "+output1.getPlayerName());
}
   }
}

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

INPUT1:

Output 1:

INput 2:

Output 2:

Add a comment
Know the answer?
Add Answer to:
public class Player { private String name; private int health; public Player(String name) { this.name =...
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
  • Here is the code for the Infant class: public class Infant{ private String name; private int...

    Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...

  • Write Junit test for the following class below: public class Player {       public int turnScore;    public int roundScore;    public int lastTurnScore;    public String name;    public int chipPile;...

    Write Junit test for the following class below: public class Player {       public int turnScore;    public int roundScore;    public int lastTurnScore;    public String name;    public int chipPile;       public Player (String name) {        this.name = name;        this.turnScore = 0;        this.chipPile = 50;    }          public int getChip() {        return chipPile;    }       public void addChip(int chips) {        chipPile += chips;    }       public int getRoundScore() {        return roundScore;    }       public void setRoundScore(int points) {        roundScore += points;    }       public int getTurnScore() {        return turnScore;   ...

  • public class Fish { private String species; private int size; private boolean hungry; public Fish() {...

    public class Fish { private String species; private int size; private boolean hungry; public Fish() { } public Fish(String species, int size) { this.species = species; this.size = size; } public String getSpecies() { return species; } public int getSize() { return size; } public boolean isHungry() { return hungry; } public void setHungry(boolean hungry) { this.hungry = hungry; } public String toString() { return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species; } }Define a class called Lake that defines the following private...

  • 1. private Node head; private Node tail; public class Node { String name; Node next; Node...

    1. private Node head; private Node tail; public class Node { String name; Node next; Node prev; } public void displayInReverse(){ //write your code here to display names in reverse } 2. public class NodeOne { String name; NodeOne next; public NodeOne(String name) { this.name = name; } } // Complete the code snippet below so that the delete method works properly public void delete(String name) { NodeOne temp = head, prev = head; while (temp != null) { if...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • Write Junit test for the following class below: public class Player {       public int...

    Write Junit test for the following class below: public class Player {       public int turnScore;    public int roundScore;    public int lastTurnScore;    public String name;    public int chipPile;       public Player (String name) {        this.name = name;        this.turnScore = 0;        this.chipPile = 50;    }          public int getChip() {        return chipPile;    }       public void addChip(int chips) {        chipPile...

  • public class Pet {    //Declaring instance variables    private String name;    private String species;...

    public class Pet {    //Declaring instance variables    private String name;    private String species;    private String parent;    private String birthday;    //Zero argumented constructor    public Pet() {    }    //Parameterized constructor    public Pet(String name, String species, String parent, String birthday) {        this.name = name;        this.species = species;        this.parent = parent;        this.birthday = birthday;    }    // getters and setters    public String getName() {        return name;   ...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • public class Animal {    private String name; //line 1    private int weight; //line 2...

    public class Animal {    private String name; //line 1    private int weight; //line 2    private String getName(){       return name;    } //line 3    public int fetchWeight(){       return weight; } //line 4 } public class Dog extends Animal {    private String food; //line 5    public void mystery(){       //System.out.println("Name = " + name); //line 6            System.out.println("Food = " + food); //line 7    } } I want to know the super...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

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