Pleas code in Java Write a generic class named PairImpl that implements the interface below.write a program that creates list of 3 pairs where each pair represents the name of a fruit and the quantity. Once the list is populated three pairs of fruits and quantities, it then prints each pair shown in the sample run using a for loop.
The Java code follows:
///////////////////////////// PairImpl.java ///////////////////////////
class PairImpl {
String fruit;
int qty;
public PairImpl (String f, int q) {
fruit = f;
qty = q;
}
}
/////////////////////////////////// Main.java /////////////////////////////////////
import java.util.*;
public class Main
{
public static void main(String[] args) {
ArrayList<PairImpl> p = new ArrayList<>();
p.add (new PairImpl ("Orange", 42));
p.add (new PairImpl ("Apple", 36));
p.add (new PairImpl ("Lemon", 99));
for (int i = 0; i < p.size(); i++) {
System.out.printf ("Fruit: %s \tquantity: %d\n", p.get(i).fruit, p.get(i).qty);
}
}
}
Get Answers For Free
Most questions answered within 1 hours.