Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Question In this exercise, you need to write a static search method that will take an array of Comparable objects and a Comparable key

Java Question

In this exercise, you need to write a static search method that will take an array of Comparable objects and a Comparable key as parameter and it returns true if the key is present in the array, otherwise it should return false.

Test your implementation with two different type of Comparable objects. One possible idea to test your code is to create two arrays, one for Monster (from exercise 4) and another for Employee (from exercise 3), then pass each one to the search method for testing.

Save this file as SearchClient.java.

Here is Monster Code

/*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */  /**  * Say that you are writing a computer game that involves a hero who encounters  * various monsters. Monsters have several characteristics: hit points, strength,  * age, and name. The number of hit points a monster has is how far it is from  * death. A monster with a large number of hit points is hard to kill. The hero  * also has hit points.  * When the hit points of the hero reach zero, the game is over. The strength of * a monster affects how many hit points the hero looses when the monster hits * the hero. The age and name of the monster do not affect the outcome of battles * between the monster and a hero.  *  *   Can you make the necessary changes to the Monster class and write a tester  * class with main method that will present the hero with monsters in  * increasing order of difficulty.  * Hints: Monster needs a compareTo() method.  */ public class Monster implements Comparable{ private int hitPoints, strength, age; private String name; public Monster( int hp, int str, int age, String nm ) { hitPoints = hp; strength = str; this.age = age; name = nm; } public int getHitPoints() { return hitPoints; } public int getStrength() { return strength; } public int getAge() { return age; } public String getName() { return name; } @Override public String toString() { return "HP: " + getHitPoints() + " \tStr: " + getStrength() + "\t" + getName(); } public int compareTo(Monster monster){ if(this.hitPoints > monster.hitPoints){ return 1; } else if(this.hitPoints < monster.hitPoints){ return -1; } else{ if(this.strength > monster.strength){ return 1; } else if(this.strength < monster.strength){ return -1; } } return -1; } } 
import java.util.ArrayList; import java.util.Collections; public class MonsterClient { public MonsterClient(){} public static void main(String[] args) { Monster m1 = new Monster(500,500,50,"m1"); Monster m2 = new Monster(400,400,50,"m2"); Monster m3 = new Monster(300,300,50,"m3"); Monster hero = new Monster(700,700,50,"hero"); ArrayList monsters = new ArrayList<>(3); monsters.add(m1); monsters.add(m2); monsters.add(m3); Collections.sort(monsters); for(int i=0;i<3;i++){ System.out.println(hero.getName()+" is fighting with "+monsters.get(i).getName()); } } } 

Here is Employee Code

public class Employee implements Comparable { private String firstName; private String lastName; private int birthYear; String getFirstName() { return firstName; } String getLastName() { return lastName; } int getBirthYear() { return birthYear; } public Employee(String f, String l, int year) { firstName = f; lastName = l; birthYear = year; } public String toString() { String str = "Employee details: "; str = str + "Name: " + getFirstName() + " " + getLastName(); str = str + " Birthdate: " + getBirthYear(); return str; } public int compareTo(Employee other) { if(!this.getLastName().equals(other.getLastName())) { return this.getLastName().compareTo(other.getLastName()); } else if(!this.getFirstName().equals(other.getFirstName())) { return this.getFirstName().compareTo(other.getFirstName()); } else if(this.getBirthYear() > other.getBirthYear()) { return this.getBirthYear(); } else  return other.getBirthYear(); } } 
import java.util.Arrays; class EmployeeTester { public static void main ( String[] args ) { Employee[] workers = new Employee[12]; workers[0] = new Employee( "Fred", "Adams", 1963); workers[1] = new Employee( "John", "Adams", 1959); workers[2] = new Employee( "Elmer", "Adams", 1976); workers[3] = new Employee( "Nancy", "Devon", 1963); workers[4] = new Employee( "Andrew", "Lewis", 1983); workers[5] = new Employee( "Douglas", "Page", 1981); workers[6] = new Employee( "Donald", "Wolder", 1963); workers[7] = new Employee( "Henry", "Wolder", 1972); workers[8] = new Employee( "Robert", "Wolder", 1959); workers[9] = new Employee( "Howard", "Cohen", 1933); workers[10] = new Employee( "Howard", "Cohen", 1958); workers[11] = new Employee( "Donald", "Rice", 1935); Arrays.sort( workers ); for ( int j=0; jlength; j++ ) System.out.println( workers[j].toString() ); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Name and explain the most widely used neural network algorithm.

Answered: 1 week ago

Question

=+such as the dirigenti in Italy?

Answered: 1 week ago

Question

=+ Are there closed-shop requirements or practices?

Answered: 1 week ago