AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Write a program to sum up the total number of overs in Cricket

in a game of cricket over are specified as 45.5 which means 45 over and five balls the digit following the decimal point gives the number of balls that is always less than six.write a program that would sum a series of such number and print result as two interger the first one specifing the number of overs and the second one giving the number of balls the series of inputs is terminated by zero.

corejava x 352
programming x 168
Posted On : 2018-05-26 20:21:43.0
profile Shivani Barik - anyforum.in Shivani Barik
400
up-rate
4
down-rate

Answers


Simple and straight forward approach is just convert all the input numbers into balls and sum it up. To display sum into overs, divide the totalBalls by 6 and takes the reminder as balls and display it like:
totalOvers.remainingBalls

Algorithm:
* Take the input numbers one by one
* For each input number:
-take the integer part and fraction part out
-take the addition of multiply by 6 into integer part and multiply by 10 into fraction part(actual input-integer part)
-add into totalBalls
* divide totalBalls by 6 to calculate the totalOvers and take the modulus 6 of it to calculate the remaining balls
* if reminder is greater than 0, then divide it by 10 and add into the totalOvers


Program:
--------------------------
import java.util.Scanner;

public class OversCalculator {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
double overs=0;
int totalBalls=0;
int temp=0;
while((overs=input.nextFloat())>0.0){
temp=(int)overs;//extract the complete overs
totalBalls=totalBalls+ temp*6+ (int)((overs-temp)*10);
}
overs=(totalBalls/6);
temp=totalBalls%6;
if(temp>0){
double remainingBalls=temp*(0.10);
overs=overs+remainingBalls;
System.out.println(overs);
}else{
System.out.println((int)overs);
}
}
}


Input-1:
----------------
1.2
7.5
5.5
0
Output-1:
----------------------
15

Input-2:
----------------
1.2
7.4
5.5
0
Output-2:
----------------------
14.5

Posted On : 2018-05-27 13:09:58
Satisfied : 0 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188222140
Reply This Thread
up-rate
0
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in