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.

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 < list1.size(); i++) {
if(  .equals(  )==false) {
return  ;
}
}
return  ;
}

2.

Complete the missing statements so that the output of the following code is:
[[10, 70, 20, 90], null, [50, 80]]
Do NOT include spaces in your answer!
ArrayList<Integer> b = new ArrayList<Integer>(Arrays.asList(10,888,20,90));
ArrayList<Integer> a = null;
ArrayList<Integer> c =  ; //make a reference copy only!
b.set(1, 70);
b = new ArrayList<Integer>(Arrays.asList(50, 80));
ArrayList<  > list = new ArrayList<  >();
list.add(  );
list.add(  );
list.add(  );
System.out.println(list);

Solutions

Expert Solution

1) //Here we need use boolean as return type, because we need to return the value true or false, which states whether list1 and list2 are same or different
   public static boolean sameSame(ArrayList<Integer> list1, ArrayList<Integer> list2) {
       if(list1 == null || list2 == null)
           return false;
      
       //If the number of elements in the lists are different, we can say that lists are not same. So, here we are checking the size of the lists and if their sizes are not equal, we are returning false.
       if( list1.size() != list2.size() )
           return false;
      
       for(int i=0; i < list1.size(); i++) {
           //In the question it is mentioned that both the lists should contain same items in same order. So, we are comparing the values in list1 with the elements in list2 at same index(i). Here i moves from index 0 to the index before end of the list.
           if( (list1.get(i)).equals(list2.get(i) )==false) {
               //If any of the values in the lists does not match, we return false.
               return false;
           }
       }
       //The program execution reaches this place, only if both the lists are not empty, and their sizes are same and all their element values are same in same order. So, here we return true.
       return true;
   }

2) ArrayList<Integer> b = new ArrayList<Integer>(Arrays.asList(10,888,20,90));
       ArrayList<Integer> a = null;
       //We are assigning array 'b' to 'c', because the value of array 'b' is changing to [50,80] later. To preserve current value of array 'b', we are creating a reference to array 'b' using 'c'. Here the value of array 'b' and 'c' is (10,888,20,90).
       ArrayList<Integer> c = b ; //make a reference copy only!
       //Here the value at index 1 of array 'b', which is 888, is set to 70. So, now the value of array 'b' and 'c' is (10,70,20,90).
       b.set(1, 70);
       //Here value of array 'b' is reassigned to (50,80). But value of array 'c' is (10,70,20,90) as it is pointing to previous list of array 'b'.
       b = new ArrayList<Integer>(Arrays.asList(50, 80));
       //Here we are adding the arrays 'a','b','c' to another list 'list'. As the datatype of arrays 'a','b','c' is ArrayList<Integer>, the ArrayList 'list' holding type should be 'ArrayList<Integer>'
       ArrayList< ArrayList<Integer> > list = new ArrayList< ArrayList<Integer> >();
       //As per the output, [10, 70, 20, 90] should be displayed first. So, we are adding the array 'c' which has these values.
       list.add( c );
       //As per the output, null should be displayed next. So, we are adding the array 'a' which has the null value.
       list.add( a );
       //As per the output, [50, 80] should be displayed next. So, we are adding the array 'b' which has these values.
       list.add( b );
       System.out.println(list);

Below is the complete program if you want to execute and check it:

ListsProgram.java:
import java.util.ArrayList;
import java.util.Arrays;

public class ListsProgram {
   public static void main(String args[]) {
       ArrayList<Integer> list1 = new ArrayList<Integer>(Arrays.asList(10,40,20,50,60,50));
       ArrayList<Integer> list2 = new ArrayList<Integer>(Arrays.asList(10,40,20,50,60,50));
       boolean result = sameSame(list1,list2);
       System.out.println("both are same:" + result);
      
       printArrays();
   }
  
   public static boolean sameSame(ArrayList<Integer> list1, ArrayList<Integer> list2) {
       if(list1 == null || list2 == null)
           return false;
      
       if( list1.size() != list2.size() )
           return false;
      
       for(int i=0; i < list1.size(); i++) {
           if( (list1.get(i)).equals(list2.get(i) )==false) {
               return false;
           }
       }
       return true;
   }
  
   public static void printArrays()
   {
       ArrayList<Integer> b = new ArrayList<Integer>(Arrays.asList(10,888,20,90));
       ArrayList<Integer> a = null;
       ArrayList<Integer> c = b ; //make a reference copy only!
       b.set(1, 70);
       b = new ArrayList<Integer>(Arrays.asList(50, 80));
       ArrayList< ArrayList<Integer> > list = new ArrayList< ArrayList<Integer> >();
       list.add( c );
       list.add( a );
       list.add( b );
       System.out.println(list);
   }
  
}


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