Question

In: Computer Science

Make sure you are using the appropriate indentation and styling conventions Open the online API documentation...

Make sure you are using the appropriate indentation and styling conventions

Open the online API documentation for the ArrayList class, or the Rephactor Lists topic (or both). You may want to refer to these as you move forward.

Exercise 1 (15 Points)

  • In BlueJ, create a new project called Lab7
  • Create a class in that project called ListORama
  • Write a static method in that class called makeLists that takes no parameters and returns no value
  • In makeLists, create an ArrayList object called avengers that can hold String objects.

Exercise 2 (15 Points)

  • Add the following names to the avengers list, one at a time:

Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark

  • Print the avengers object. You will notice that the contents are displayed in between two brackets [] and separated by a comma. This is how the toString() method of the ArrayList class is implemented!
  • Then use a for-each loop to print each person in the list, one per line.

Exercise 3 (15 Points)

  • Print a blank line.
  • Print the current size of the ArrayList:

Current size of avengers: xxx

  • Now use a regular for loop to print the elements of the list, one per line.

Exercise 4 (15 Points)

  • Now remove the "Clark" and "Gwyneth" elements from the ArrayList. Remember, the list "closes the gaps" when something is removed.
  • Print the current size again.

Now the size is: xxx

  • Print the entire list object again.
  • Make sure you removed the right people!

Exercise 5 (20 Points)

  • Add "Chris H." to the end of the ArrayList.
  • Change the first element "Chris" to "Chris E."
  • Insert "Loki" between "Robert" and "Scarlett"
  • Print the size of the ArrayList again.
  • Print the entire list again.

Exercise 6 (20 Points)

  • Print the elements of the ArrayList again, this time on one line separated by commas.
  • Don't print a comma after the last name!
  • To do this, use a regular for loop and the ArrayList get method to get each name to print.
  • Use an if statement to decide whether to print a comma after each name

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.List;

public class ListORama {
        private static List<String> avengers;
        public static void makeLists(){
                avengers=new ArrayList<String>();
        }
        public static void main(String[] args) {
                makeLists();
                avengers.add("Chris");
                avengers.add("Robert");
                avengers.add("Scarlett");
                avengers.add("Clark");
                avengers.add("Jeremy");
                avengers.add("Gwyneth");
                avengers.add("Mark");
                System.out.println(avengers.toString());
                for (String avenger : avengers) {
                        System.out.println(avenger);
                }
                System.out.println();
                System.out.println("Now the size is:"+avengers.size());
                for (int i = 0; i < avengers.size(); i++) {
                        String string = avengers.get(i);
                        System.out.println(string);
                        
                }
                avengers.remove("Clark");
                avengers.remove("Gwyneth");
                System.out.println("Now the size is:"+avengers.size());
                System.out.println(avengers.toString());
                avengers.add("Chris H");
                avengers.set(0, "Chris E.");
                avengers.add(avengers.indexOf("Robert")+1, "Loki");
                System.out.println("Now the size is:"+avengers.size());
                System.out.println(avengers.toString());
                for (int i = 0; i < avengers.size(); i++) {
                        String string = avengers.get(i);
                        if(i<avengers.size()-1)
                                System.out.print(string+",");
                        else
                                System.out.print(string);
                        
                }
                
        }
}


Related Solutions

You have decided to open a business. What ownership method will you use? Please make sure...
You have decided to open a business. What ownership method will you use? Please make sure to include three points to support your decision.
Make a menu for a restaurant using python. Make sure there are choices on the menu....
Make a menu for a restaurant using python. Make sure there are choices on the menu. Give me a little description of how you did it.
(For entries with a​ $0 balance, make sure to enter​ "0" in the appropriate cell. Round...
(For entries with a​ $0 balance, make sure to enter​ "0" in the appropriate cell. Round the contribution margin percentage to the nearest whole​ percent.) Variable Fixed Total Operating Contribution Case Revenues Costs Costs Costs Income Margin Percentage. Please fill in all the missing data where the question marks are. Revenues Variable Cost Fixed Cost Total Costs Operating Income Contribution Margin Percentage ? $200 ? $700 $1,900 ? % $2,500 ? $500 ? $700 ?% $1,300 $800 ? $1,300 ?...
Explain to a friend what you have learned about documentation and evidence. Be sure to detail...
Explain to a friend what you have learned about documentation and evidence. Be sure to detail why we need it and how we get it. Do not get too technical; they do not understand accounting or auditing. You may use a combination of formal terms, examples, and resources to support your answer.
Describe, using equations, if appropriate, the compromises you will make when you change the aperture half...
Describe, using equations, if appropriate, the compromises you will make when you change the aperture half angle in an SEM. You should discuss the signal intensity, the depth of field, aberrations and the resolution.
Rewrite the following notes using the appropriate language for nursing documentation. Please Correct only where necessary....
Rewrite the following notes using the appropriate language for nursing documentation. Please Correct only where necessary. a. D.K. is a 56 y/o African American male with a h/o HTN, high cholesterol, and arthritis; D.K is 5'10" and 300 pounds; pt. denies CP, SOB; pt. does not care about the risks of heart disease - he eats whatever he wants including fatty and salty foods; pt. told that he has to eat a low sodium low cholesterol diet if he doesn't...
For this project you will estimate parameters using confidence intervals. For each confidence interval, make sure...
For this project you will estimate parameters using confidence intervals. For each confidence interval, make sure you report   The confidence interval limits rounded as directed (Use StatCrunch to find all confidence interval limits. Use Stat>Proportion Stats to find confidence interval limits for proportions. Use Stat>T Stats to find confidence interval limits for means.) Refer to the resources on the course home page for more information about confidence intervals and how to find them using StatCrunch. An interpretation of the confidence...
Please show steps in the calculation. Please make sure you include checking conditions for using the...
Please show steps in the calculation. Please make sure you include checking conditions for using the CLT. Only Answer Question 3 and 4 The Human Resources (HR) Department of a certain college has asked all employees who were employed in 2018 to fill out a survey in December 2018. Three items on the survey were: “Your dental expense in 2018”, “Are you in a family with at least three other family members?”, and “Your medical expenses in 2018”. The manager...
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
Please show steps in the calculation. Please make sure you include checking conditions for using the...
Please show steps in the calculation. Please make sure you include checking conditions for using the CLT. The Human Resources (HR) Department of a certain college has asked all employees who were employed in 2018 to fill out a survey in December 2018. Three items on the survey were: “Your dental expense in 2018”, “Are you in a family with at least three other family members?”, and “Your medical expenses in 2018”. The manager of HR has randomly selected a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT