Question

In: Computer Science

Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...

Problem 1

  • 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.

Problem 2

  • 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.

Problem 3

  • 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.

Problem 4

  • 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!

Problem 5

  • 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.

Problem 6

  • 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

Java code:

import java.util.ArrayList;

public class ListORama {
    public static void makeLists() {
        // Problem 1
        ArrayList<String> avengers = new ArrayList<String>();

        // Problem 2
        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);
        for (String avenger : avengers)
            System.out.println(avenger);

        // Problem 3
        System.out.println();
        System.out.println("Current size of avengers: " + avengers.size());
        for (int i = 0; i < avengers.size(); i++)
            System.out.println(avengers.get(i));

        // Problem 4
        avengers.remove("Clark");
        avengers.remove("Gwyneth");
        System.out.println("Now the size is: " + avengers.size());
        System.out.println(avengers);

        // Problem 5
        avengers.add("Chris H.");
        avengers.set(0, "Chris E.");
        avengers.add(2, "Loki");
        System.out.println("Now the size is: " + avengers.size());
        System.out.println(avengers);

        // Problem 6
        for (int i = 0; i < avengers.size(); i++) {
            if (i == avengers.size() - 1)
                System.out.println(avengers.get(i));
            else
                System.out.print(avengers.get(i) + ", ");
        }
    }
}

Output:

Kindly rate the answer and for any help just drop a comment


Related Solutions

1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as...
In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as you go Copy the code for LineSegment given below into the class. Take a few minutes to understand what the class does. Besides the mutators and accessors, it has a method called print, that prints the end points of the line segment in the form: (x, y) to (x, y) You will have to write two methods in the LineSegment class. There is information...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include the following three static methods described below. However, don't implement them right away. Instead, start by returning the default value followed by a // TODO comment. public static int sumOfDigits(int n) This method returns the sum of all the digits. sumOfDigits(-34) -> 7 sumOfDigits(1038) -> 12 public static int countSmiles(char[] letters, int index) This method counts the number of colons followed by a closing...
Create a new project ‘Clocky’ - Create a ‘ClockAlarm’ class Include 1 member – a List...
Create a new project ‘Clocky’ - Create a ‘ClockAlarm’ class Include 1 member – a List alarmTimes Create an accessor and mutator Create a method addAlarmTime which adds an alarm time to your list Create a method deleteAlarmTIme which removes an alarm time from your list Create a method – displayAlarmTimes which prints all alarm times in the list - Create a ‘Clock’ class - include at least 2 members - one member of Clock class needs to be private...
Create a project called rise. Add a class called GCD. Complete a program that reads in...
Create a project called rise. Add a class called GCD. Complete a program that reads in a numerator (as an int) and a denominator (again, as an int), computes and outputs the GCD, and then outputs the fraction in lowest terms. Write your program so that your output looks as close to the following sample output as possible. In this sample, the user entered 42 and 56, shown in green. Enter the numerator: 42 Enter the denominator: 56 The GCD...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT