Question

In: Computer Science

Could I please get explainations and working out as to how to get to the answers....

Could I please get explainations and working out as to how to get to the answers.

Note that  is where part of the answer goes.

1.

Consider the following ArrayList list:

ArrayList list = new ArrayList(Arrays.asList(10,70,20,90));

//list = [10, 70, 20, 90]

Complete the following statements so that list contains the items [50, 70, 20]

Do NOT include spaces in your answers.

list.remove(  ); list.  (  , 50);

2.

Assume there exists an ArrayList list that contains the items

[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, ....]

(So, we know the first item is 10, second item is 20, but we don't know how many items it contains. You may assume it contains at least 10 items)

Complete the following code that displays every other item starting at the fifth item (so, the pattern 50 70 90 ...)

Do NOT include spaces in your answer.

for(int i=  ; i <  ;  ) {

System.out.println(  +" ");

}

3.

Complete the following function that returns the number of negative (less than 0) items in ArrayList list.

public static int countNegatives(ArrayList list) {

int result =  ;

for(int i=0; i < list.size(); i++) {

if(    ) {  ; }

} return result; }

Solutions

Expert Solution

Ques 1)

There will be two remove statements to remove elements 10 and 90.

remove function takes the index of the element to remove. So the first remove statement will be as list.remove(0) to remove first element because list indexing starts from 0 and the second remove statement will be list.remove(list.size()-1) because list.size() returns the last index of element + 1.

The third statement to add 50 to list will be list.add(0, 50). This will add 50 as the first element in the list.

I'll add sample code for all question for your reference.

Sample Code:-

list.remove(0);
list.remove(list.size()-1);
list.add(0,50);

Ques 2)

To get the element of list from 5 element to every other element, a for loop has to be implemented iterating from the 5 element and incrementing the loop variable by 2 for every iteration.

5th element of array will have index as 4 since array indexing starts from 0.

So loop variable will start from 4 and will be incremented by 2 in every iteration. Hence the variable values will be as 4, 6, 8, 10 and so on.

Sample Code:-

for(int i=4;i<list.size();i+=2)
{
   System.out.println(list.get(i)+"");
}

Ques 3:-

Initialize the value of result as 0, since in the starting of function there are no negative values, unless we check whole array.

To count the number of negatives in the list we have to iterate over complete list and check if the value at that index of list in less than 0.

If the value is less than 0, then increment the count of result value.

Sample Code:-

public static int countNegatives(ArrayList<Integer> list){
int result = 0;
for(int i=0;i<list.size();i++)
{
   if(list.get(i)<0){
   result++;
   }
}
   return result;
}

Note:- Please comment down if you face any problem. :)


Related Solutions

Could I please get explainations and working out as to how to get to the answers....
Could I please get explainations and working out as to how to get to the answers. Note that  is where part of the answer goes. 1. Write a statement that removes the first occurrence of item 10 (if any) from an ArrayList of Integer objects, data. data.remove(  ); 2. Complete the following function that returns true if ArrayList list contains any positive (more than 0) item, false otherwise. public static  containsPositive(ArrayList list) { for(int i=0; i <...
Could I please get explainations and working out as to how to get to the answers....
Could I please get explainations and working out as to how to get to the answers. Note that  is where part of the answer goes. 1. Complete the following function that returns true if ArrayLists list1 and list2 are exactly the same in terms of contents (same items in same order), false otherwise. public static  sameSame(ArrayList<Integer> list1, ArrayList<Integer> list2) { if(list1 == null || list2 == null) return false; if(  !=  ) return false; for(int i=0;...
I am stumped on these problems and homework question, please could I get the answers to...
I am stumped on these problems and homework question, please could I get the answers to the questions below from an expert. Thank you 5). Suppose that two population proportions are being compared to test weather there is any difference between them. Assume that the test statistic has been calculated to be z= 2.21. Find the p-value for this situation?   a). p-value = 0.4864 b). p-value = 0.0272 c). p-value = 0.9728 d). p-value = 0.0136. 8). If you are...
Hi, I have the answers, but I don't understand how to get the answers. Please explain...
Hi, I have the answers, but I don't understand how to get the answers. Please explain thoroughly. Bob earns ($25,000) in passive losses from BHI partnership. He has an outside basis of $40,000 of which $30,000 comes from non-recourse debt, and he has passive income of $50,000. What are the tax consequences to Bob? $10,000 deductible loss What basis does Bob take in his partnership interest? $15,000 How much is Bob at-risk after the allocation? 0 How much, if any...
Working principle with explainations in roller Coster
Working principle with explainations in roller Coster
PART 2 Please answer these with true and false answers, but please give your explainations, use...
PART 2 Please answer these with true and false answers, but please give your explainations, use diagrams if necessary 5. Suppose the government funds the provision of a pure public good from tax revenue. The total burden to the economy of providing the good exceeds the amount spent on the good. 6. A decrease in posted (nominal) interest rates necessarily means a decrease in real interest rates. 7. At the end of 2016 the Canadian dollar exchange rate with the...
hello Could I get the answers for Question 5 here's the question: QUESTION5 You are the...
hello Could I get the answers for Question 5 here's the question: QUESTION5 You are the Chief Technology Officer of Vegas Girl's Pizza involved as part of the IT overnance team to provide your opinion and solutions for the following questions Consider the following additional paragraph s and answer the question that follows: Back at his desk, Peter Greyton is thinking of the day's developments. He reflects uporn his meeting with Jim Saxton and Elaine Black. He considers where the...
Business Analytics Could I get the answer on a downloadable excel sheet please? if i cannot...
Business Analytics Could I get the answer on a downloadable excel sheet please? if i cannot get the downloadable file here on chegg: my email is [email protected] i can paypal $10 for the answer. Market Insights Co. (MIC) is a full-service market research company. MIC is being hired to interview registered voters in a district to gain insight into their opinions about certain issues. Each voter is to be interviewed in person. The costs of interviewing different types of voters...
I know the what the answers are but I don't know how to get them. Can...
I know the what the answers are but I don't know how to get them. Can you please explain the process? Thank you. Part VII. Discontinued Operations and Earnings per Share (11 points) Todd Corporation had pre-tax income for 2017 of $2,500,000. On December 31, 2017, Boyd disposed of a component of its business that represented a strategic shift in operation. That component had a Loss on Discontinued Operations of $450,000 (pre-tax). Boyd received $1,000,000 net cash proceeds from the...
I am not quite sure how to get this program working or how to start it...
I am not quite sure how to get this program working or how to start it in the first place. Write a full Java program and do the following: 1- Create a generic class and declare a one dim array as a private member. 2- Add a constructor to initialize the array. 3- A set method to set the array. 4- Sort method to sort the array. 5- Print method to print the array. 6- Reverse method to reverse the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT