In: Advanced Math
**Program needs to be in matlab format**
A bowling match consists of ten frames. Each frame except for the tenth consists of one or two balls, or attempts to knock down the ten pins at the end of the alley. Doing so on the first ball of the frame is called a strike, and the second ball of the frame is not rolled. Knocking down all ten pins with both balls (having left some up with the first ball) is called a spare. If both attempts to knock down the pins leave some standing, the frame is called an open frame. A spare in the tenth frame gives the bowler one extra ball; a strike in the tenth gives him or her two extra balls. A bowling score is computed as follows. A strike counts as 10 points plus the sum of the next two balls. A spare counts as 10 points plus the next ball. Any other balls merely count as themselves, as do any bonus balls rolled as a result of a strike or a spare in the tenth frame. Suppose for example that the sequence of balls was 9 1 0 10 10 10 6 2 7 3 8 2 10 9 0 9 1 10 The score for the ten frames would be Frame score ----- ----- 1 10 2 30 3 56 4 74 5 82 6 100 7 120 8 139 9 148 10 168 Your goal in this project is to write a program to accept from standard input the scores for a sequence of balls and output the scores for the ten frames. The program should ask for input for each frame, asking for a second score if the first roll was not a strike. At most bowling alleys, the computerized scoring machines congratulate bowlers when they are doing a good job. When a bowler achieves consecutive strikes, output the following, according to the number of consecutive strikes: No. of consecutive strikes Output 2 Double! 3 Turkey! 4 Hambone! 5 Yahtzee! 6 Wild Turkey! This is a two week homework. For this homework, you will need to come up with an outline of goals for the project. By the end of the first week, your program should be able to do the following things: -Add up scores for the first nine frames (do NOT worry about the tenth frame yet) for both simple bowls (no strikes or spares) and frames that involve spares You will evaluate each other's code in class with several tests to see if the code does indeed successfully calculate simple games and games that involve spares. The complete checklist that you will be evaluated on is below: -The program runs -The program asks for scores one at a time (not for all of them at once) -The program asks for scores for 9 frames -Test the program for the following easy case: Every frame you bowl zero for both times. --Does the program output a final score? --Is the final score correct (i.e. 0)? --Does the program output the score at the end of every frame? -Next, test the program for the next most difficult case: Every frame you bowl a score of 1. -Is the final score correct (i.e. 18)? -Next, test the program to see if it successfully calculates spares. For the first two frames, the bowler bowls a 5 on the first throw and a 5 on the second throw. For frames 3-9, the bowler throws zeros. -Is the final score correct (i.e. 25)? -Lastly, test the program for a case in which you have many spares. For every frame, the bowler bowls a 5 on the first throw and a 5 on the second throw. -Is the final score correct (i.e. 135)? -The program does not crash (i.e. encounter Octave errors) for any of the test cases.
IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE TTHERE TO HELP YOU..ALL THE BEST..
CODE:
package com.dummy.suriya; import java.util.*; public class BowlingAlleyTest { private static final int MAX_SCORE = 10; public static void main(String args[]) { //To get the user input from the console Scanner sc = new Scanner(System.in); HashMap strikesMap = new HashMap(); int gameFinalScore = 0; HashMap<Integer,ArrayList<Integer>> framesScoresMap = new HashMap<>(); ArrayList<Integer> ballScoresList = new ArrayList<Integer>(); for(int i=0;i<=9;i++) { int frameFinalScore = 0; for(int j=0;j<=1;j++) { System.out.println("Please enter the score for : Frame "+(i+1)+" Ball "+(j+1)+":::"); int currentBallScore = sc.nextInt(); ballScoresList.add(j,currentBallScore); frameFinalScore += currentBallScore; //gameFinalScore += scoreball1; if(!checkStrike(currentBallScore)) { //System.out.println("Please enter the score for : Frame "+(i+1)+" Ball "+(j+1)+":::"); strikesMap.put(i,0); //1-strike 0-no strike if(frameFinalScore == 10) { System.out.println("Congrats ..It's a Spare !!! Good Job !!!"); } } else { System.out.println("Congrats .. It's a Strike !!! Good Job !!!"); strikesMap.put(i,1); //1-strike 0-no strike break; } } framesScoresMap.put(i,ballScoresList); checkConsecutiveStrikes(strikesMap); } System.out.println("Your total score for 9 frames = "+calculateFinalScore(framesScoresMap)); } public static int calculateFinalScore(HashMap<Integer,ArrayList<Integer>> scoresMap) { int totalScore =0; if(scoresMap!=null && scoresMap.size()>0) { Iterator it = scoresMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); int mapKey = Integer.parseInt(pair.getKey().toString()); ArrayList<Integer> ballsScoreList = (ArrayList)pair.getValue(); boolean wasPreviousFrameStrike = false; boolean wasPreviousFrameSpare = false; if(ballsScoreList!=null) { int frameScore = 0; for(int ball:ballsScoreList) { int ballScore = ballsScoreList.get(ball); frameScore += ballScore; totalScore+= ballScore; if(frameScore == 10) { totalScore += scoresMap.get(mapKey+1).get(0); } if(ballScore == 10) { if(scoresMap.get(mapKey+1).size()==2) { totalScore += scoresMap.get(mapKey+1).get(0) + scoresMap.get(mapKey+1).get(1); } else { totalScore += scoresMap.get(mapKey+1).get(0) + scoresMap.get(mapKey+2).get(0); } } } } it.remove(); // avoids a ConcurrentModificationException } } return totalScore; } public static boolean checkStrike(int score) { if(score < MAX_SCORE) return false; else return true; } public static void checkConsecutiveStrikes(HashMap strikesMap) { if(strikesMap.size()>1) { int strikeCntr = 0; Iterator it = strikesMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); if(Integer.parseInt(pair.getValue().toString())==1) strikeCntr++; else strikeCntr=0; //reset } switch (strikeCntr){ case 2: System.out.println("Good Job. Two times Consecutive Strikes...Double!"); break; case 3: System.out.println("Good Job. Three times Consecutive Strikes...Turkey!"); break; case 4: System.out.println("Good Job. Four times Consecutive Strikes...Hambone!"); break; case 5: System.out.println("Good Job. Five times Consecutive Strikes...Yahtzee!"); break; case 6: System.out.println("Good Job. Six times Consecutive Strikes...Wild Turkey!"); break; default: System.out.println("No consecutive Strikes Yet !!!"); break; } } } }
I HOPE YOU UNDERSTAND..
PLS RATE THUMBS UP..ITS HELPS ME ALOT..
THANK YOU...!!