id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

jebbert@volusia.k12.fl.us > test2022_01_20_SLHL_hits
prev  |  next  |  chance

Write a method that plays a form of "tank battle". In this game, tank locations are given along a linear path in the array 'locations'. A volley of shots is taken at the tanks. The locations where the shots land are given in the array 'shots'. A shot is considered a hit if it is within 'range' of the location of a tank. If 'range' is zero, only direct hits count. Write a method that returns the number of tanks that were hit by the volley of shots. Note taht a single tank can be hit multiple times, but it only counts as ONE tank being hit (hitting the same tank multiple times only counts once). Look at the test data for examples. Note that one shot can hit multiple target locations if the range is large enough. For example, if locations={200,210,212,500} and shots={208,350,12} with range=15 the result is 3 targets hit because the 208 shot is within 15 of three different tanks! As preconditions you may assume that only non-negative values will be used for all locations, shots, and ranges. You may also assume that each element in 'locations' will be a unique location.


test2022_01_20_SLHL_hits([15, 25, 60, 100, 120, 150], [65], 7) → 1
test2022_01_20_SLHL_hits([200, 330, 700, 100, 12, 5000], [2000], 4000) → 6
test2022_01_20_SLHL_hits([512, 530, 550, 600, 602, 615], [601, 604, 529, 900], 12) → 4

...Save, Compile, Run (ctrl-enter)

public int test2022_01_20_SLHL_hits(int[] locations, int[] shots, int range) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 290

Copyright Nick Parlante 2017 - privacy