Question

In: Computer Science

For each of the following write the line(s) of code that: Declares and initializes (creates) an...

For each of the following write the line(s) of code that:

  1. Declares and initializes (creates) an ArrayList that holds String objects

  2. Adds to your ArrayList the words "Too" and "Fun"

  3. Verifies that your ArrayList now contains 2 elements

  4. Sets the second String in the ArrayList to "No"

  5. Verifies that your ArrayList still contains exactly 2 elements

  6. Prints the contents of the ArrayList to the screen in the following format:

    <element>, <element>, . . . , <element>

Solutions

Expert Solution

The programming language used is python 3.8

1)Declaring the ArrayLIst:

The code in python : ArrayList = []

2)Adding string values to ArrayList:

The code in python:

ArrayList.append("Too")
ArrayList.append("Fun")

3)Verifying ArrayList has 2 elements:

This is done by printing the values in ArrayList.The python code is:

for value in ArrayList:
    print(value,end=" ")

4)Setting the second string in ArrayList to "No"

The index of second string in ArrayList will be 1.

So the python code is:

ArrayList[1]="No"

5)Verifying ArrayList has 2 elements:

This is done by printing the values in ArrayList.The python code is:

for value in ArrayList:
    print(value,end=" ")

6)Printing of the contents in ArrayList can be done using the code:

for value in ArrayList:
    print(value,end=" ")

The python code snippet is provided below:

ArrayList=[]
ArrayList.append("Too")
ArrayList.append("Fun")
for value in ArrayList:
    print(value,end=" ") #To verify there are 2 contents in array list
ArrayList[1]="No"
print("")
for value in ArrayList:
    print(value,end=" ") #To verify there are 2 contents in array list

print(" ")
#To print the contents
for value in ArrayList:
    print(value,end=" ")

The screenshot of the code and output generated is attached for your reference:

Hope it helps!!!


Related Solutions

Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant...
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant float variable NJSALES_TAX; declares and initializes to 1000 an integer variable total; declares and initializes to zero a float variable grand_total; prompt and input on new line total; calculate grand_total to equal total plus (total*NJSALES_TAX); if grand_total <= 1000 print on new line “Grand total is less than or equal to 1000 it is $” and the grand_total to two decimal places; else if...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and...
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and produces as output the sum of integers in an array, the largest and the smallest element in an array. Your program should contain the following methods: public static int sum(int[]) public static int findLargest(int[]) public static int findSmallest(int[])
Write a C program (using Code::blocks) that outputs a formatted header line and creates 10 children...
Write a C program (using Code::blocks) that outputs a formatted header line and creates 10 children processes each of which displays a line of the output as shown below. Notice the values of Child no and x, they have to be the same as shown here while the processes PIDs values are based on the what your system assigns to these processes. Child    PID        PPID      X 0           13654    13653    5 1           13655    13653    10 2           13656    13653    15 3           13657   ...
write the code that declares an array of booleans, called myarray, with size of 40 elements
write the code that declares an array of booleans, called myarray, with size of 40 elements
Write a C program that outputs a formatted header line and creates 10 children processes each...
Write a C program that outputs a formatted header line and creates 10 children processes each of which displays a line of the output as shown below. Notice the values of Child no and x, they have to be the same as shown here while the processes PIDs values are based on the what your system assigns to these processes. Child    PID        PPID      X 0           13654    13653    5 1           13655    13653    10 2           13656    13653    15 3           13657    13653    20...
C Code Please! Declares a constant variable N and sets it to 42. * Declares an...
C Code Please! Declares a constant variable N and sets it to 42. * Declares an array of integers, with N elements, called fibonacci. * Declares an array of double floating-point numbers, with N-1 elements, called ratio. * Fills the fibonacci array with the numbers in the Fibonacci sequence (Wikipedia is your friend if you don't know what this sequence is about but you want to know).    Into the i-th element of the fibonacci array, you need to put...
Write pseudo-code to solve the following problem using MapReduce and explain how it works. Each line...
Write pseudo-code to solve the following problem using MapReduce and explain how it works. Each line in the file lists a user ID, the ID of the movie the user watched, the rating the user gave for the movie, and the timestamp. For example line 1 indicates that the user’s ID is 196, the movie ID is 242, the user gave this movie a rating of 3, and the timestamp is 881250949. Given the file, find out the top similar...
What is the output of the code below? After each line that has a cout, write...
What is the output of the code below? After each line that has a cout, write what the output of the code is. See example in code below. int i = 2, j = 1, k = i + j; cout << k << endl; ➔ 3 double d = 2.99, e = 1, f = d + e; cout << f << endl; double q = 1.50; cout << q/3 << endl; cout << 6.0/4 << endl; char c...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a Singapore Airlines Loyalty program. You are asked to write an inheritance hierarchy discount system that benefits KrisFlyer members program to calculate their profit. A brief about KrisFlyer is that it is useful for those who fly on Singapore Airlines (its partners like Virgin Australia and Air New Zealand) frequently. KrisFlyer miles can be earned through credit cards, flying and bonus miles promotions. The miles...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT