Question

In: Computer Science

SLUGGING PERCENTAGES & BATTING AVERAGES - SENTINEL LOOP Develop a program in JAVA that will determine...

SLUGGING PERCENTAGES & BATTING AVERAGES - SENTINEL LOOP

Develop a program in JAVA that will determine the slugging percentages and batting average of several New York Yankees from the 2006 season.  Slugging percentage is calculated by dividing the total number of bases by the number of at bats.  The number of bases would be one for every single, two for every double, three for every triple, and four for every home run.  The batting average is calculated by dividing the total number of hits by the number of at bats.  You do not know the number of players in advance, but for each player you know their number of singles, doubles, triples, home runs, total number of at bats, and the player's name (use the proper type and method for each variable).

You will use a while sentinel loop on the 1st item to input.  If it is not the sentinel, go into the while sentinel loop and separately input the rest of the input items (be careful of the enter in the input memory buffer for the player name).  You will then calculate the total bases and the slugging percentage.  You will then calculate the batting average.  You will then print out the player's name, the labeled slugging percentage for that player to three decimal places, and the labeled batting average for that player to three decimal places.  Use separate output statements.  Print a blank line between players.  This loop will repeat for as many players as you need.

Run your program with the following players and sentinel value (to show the sentinel value worked):

Singles:  158

Doubles:  39

Triples:  3

Home Runs:  14

At Bats:  623

Player:  Derek Jeter

Singles:  51

Doubles:  25

Triples:  0

Home Runs:  37

At Bats:  446             

Player:  Jason Giambi                                                                                                          

Singles:  104

Doubles:  26

Triples:  1

Home Runs:  35

At Bats:  572             

Player:  Alex Rodriguez                                                                  

Singles: -1

Solutions

Expert Solution

Solution: Things to remember:

  • To take user input, use Scanner class
  • To print output upto 3 decimal places, use String.format()
  • I have used sentinel value (singles) to decide whether while loop will run or not. It terminates when (singles=-1)
import java.util.*;
import java.lang.*;
import java.io.*;

class Baseball
{
        public static void main (String[] args) throws java.lang.Exception
        {
                int singles=-1, doubles=0, triples=0, homerun=0, bats=0;
                String name="";
                
                Scanner in = new Scanner(System.in); 
                
        String input = in.nextLine();
        String[] s = input.split(":", 2);
        try { 
            singles = Integer.parseInt(s[1].trim());
        } 
        catch (NumberFormatException e) { 
        }
  
        while(singles != -1){
            input = in.nextLine();
            s = input.split(":", 2);
            try { 
                doubles = Integer.parseInt(s[1].trim());
            } 
            catch (NumberFormatException e) { 
            }
            
            input = in.nextLine();
            s = input.split(":", 2);
            try { 
                triples = Integer.parseInt(s[1].trim());
            } 
            catch (NumberFormatException e) { 
            }
            
            input = in.nextLine();
            s = input.split(":", 2);
            try { 
                homerun = Integer.parseInt(s[1].trim());
            } 
            catch (NumberFormatException e) { 
            }
            
            input = in.nextLine();
            s = input.split(":", 2);
            try { 
                bats = Integer.parseInt(s[1].trim());
            } 
            catch (NumberFormatException e) { 
            }
            
            input = in.nextLine();
            s = input.split(":", 2);
            name = s[1];
            
            int bases = 0, hits = 0;
            
            bases = 1*singles + 2*doubles + 3*triples + 4*homerun;
            hits = singles + doubles + triples + homerun;
            
            float slug = (float)bases/bats;
            float bat_avg = (float)hits/bats;
            
            System.out.println("Name : " + name); 
            System.out.println("Slugging percentage : " + String.format("%.3f", slug)); 
            System.out.println("Batting average : " + String.format("%.3f", bat_avg)); 
            System.out.println(); 
            
            
            input = in.nextLine();
            s = input.split(":", 2);
            try { 
                singles = Integer.parseInt(s[1].trim());
            } 
            catch (NumberFormatException e) { 
            }
        }
        }
}

Input format:

Singles: 158
Doubles: 39
Triples: 3
Home Runs: 14
At Bats: 623
Player: Derek Jeter
Singles: 51
Doubles: 25
Triples: 0
Home Runs: 37
At Bats: 446   
Player: Jason Giambi Singles: 104
Doubles: 26
Triples: 1
Home Runs: 35
At Bats: 572   
Player: Alex Rodriguez Singles: -1

Output:

Name : Derek Jeter
Slugging percentage : 0.483
Batting average : 0.343

Name : Jason Giambi   
Slugging percentage : 0.558
Batting average : 0.253

Name : Alex Rodriguez
Slugging percentage : 0.523
Batting average : 0.290

PS: Let me know if you face any issue to run the code or have any doubts.


Related Solutions

Java Script. Develop a program that will determine the slugging percentages and batting average of several...
Java Script. Develop a program that will determine the slugging percentages and batting average of several New York Yankees from the 2006 season. Slugging percentage is calculated by dividing the total number of bases by the number of at bats. The number of bases would be one for every single, two for every double, three for every triple, and four for every home run. The batting average is calculated by dividing the total number of hits by the number of...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of the grades entered. When computing the average, make sure that there is...
Batting Averages Random samples of batting averages from the leaders in both leagues prior to the...
Batting Averages Random samples of batting averages from the leaders in both leagues prior to the All-Star break are shown. At the 0.01 level of significance, can a difference be concluded? National 0.311 0.328 0.324 0.338 0.326 American 0.348 0.325 0.352 0.321 0.333 Compute the test value. Always round t- score value to three decimal places. Do not round intermediate steps. Reject or do not reject the null hypothesis. There is or is not enough evidence to support the claim?
I have to use a sentinel while loop to complete the following task in a java...
I have to use a sentinel while loop to complete the following task in a java program, I want to see how this is executed so I can better understand how the sentinel while loop works. Thank you! Convert Lab 10 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User...
Create a python program that contains a while loop together with a Sentinel (0) to process...
Create a python program that contains a while loop together with a Sentinel (0) to process indefinite item costs that are purchased online from a vendor. Be sure that you assign a variable SENTINEL to 0 to use in the Boolean condition of your while loop. A sales tax rate of 6.25% is applied to the subtotal for the items purchased. Be sure you assign a variable, TAXRATE to 0.0625. The program is to process a number of items, numItems,...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of...
You are working for the Red Sox and are asked to communicate the batting averages to...
You are working for the Red Sox and are asked to communicate the batting averages to the managers / coaches. What type of graph, chart would you use and why? What method of sampling would you used if asked to find out what type of soft drinks/sodas high school students like? How would you collect the data? You are an analyst in the medical field and your first assignment is in primary care. You want to see which is more...
Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
Suppose that the batting averages in baseball are normally distributed so that the mean is .258...
Suppose that the batting averages in baseball are normally distributed so that the mean is .258 and the standard deviation is 0.04. What is the probability that a player has a batting average of (A) More than .258? (B) More than .298? (C) More than .318?
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT