Question

In: Computer Science

Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the...

Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. (2 point) 2. Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. (2 points) 3. You have been given the job of creating a new order processing system for the Yummy Fruit CompanyTM. The system reads pricing information for the various delicious varieties of fruit stocked by YFC, and then processes invoices from customers, determining the total amount for each invoice based on the type and quantity of fruit for each line item in the invoice. The program input starts with the pricing information. Each fruit price (single quantity) is specified on a single line, with the fruit name followed by the price. You can assume that each fruit name is a single word consisting of alphabetic characters (A–Z and a–z). You can also assume that prices will have exactly two decimal places after the decimal point. Fruit names and prices are separated by a single space character. The list of fruit prices is terminated by a single line consisting of the text END_PRICES. After the fruit prices, there will be one or more invoices. Each invoice consists of a series of line items. A line item is a fruit name followed by an integer quantity, with the fruit name and quantity separated by a single space. You can assume that no line item will specify a fruit name that is not specified in the fruit prices. Each invoice is terminated by a line consisting of the text END_INVOICE. As a special case, if a line with the text QUIT appears instead of the beginning of an invoice, the program should exit immediately. The overall input will always be terminated by a QUIT line. (5 points) Example input: orange 0.80 pomegranate 2.50 plum 1.20 peach 1.00 persimmon 1.75 lime 0.60 END_PRICES persimmon 2 orange 3 peach 1 plum 10 pomegranate 5 END_INVOICE peach 11 plum 5 orange 1 lime 9 END_INVOICE QUIT For each invoice, the program should print a single line of the form Total: X.YY where X.YY is the total cost of the invoice, which is the sum of the costs of all of the line items in the invoice. The cost should be printed with exactly two digits after the decimal point. Example output (corresponding to the input shown above): Total: 31.40 Total: 23.20 4. Implement insertionSort. (8 points) 5. Write the code to the No. 50 Fibonacci number. (2 points) 6. Write the main method to test your program and use System.nanoTime() to find out the speed of each step of your program. (1 point)

Solutions

Expert Solution

1)

class ArrayListReview<E>{
ArrayList<E> list=new ArrayList<>();
LinkedList<E> linkedlist=new LinkedList<>();

}

2)

Since you said to create a constructor to add an element at index i, but I got a doubt that at start the size of list is empty so how can we insert the value at that position. So i created 3 types of constructors and a method. Use whichever you like.

class ArrayListReview<E>{
ArrayList<E> list=new ArrayList<>();
LinkedList<E> linkedlist=new LinkedList<>();
ArrayListReview(){
list=new ArrayList<>();
linkedlist=new LinkedList<>();
}
ArrayListReview(ArrayList<E> a,LinkedList<E> b){
list=new ArrayList<>(a);
linkedlist=new LinkedList<>(b);
}
ArrayListReview(E data,int index){
list.add(index, data);
linkedlist.add(index, data);
}
public void insert(E data,int index){
list.add(index, data);
linkedlist.add(index, data);
}
}

3)

public static void yfc(Scanner scan){
Map<String,Double> items=new HashMap<>();
//Scanner scan=new Scanner(System.in);
String item=scan.next();
while(item.equals("END_PRICES")){
double price=scan.nextDouble();
items.put(item,price);
item=scan.next();
}
  
item=scan.next();
while(item.equals("QUIT")){
double total=0;
while(item.equals("END_INVOICE")){
int quantity=scan.nextInt();
total+=quantity*items.get(item);
item=scan.next();
}

System.out.printf("Total: %.2f\n",total);
item=scan.next();
}
}

4)

1) Pick element arr[i] and insert it into sorted sequence arr[0…i-1]

public static void insertionSort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

5)

Fibonacci: 1,1,2,3,5,8,13,....

public static int fibonacci(int n){
if(n==1) return 1;
if(n==2) return 1;
int a=1,b=1,c;
for(int i=2;i<n;++i){
c=a+b;
a=b;
b=c;
}
return b;
}

6)

public static void main(String args[]){
Scanner scan=new Scanner(System.in);
long a=System.nanoTime();
yfc(scan);
long b=System.nanoTime();
System.out.println("Time of YFC: "+(b-a));
  
int[] arr=new int[10];
for(int i=0;i<10;++i){
arr[i]=scan.nextInt();
}
a=System.nanoTime();
insertionSort(arr);
b=System.nanoTime();
for(int i=0;i<10;++i){
System.out.print(arr[i]+" ");
}
System.out.println();
System.out.println("Time to insertionSort: "+(b-a));
  
a=System.nanoTime();
int num=fibonacci(50);
b=System.nanoTime();
System.out.println("Time to find fabinocci: "+(b-a));
}

Output:

Total: 31.40
Total: 23.20
Time of YFC: 26984662
1 5 7 8 9 12 21 23 39 89 
Time to insertionSort: 6530
Time to find fabinocci: 4296

Related Solutions

in java please: Create an ArrayListReview class with one data field of ArrayList and one with...
in java please: Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. (2 point) Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. (2 points)
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
Create a concrete LinkedList class that extends the provided ALinkedList class. You will need to override...
Create a concrete LinkedList class that extends the provided ALinkedList class. You will need to override the extract()method in your class. You can use your main() method for testing your method (although you do not need to provide a main method). Recall that a list is an ordered collection of data X_0, X_1, X_2, ..., X_n-1 The extract(int start, int end) method removes all elements X_start, X_start_1, ..., X_end-1 from the list. It also returns all removed elements as a...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance Variables Declare an ArrayList of BabyName objects. Constructor public BabyNamesDatabase () - instantiate the ArrayList of BabyName objects. You will not insert the items yet. This method will be one line of code. Mutator Methods public void readBabyNameData(String filename) - open the provided filename given as input parameter, use a loop to read all the data in the file, create a BabyName object for...
Is List type is an interface in the Java collections framework? Classes Vector, ArrayList, and LinkedList...
Is List type is an interface in the Java collections framework? Classes Vector, ArrayList, and LinkedList are the same data structure but implement data storage in different ways. Classes, that implement Map interface in Java collections framework are used for storing what type of data? Declare and instantiate a list of elements of type String. Name this list myArray. what type of data structure is Stack? LinkedList data structure in Java collections is implemented as doubly linked lists. In PriorityQueue...
Here is a C++ class definition for an abstract data type LinkedList of string objects. Implement...
Here is a C++ class definition for an abstract data type LinkedList of string objects. Implement each member function in the class below. Some of the functions we may have already done in the lecture, that's fine, try to do those first without looking at your notes. You may add whatever private data members or private member functions you want to this class. #include #include typedef std::string ItemType; struct Node { ItemType value; Node *next; }; class LinkedList { private:...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT