Question

In: Computer Science

You are given a List L of generic type <T> , and another List P, which...

You are given a List L of generic type <T> , and another List P, which contains integers sorted in ascending order. The operation printLots(L,P) will print the elements in L that are in positions specified by P. For instance, if P = [1,3,4,6], the elements in positions 1,3,4, and 6 in L are printed. Write the procedure printLots(L,P). The code you provide should be the Java method itself (not pseudocode), the containing class is not necessary. You may use only use public Collection method that is inherited by Lists L and P, with the exception of the toArray() method. You may assume that the any integer in P is greater than or equal to 0 and less than L.size(). Both L and P can be empty. (Note: this is an iterator question, your solution must use iterators on L and P.)

Solutions

Expert Solution

import java.util.*;
public class PrintMessage{
    public static void main (String[] args) {
      Scanner sc=new Scanner(System.in);
      List<Integer> L=new ArrayList<Integer>();
      L.add(5);
      L.add(4);
      L.add(6);
      L.add(7);
      L.add(9);
      L.add(2);
      List<Integer> P=new ArrayList<Integer>();
      P.add(1);
      P.add(4);
      P.add(5);
      P.add(2);
      System.out.println("Elements in L are:");
      for (int i=0;i<L.size();i++){
          System.out.print(L.get(i)+" ");
      }
      System.out.println("\nElements in P are:");
      for (int i=0;i<P.size();i++){
          System.out.print(P.get(i)+" ");
      } 
      PrintMessage obj=new PrintMessage();
      obj.printLots(L,P);
    }
    public void printLots(List<Integer> L,List<Integer> P){
        List<Integer> result=new ArrayList<Integer>();
        for (int i=0;i<P.size();i++){
            result.add(L.get(P.get(i)));
        }
        System.out.println("\nResult:");
        for(int i=0;i<result.size();i++){
            System.out.print(result.get(i)+" ");
        } 
    }
}

Thank you!, if you have any queries post it below in the comment section i will try my best to resolve your queries and i will add it to my answer if required. Please give upvote if you like it.


Related Solutions

The period T of a simple pendulum is given by T=2π√L/g where L is the length...
The period T of a simple pendulum is given by T=2π√L/g where L is the length of the pendulum and g is the acceleration due to gravity. Assume that g = 9.80 m/s^2 exactly, and that L, in meters, is lognormal with parameters μL = 0.6 and σ^2L=0.05. NOTE: This is a multi-part question. Once an answer is submitted, you will be unable to return to this part. Find the mean of T. Find the median of T. Find the...
Let T∈ L(V), and let p ∈ P(F) be a polynomial. Show that if p(λ) is...
Let T∈ L(V), and let p ∈ P(F) be a polynomial. Show that if p(λ) is an eigenvalue of p(T), then λ is an eigenvalue of T. Under the additional assumption that V is a complex vector space, and conclude that {μ | λ an eigenvalue of p(T)} = {p(λ) | λan eigenvalue of T}.
Consider the linear transformation T : P2 ? P2 given by T(p(x)) = p(0) + p(1)...
Consider the linear transformation T : P2 ? P2 given by T(p(x)) = p(0) + p(1) + p 0 (x) + 3x 2p 00(x). Let B be the basis {1, x, x2} for P2. (a) Find the matrix A for T with respect to the basis B. (b) Find the eigenvalues of A, and a basis for R 3 consisting of eigenvectors of A. (c) Find a basis for P2 consisting of eigenvectors for T.
10. You are the manager of a firm that produces and markets a generic type of...
10. You are the manager of a firm that produces and markets a generic type of soft drink in a competitive market. In addition to the large number of generic products in your market, you also compete against major brands such as Coca-Cola and Pepsi. Suppose that, due to the successful lobbying efforts of sugar producers in the United States, Congress is going to levy a $0.50 per pound tariff on all imported raw sugar-the primary input of your product....
a. Define a function less of type (String, List) -> List so that less(e, L) is...
a. Define a function less of type (String, List) -> List so that less(e, L) is a list of all the strings in L that are shorter than e. b. Define a function more of type (String, List) -> List so that more(e, L) is a list of all the strings in L that are longer than e. c. Replace the above functions with a function compare of type (String, List, (String, String) -> Boolean) such that depending on the...
Write a generic class Pair which has two type parameters—F andS—each representing the type of...
Write a generic class Pair which has two type parameters—F and S—each representing the type of the first and second element of the pair, respectively. Add set and get methods for the first and second elements of the pair. (Hint: The class header should be public class Pair.)Write a separate PairTest class to test class Pair. Create 2 Pair types and test your get and set methods on the following pair types:Pairp1 takes Integer and String types as a pairPairp2...
For a fraction nonconforming of p = 0.05, which type of plan would give you the...
For a fraction nonconforming of p = 0.05, which type of plan would give you the lowest ASN? a. Single Sampling Plans b. Double Sampling Plans c. Sequential Sampling Plans d. Simple Random Sampling Plans
If tax revenue is given by the following function: REV(t)=t×w×(h-l(t)), where t is the labor tax...
If tax revenue is given by the following function: REV(t)=t×w×(h-l(t)), where t is the labor tax rate, w is the wage rate, h is the maximum amount of time available to the household, and l(t) is leisure as an increasing function of the tax rate i.e. if the labor tax rate t increases, leisure increases, so that individuals work less. Assume that l(t)=min[h,t²]. This simply means that leisure cannot exceed h which is the maximum amount of time available. Find...
A model for the population P(t) in a suburb of a large city is given by...
A model for the population P(t) in a suburb of a large city is given by the initial-value problem dP dt = P(10−1 − 10−7P),    P(0) = 3000, where t is measured in months. What is the limiting value of the population? At what time will the population be equal to one-half of this limiting value? (Round your answer to one decimal place.) months
A list of six positive integers, p, q, r, s, t, u satisfies p < q...
A list of six positive integers, p, q, r, s, t, u satisfies p < q < r < s < t < u. There are exactly 15 pairs of numbers that can be formed by choosing two different numbers from this list. The sums of these 15 pairs of numbers are: 25, 30, 38, 41, 49, 52, 54, 63, 68, 76, 79, 90, 95, 103, 117. Which sum equals r + s?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT