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.

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 < list.size(); i++) {

if(    ) {

 ; }

}  ; }

3.

Consider the following list:

ArrayList list = new ArrayList(Arrays.asList(10, 70, 20, 90, 30, 80, 50, 40, 60));

Complete the following code that gives the following output:

20 90 30 80 50 40 60

 <interger> iter = list.listIterator(  );

while(iter.  ) {

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

}

Solutions

Expert Solution

Ans.) 1. ) data.remove(10)

Because 'remove' removes first occurrence given element from the list. Ex-

 Input :
    list [10, 20, 30, 40, 30]

    function call to remove 30 from the list:
    list.remove(30)

    Output:
    List elements after removing 30
    list [10, 20, 40, 30]

2.)

public static boolean containsPositive(ArrayList list)

{

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

{

if( (int)list.get(i)>0 )

{

return true ;

}

}

return false ;

}

To access an element in the ArrayList, use the get() method and refer to the index number:

Ex- list= 10,20,30

list.get(1)=20.

Since we want the answer in boolean type i.e. whether the list contain any positive number or not , hence we use the return type of the function as boolean.

We use the 'get' function to access the element of the list and compare whether any digit id positive or not. If it is then it returns true otherwise false. Notic that we used the typecasting to convert the list of object type to integer type '(int)list.get(i)>0'.

Check the sample code for the following problem-

/******************************************************************************

                            Online Java Compiler.
                Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/
import java.util.ArrayList;
public class Main
{
    public static boolean containsPositive(ArrayList list) 

{
for(int i=0; i < list.size(); i++) 
{
if((int) list.get(i)>0)
{
return true ;
}
}
return false ;
}
        public static void main(String[] args) {
                 ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(-1);
     list.add(-5);
      list.add(9);
       boolean m= containsPositive(list);
       System.out.println(m);
      
   
        }
}

OUTPUT-

3.)

ListIterator <interger> iter = list.listIterator( 2 );

while(iter. hasNext()) {

System.out.println( iter.next()+" ");

The 'listIterator(int index)' method of java.util.ArrayList class is used to return a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one.

Here 'list.listIterator( 2 )' means that our list will start from the index 2 i.e element 20 and print all the element after it.

'hasNext()'  method Returns true if this scanner or list has another token in its input.

next() method finds and returns the next complete token from this scanner(here list). A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.


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