Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

To do: add code to Ex5BaseballPlayers to complete the comments in the code. import java.util.ArrayList; public class Ex5BaseballPlayers { //----------------------------------------------------------------- // Stores and modifies a

To do: add code to Ex5BaseballPlayers to complete the comments in the code.

import java.util.ArrayList;

public class Ex5BaseballPlayers

{

//-----------------------------------------------------------------

// Stores and modifies a list of baseball players.

//-----------------------------------------------------------------

// NOte: We could (and probably should) create a Player blueprint

// and use that. But in this case, I just want an array list

// of Strings.

public static void main (String[] args)

{

// always make your array lists generic

ArrayList players = new ArrayList();

players.add("Albert Pujols");

players.add ("Aaron Judge");

players.add ("Paul Goldschmidt");

players.add ("Mike Trout");

players.add ("Anthony Rizzo");

players.add ("Mike Trout"); // yes - I know he is here multiple times but I need that for later

players.add("Francisco Lindor");

players.add("Steven Strasburg");

players.add("Jose Altuve");

players.add("Bryce Harper");

players.add ("Mike Trout");

players.add("George Springer");

players.add("Max Scherzer");

players.add ("Madison Bumgardner");

players.add ("Jason Kipnis");

// You could use the following to print out all of the players

// since Java will call the toString() on each individual item if you do not

// but it is better to do it explicitly yourself

//System.out.println (players);

System.out.println("The baseball players are: ");

for (int i=0;i

System.out.println(players.get(i).toString());

// Note the line above---if this was anything but Strings in the array list

// it would work the same way but I would explicitly call the toString() method.

// Now let's remove Anthony Rizzo

int location = players.indexOf ("Anthony Rizzo");

players.remove(location);

// could do it in one step: players.remove (players.indexOf ("Anthony Rizzo"));

// Prove that it worked by printing the information out

System.out.println(" Without Anthony:");

for (int i=0;i

System.out.println(players.get(i));

// Find out who is the third one on the list (remember that we start counting at zero

System.out.println (" The third player in the list now is " + players.get(2));

// Add in Zack Greinke as the fourth player

players.add (3, "Zack Greinke");

//Print these out

System.out.println(" With Zack added as the fourth player we now have:");

for (int i=0;i

System.out.println(players.get(i));

// print out how many players there are

System.out.println(" The number of players is now "+ players.size());

//YOU DO THE FOLLOWING. FIND METHODS FOR THESE!!

// check to see if Adrian Gonzalez is on the player list. It should print true or false.

// find out the index for the location of Jose Altuve

// find the indexes for Mike Trout. There is no single method to do this.

// Loop through the array list. Use the method .equals to compare the entry to "Mike Trout"

// print out the index number if it is equal.

// NOTE: For Strings you cannot use == (this can only be used for primitive data types)

// for objects == checks to see if it is the same object ---not that the contents are the same

// find all of the players whose first names start with the letter J.

// Print these. HINT!!! There is

// no single method for this. You will need to loop through the ArrayList getting

// each entry and checking to see if the string starts with an J.

// find a way to print out the list of the LAST names of the players. (Hint: I would use substrings or the String split method)

// print out the list of all of the players but all of the letters in all of the names

// need to be capitalized.

// clear out all the members of the ArrayList

// print out the ArrayList now and the size now.

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server T-SQL Recipes

Authors: David Dye, Jason Brimhall

4th Edition

1484200616, 9781484200612

More Books

Students also viewed these Databases questions