Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description of Program Write a program named BravesStats that displays batting s

ID: 3635085 • Letter: D

Question

Description of Program

Write a program named BravesStats that displays batting statistics from the Atlanta Braves 1999 regular season. A sample dialog with the program appears below, with user input in bold.

--------------------------------------------
|Commands: c - Show category leaders |
| p - Show stats for a player |
| t - Show totals for all players |
| q - Quit |
--------------------------------------------

Enter command (c, p, t, or q): c

At bats:
Boone, Bret 608
Runs scored:
Jones, Chipper 116
Hits:
Jones, Chipper 181
Doubles:
Jones, Chipper 41
Triples:
Jones, Andruw 5
Home runs:
Jones, Chipper 45
Runs batted in:
Jordan, Brian 115
Walks:
Jones, Chipper 126
Strikeouts:
Boone, Bret 112
Batting average:
Jones, Chipper .319
Slugging average:
Jones, Chipper .633

Enter command (c, p, t, or q): p
Enter player name, last name first: Jones, A

Name: Jones, Andruw
Games played: 162
At bats: 592
Runs scored: 97
Hits: 163
Doubles: 35
Triples: 5
Home runs: 26
Runs batted in: 84
Walks: 76
Strikeouts: 103
Batting average: .275
Slugging average: .483

Enter command (c, p, t, or q): t

At bats: 5569
Runs scored: 840
Hits: 1481
Doubles: 309
Triples: 23
Home runs: 197
Runs batted in: 791
Walks: 608
Strikeouts: 962
Batting average: .266
Slugging average: .436

Enter command (c, p, t, or q): foo
Command was not recognized; please try again.

Enter command (c, p, t, or q): q

The program begins by displaying a menu of commands. The user may then enter any number of commands in any order. The program terminates when the user enters the q command.

The c command causes the program to display the leading player(s) in each of several categories. If more than one player is tied for the lead in a particular category, the program must display all players who are tied, with one player per line. In order for a player to have the highest batting average or slugging average, he must have at least 100 at bats.

The p command displays statistics for all players who match the name entered by the user. Partial matches are allowed. For example, if the user enters J as the name, the program will display statistics for all players whose last names begin with J. Case is ignored when determining whether names match; entering Jones as the name is the same as entering JONES or jones.

The t command causes the program to display statistics for the entire team. The numbers shown for at bats, runs scored, hits, doubles, triples, home runs, runs batted in, walks, and strikeouts are computed by adding the numbers for the individual players. The overall batting and slugging averages are computed from the totals for at bats, hits, doubles, triples, and home runs.

If the user enters any command other than c, p, t, or q, the program will display the message

Command was not recognized; please try again.

and ignore the command. Commands may be either lower case or upper case.

The user may enter any number of spaces before and after each input. Batting averages and slugging averages must be rounded to three decimal places and displayed without a leading zero.
Data Structures and Algorithms

The program will store the data in parallel arrays. One array will store the player names. Each of the other arrays will store one set of numbers, with the number for a particular player always stored at the same index. The values to be stored in the arrays are shown at the end of this assignment.

To compute the batting average and slugging average, use the following formulas:

batting average = (number of hits) / (number of at bats)
slugging average = ((number of singles) + (number of doubles) * 2 + (number of triples) * 3 + (number of home runs) * 4) / (number of at bats)

where

number of singles = (number of hits) - (number of doubles) - (number of triples) - (number of home runs)
Hints

1. Develop the program gradually. Start by developing code to implement the menu structure. Then add the commands one by one. For each command, start by having it handle just one statistic.

2. Use the startsWith method to determine whether a player's name begins with the characters entered by the user.

3. To display a batting average or slugging average without a leading zero, first convert the number to string form, then use the substring method to remove the first character.
Player names

"Battle, Howard", "Lombard, George", "Speier, Justin",
"Jones, Chipper", "Lopez, Javy", "Simon, Randall",
"Klesko, Ryan", "Jordan, Brian", "Jones, Andruw",
"Williams, Gerald", "Smoltz, John", "Hernandez, Jose",
"Myers, Greg", "Lockhart, Keith", "Boone, Bret",
"Perez, Eddie", "Hunter, Brian", "Guillen, Ozzie",
"Garcia, Freddy", "Weiss, Walt", "Nixon, Otis",
"Fabregas, Jorge", "Maddux, Greg", "Millwood, Kevin",
"Glavine, Tom", "Perez, Odalis", "Matos, Pascual",
"Bergman, Sean", "Mulholland, Terry", "Chen, Bruce",
"DeRosa, Mark", "Ebert, Derrin", "Hudek, John",
"McGlinchy, Kevin", "Remlinger, Mike", "Seanez, Rudy",
"Bowie, Micah", "Cather, Mike", "Cortes, David",
"Rocker, John", "Springer, Russ", "Stull, Everett",
"Winkelsas, Joe", "Wohlers, Mark"

Games played

15, 6, 18, 157, 65, 90, 133, 153, 162, 143, 29, 48,
34, 108, 152, 104, 114, 92, 2, 110, 84, 6, 31, 31,
33, 17, 6, 6, 16, 15, 7, 5, 12, 61, 70, 52,
3, 4, 4, 69, 46, 1, 1, 2

At bats

17, 6, 3, 567, 246, 218, 404, 576, 592, 422, 62, 166,
72, 161, 608, 309, 181, 232, 2, 279, 151, 8, 64, 78,
65, 30, 8, 0, 16, 11, 8, 1, 1, 2, 2, 1,
0, 0, 0, 0, 0, 0, 0, 0

Runs scored

2, 1, 0, 116, 34, 26, 55, 100, 97, 76, 11, 22,
10, 20, 102, 30, 28, 21, 1, 38, 31, 0, 7, 4,
3, 1, 0, 0, 2, 0, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0

Hits

6, 2, 1, 181, 78, 69, 120, 163, 163, 116, 17, 42,
16, 42, 153, 77, 45, 56, 1, 63, 31, 0, 11, 12,
9, 4, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0

Doubles

0, 0, 0, 41, 18, 16, 28, 28, 35, 24, 4, 8,
2, 3, 38, 17, 12, 16, 0, 13, 2, 0, 1, 2,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0

Triples

0, 0, 0, 1, 1, 0, 2, 4, 5, 1, 0, 0,
0, 1, 1, 0, 1, 0, 0, 4, 1, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0

Home runs

1, 0, 0, 45, 11, 5, 21, 23, 26, 17, 1, 4,
2, 1, 20, 7, 6, 1, 1, 2, 0, 0, 2, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0

Runs batted in

5, 0, 0, 110, 45, 25, 80, 115, 84, 68, 7, 19,
9, 21, 63, 30, 30, 20, 1, 29, 8, 0, 7, 6,
4, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0

Walks

2, 1, 0, 126, 20, 17, 53, 51, 76, 33, 5, 12,
13, 19, 47, 17, 31, 15, 1, 35, 23, 0, 1, 2,
5, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0

Strikeouts

3, 2, 2, 94, 41, 25, 69, 81, 103, 67, 28, 44,
16, 21, 112, 40, 40, 17, 1, 48, 15, 0, 18, 29,
17, 10, 1, 0, 4, 6, 2, 1, 1, 1, 2, 1,
0, 0, 0, 0, 0, 0, 0, 0

Explanation / Answer

please rate - thanks

figured out the problem I messaged you about

couldn't figure this out

3. To display a batting average or slugging average without a leading zero, first convert the number to string form, then use the substring method to remove the first character.

so did it differently --hope thats ok

import java.util.*;
import java.text.*;
public class BravesStats
{public static String name[]={"Battle, Howard", "Lombard, George", "Speier, Justin",
"Jones, Chipper", "Lopez, Javy", "Simon, Randall",
"Klesko, Ryan", "Jordan, Brian", "Jones, Andruw",
"Williams, Gerald", "Smoltz, John", "Hernandez, Jose",
"Myers, Greg", "Lockhart, Keith", "Boone, Bret",
"Perez, Eddie", "Hunter, Brian", "Guillen, Ozzie",
"Garcia, Freddy", "Weiss, Walt", "Nixon, Otis",
"Fabregas, Jorge", "Maddux, Greg", "Millwood, Kevin",
"Glavine, Tom", "Perez, Odalis", "Matos, Pascual",
"Bergman, Sean", "Mulholland, Terry", "Chen, Bruce",
"DeRosa, Mark", "Ebert, Derrin", "Hudek, John",
"McGlinchy, Kevin", "Remlinger, Mike", "Seanez, Rudy",
"Bowie, Micah", "Cather, Mike", "Cortes, David",
"Rocker, John", "Springer, Russ", "Stull, Everett",
"Winkelsas, Joe", "Wohlers, Mark"};
public static int played[]={15, 6, 18, 157, 65, 90, 133, 153, 162, 143, 29, 48,
34, 108, 152, 104, 114, 92, 2, 110, 84, 6, 31, 31,
33, 17, 6, 6, 16, 15, 7, 5, 12, 61, 70, 52,
3, 4, 4, 69, 46, 1, 1, 2 };
public static int atBat[]={17, 6, 3, 567, 246, 218, 404, 576, 592, 422, 62, 166,
72, 161, 608, 309, 181, 232, 2, 279, 151, 8, 64, 78,
65, 30, 8, 0, 16, 11, 8, 1, 1, 2, 2, 1,
0, 0, 0, 0, 0, 0, 0, 0};
public static int scored[]={2, 1, 0, 116, 34, 26, 55, 100, 97, 76, 11, 22,
10, 20, 102, 30, 28, 21, 1, 38, 31, 0, 7, 4,
3, 1, 0, 0, 2, 0, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0};
public static int hits[]={6, 2, 1, 181, 78, 69, 120, 163, 163, 116, 17, 42,
16, 42, 153, 77, 45, 56, 1, 63, 31, 0, 11, 12,
9, 4, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0};
public static int doubles[]={0, 0, 0, 41, 18, 16, 28, 28, 35, 24, 4, 8,
2, 3, 38, 17, 12, 16, 0, 13, 2, 0, 1, 2,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0};
public static int triples[]={0, 0, 0, 1, 1, 0, 2, 4, 5, 1, 0, 0,
0, 1, 1, 0, 1, 0, 0, 4, 1, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0};
public static int hr[]={1, 0, 0, 45, 11, 5, 21, 23, 26, 17, 1, 4,
2, 1, 20, 7, 6, 1, 1, 2, 0, 0, 2, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0};
public static int rbi[]={5, 0, 0, 110, 45, 25, 80, 115, 84, 68, 7, 19,
9, 21, 63, 30, 30, 20, 1, 29, 8, 0, 7, 6,
4, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0};
public static int walks[]={2, 1, 0, 126, 20, 17, 53, 51, 76, 33, 5, 12,
13, 19, 47, 17, 31, 15, 1, 35, 23, 0, 1, 2,
5, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0};
public static int k[]={3, 2, 2, 94, 41, 25, 69, 81, 103, 67, 28, 44,
16, 21, 112, 40, 40, 17, 1, 48, 15, 0, 18, 29,
17, 10, 1, 0, 4, 6, 2, 1, 1, 1, 2, 1,
0, 0, 0, 0, 0, 0, 0, 0 };
public static void main(String [] args)
{Scanner in=new Scanner(System.in);
char choice;
choice=menu(in);
while(choice!='Q')
{switch(choice)
    {case 'C': catLeaders(); break;
    case 'P': player(in); break;
    case 'T': totals(); break;
    }
choice=menu(in);
}
}
public static void totals()
{System.out.print("At bats: ");
tot(atBat);
System.out.print("Runs scored: ");
tot(scored);
System.out.print("Hits: ");
tot(hits);
System.out.print("Doubles: ");
tot(doubles);
System.out.print("Triples: ");
tot(triples);
System.out.print("Home Runs: ");
tot(hr);
System.out.print("Runs Batted in: ");
tot(rbi);
System.out.print("Walks: ");
tot(walks);
System.out.print("Strikeouts: ");
tot(k);
System.out.print("Batting Average: ");
totBA();
System.out.print("Slugging Average: ");
totSlug();
}
public static void catLeaders()
{
System.out.println("At bats:");
max(atBat);
System.out.println("Runs scored:");
max(scored);
System.out.println("Hits:");
max(hits);
System.out.println("Doubles:");
max(doubles);
System.out.println("Triples:");
max(triples);
System.out.println("Home Runs:");
max(hr);
System.out.println("Runs Batted in:");
max(rbi);
System.out.println("Walks:");
max(walks);
System.out.println("Strikeouts:");
max(k);
System.out.println("Batting Average:");
maxBA();
System.out.println("Slugging Average:");
maxSlug();
}
public static void totBA()
{int i,h=0,bats=0;
double sum=0;
DecimalFormat df = new DecimalFormat(".000");
for(i=0;i<name.length;i++)
         {h+=hits[i];
           bats+=atBat[i];
            }
         sum+=(double)h/bats;
    
    System.out.println(df.format(sum));
}
public static void totSlug()
{int i,h=0,d=0,t=0,f=0,bats=0;
double sum=0;
DecimalFormat df = new DecimalFormat(".000");
for(i=0;i<name.length;i++)
         {h+=hits[i];
           d+=doubles[i];
           t+=triples[i];
           f+=hr[i];
           bats+=atBat[i];
            }

sum=((h-d-t-f)+(d*2)+(t*3)+(f*4))/(double)bats;
   System.out.println(df.format(sum));
}

public static void maxBA()
{int i,index;
DecimalFormat df = new DecimalFormat(".000");
double [] b=new double[name.length];
for(i=0;i<b.length;i++)
     {if(atBat[i]>=100)
          b[i]=(double)hits[i]/atBat[i];
    else
          b[i]=0;
    }
index=0;
for(i=1;i<b.length;i++)
     if(b[i]>b[index])
           index=i;
for(i=0;i<b.length;i++)
    if(b[i]==b[index])
          System.out.println(name[i]+" "+df.format(b[i]));
}
public static void maxSlug()
{int i,index;
DecimalFormat df = new DecimalFormat(".000");
double [] b=new double[name.length];
for(i=0;i<b.length;i++)
   if(atBat[i]>=100)
     b[i]=((hits[i]-doubles[i]-triples[i]-hr[i])+(doubles[i]*2)+(triples[i]*3)+(hr[i]*4))/(double)atBat[i];
   else
    b[i]=0;
index=0;
for(i=1;i<b.length;i++)
     if(b[i]>b[index])
           index=i;
for(i=0;i<b.length;i++)
    if(b[i]==b[index])
          System.out.println(name[i]+" "+df.format(b[i]));
}
public static void tot(int a[])
{int i,sum=0;
for(i=0;i<a.length;i++)
    sum+=a[i];
          System.out.println(sum);
}


public static void max(int a[])
{int i,index;
index=0;
for(i=1;i<a.length;i++)
     if(a[i]>a[index])
           index=i;
for(i=0;i<a.length;i++)
    if(a[i]==a[index])
          System.out.println(name[i]+" "+a[i]);
}
public static void player(Scanner in)
{String n,m;
DecimalFormat df = new DecimalFormat(".000");
int letters,i;
in.nextLine();
System.out.println("Enter player name, last name first:");
n=in.nextLine();
letters=n.length();
for(i=0;i<name.length;i++)
    {m=name[i].substring(0,letters);
    if(m.compareToIgnoreCase(n)==0)
         {System.out.println(" Name: "+name[i]);
        System.out.println("Games played: "+played[i]);
        System.out.println("At bats: "+atBat[i]);
        System.out.println("Runs scored: "+scored[i]);
        System.out.println("Hits: "+hits[i]);
        System.out.println("Doubles: "+doubles[i]);
        System.out.println("Triples: "+triples[i]);
        System.out.println("Home Runs: "+hr[i]);
        System.out.println("Runs Batted in: "+rbi[i]);
        System.out.println("Walks: "+walks[i]);
        System.out.println("Strikeouts: "+k[i]);
        System.out.println("Batting Average: "+df.format(hits[i]/(double)atBat[i]));
        System.out.println("Slugging Average: "+df.format(((hits[i]-doubles[i]-triples[i]-hr[i])+(doubles[i]*2)+(triples[i]*3)+(hr[i]*4))/(double)atBat[i]));
        }
    }
}
public static char menu(Scanner in)
{char choice;
System.out.println("--------------------------------------------");
System.out.println("|Commands: c - Show category leaders       |");
System.out.println("|          p - Show stats for a player     |");
System.out.println("|          t - Show totals for all players |");
System.out.println("|          q - Quit                        |");
System.out.println("--------------------------------------------");
do
{System.out.println("Enter command (c, p, t, or q): ");
choice=Character.toUpperCase(in.next().trim().charAt(0));
if(choice=='P'||choice=='C'||choice=='T'||choice=='Q')
        return choice;
System.out.println("Command was not recognized; please try again.");
}while(true);
}
}