Loading...
Loading...
Two batters face balls one at a time. After an odd number of runs (1 or 3) they swap ends, and after every sixth ball (end of an over) they swap ends again. Print each batter's runs, balls and strike rate, and who is on strike at the end.
Write a class Batter with fields name, runs and balls and methods void face(int r) and double strikeRate() (runs × 100 / balls, or 0 for no balls). In main, keep two references, striker and other, and swap the references, not the data.
name runs (balls) SR x.xx. Then ON STRIKE name.Example
Input
Rohit Gill 7 1 4 0 2 1 6 1
Output
Rohit 7 (2) SR 350.00 Gill 8 (5) SR 160.00 ON STRIKE Rohit
Rohit takes a single, Gill scores 4, 0, 2 and a single, Rohit hits 6 off the last ball, the over ends so Gill faces ball 7, and his single brings Rohit back on strike.
Try the problem first. Open one hint at a time.
Batter striker = a; Batter other = b; makes two more arrows to the same two objects.
Swap with a temporary variable: Batter t = striker; striker = other; other = t;.
On the sixth ball an odd run and the end of the over cancel out: the batter keeps the strike.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor