Question

In: Computer Science

3.1 Write code that creates an ArrayList object named list and fills list with these numbers...

3.1 Write code that creates an ArrayList object named list and fills list with these numbers (using one or a pair of for or while loops): 0 1 2 3 4 0 1 2 3 4

3.2 Consider the ArrayList object named list containing these Integers: list = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 } What are the contents of list after this loop completes? for (int i = 1; i < 10; ++i) { list.set(i, list.get(i) + list.get(i-1)); }

3.3 Write an enhanced for loop that counts how many numbers in an ArrayList object named list are negative. Print the count after the loop terminates.

3.4 Write a method named arrayListSum() that has one parameter pList which is an object of the class ArrayList . The method shall return the sum of the elements of pList.

3.5 Write a method named named arrayListCreate() that has two int parameters pLen and pInitValue. The method shall create and return a new ArrayList object which has exactly pLen elements. Each element shall be initialized to pInitValue.

3.6 Write a void method named insertName() that has two input parameters: (1) pList which is an object of ArrayList link in course schedule where each element of pList is a person's name; and (2) a String object named pName. Assume the names in pList are sorted into ascending order. The method shall insert pName into pList such that the sort order is maintained.

3.7 Write a void method named arrayListRemove() which has two parameters: pList is an object of the ArrayList class and pValue is an int. The method shall remove all elements from pList that are equal to pValue.

Solutions

Expert Solution

Hi I have aswered first 4 parts. Please repost other parts in separate post.

Please let me know in case of any issue.

3.1)

// creating array list object

       ArrayList<Integer> list = new ArrayList<>();

      

       for(int i=1; i<=2; i++){

           for(int j=0; j<5; j++)

               list.add(j);

       }

3.2)


list = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 }

for (int i = 1; i < 10; ++i) {

   list.set(i, list.get(i) + list.get(i-1));
}

content of list after for loop execution:

    list = { 1, 3, 6, 10, 15, 19, 22, 24, 25, 25 }

3.3)

// creating array list object

       ArrayList<Integer> list = new ArrayList<>();

      

       int count = 0;

       for(int x : list){

           if(x < 0)

               count++;

       }

      

       System.out.println(count);

3.4)

public int arrayListSum(ArrayList<Integer> pList){

      

       int sum = 0;

      

       for(int x: pList)

           sum = sum + x;

      

       return sum;

   }


Related Solutions

CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a...
CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a price of $50.99, prints the name and price of the product, reduces its price by 4.0, and then prints its price again. public class Product { private String name; private double price; public Product(String productName, double productPrice) { name = productName; price = productPrice; } public String getName() { return name; } public double getPrice() { return price; } public void reducePrice(double amount) {...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date object, a ClockWithAudio object, a BMI object, a Day object, and a FigurePane object. Then display all elements in the list. Assume that all classes (i.e. Date, Account, etc.) have their own no-argument constructor.
Given the list values = [], write code that fills the list with each set of...
Given the list values = [], write code that fills the list with each set of numbers below. Note: use loops if it is necessary 1 2 3 4 5 6 7 8 9 10 0 2 4 6 8 10 12 14 16 18 20 1 4 9 16 25 36 49 64 81 100 0 0 0 0 0 0 0 0 0 0 1 4 9 16 9 7 4 9 11 0 1 0 1 0...
Write a statement that creates a list with the following string numbers:
Write a statement that creates a list with the following string numbers:      ‘9’, ‘5’, ‘12’, ‘31’, ‘4’, and ‘8’.Assume listDATA references this data list. Write a for-loop that displays each element of this list and the total of all these numbersin python programming
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
SQL Code: Write a script that creates and calls a stored procedure named test. This procedure...
SQL Code: Write a script that creates and calls a stored procedure named test. This procedure should identify all of the prime numbers less than 100. (A prime number is an integer that can't be divided by another integer other than 1 and itself.) Then, it should display a string variable that includes the prime numbers like this: 2 1 3 1 5 1 7 1 1 1 1 1 3 1 1 7 1 1 9 1 2 3...
Please write in Python code please Write a program that creates a dictionary containing course numbers...
Please write in Python code please Write a program that creates a dictionary containing course numbers and the room numbers of the rooms where the courses meet. The dictionary should have the following key-value pairs: Course Number (key) Room Number (value) CS101 3004 CS102 4501 CS103 6755 NT110 1244 CM241 1411 The program should also create a dictionary containing course numbers and the names of the instructors that teach each course. The dictionary should have the following key-value pairs: Course...
1. Write a function named “Number” that reads in a list of numbers until the user...
1. Write a function named “Number” that reads in a list of numbers until the user enters 0. It will return true if the user has entered more even numbers than odd numbers; otherwise it returns false. 2. Write a code segment to sort an array of students in ascending order of their IDs. Assume the array has been filled with names and assume the following declaration: struct Student { string name; int ID; } Student roster[30]; // Code to...
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");           &
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");             list.add("Sauer");             list.add("Maynard");             list.add("Namath");             list.add("Boozer");             list.add("Snell");             list.add("Namath");             list.add("Atkinson");             list.add("Lammonds");             list.add("Dockery");             list.add("Darnold");             list.remove(2);             list.set(2, "Brady");             list.remove(2);             list.set(4,"Unitas");             list.add(1,"Lamomica");             list.add(3,"Hanratty");             list.remove("Namath");             list.remove(list.size()-1);             list.remove(2);             list.set(7, "Riggins");             Iterator iter = list.iterator();           while (iter.hasNext())         {   System.out.print(iter.next() + " ");                         }                     } }
rite the code for a class named Funnel. A funnel is a cone-shaped object that is...
rite the code for a class named Funnel. A funnel is a cone-shaped object that is used to pour liquids into small openings. For the purpose of this question, you may assume that a Funnel is an inverted cone (see image at left) and has a flap (a lid) at the bottom that can be opened and closed. A Funnel has the following PUBLIC features: a) A Funnel created without any data has a radius of 12, a height of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT