Question

In: Computer Science

The following Assignment Questions are related to Java Collection Interfaces (Queue, List, Map, Set, etc), and...

The following Assignment Questions are related to Java Collection Interfaces (Queue, List, Map, Set, etc), and Java IOStream.

  1. As mentioned at the beginning of our Collection’s class that the basis of java interfaces is mainly based on "TREES". However, one of these interfaces forms a different "TREE". Describe that particular interface. [1 Marks].
  2. Why do we need to declare a type of object within each interface in the collection packages by "<E>" for example: <Integer, boolean, float, etc>”. [1 Marks].
  3. How can we build an interface (list, set, etc) without having repeated (duplicated) items? [1 Marks].
  4. Check the syntax of Advanced For Loop, if we use it over a List interface, you would notice that we cannot modify elements within that list. using the Iterator allows us to make some changes. Create a simple code that obtains a List <String> and uses the String.trim method to every item (element) within the created list. [3 Marks].
  5. What method/class use for reading a segment of data that has determined index reference (positions) closed or at the end of a huge file [1 Marks].
  6. Write a function that counts the occurrence of a specific letter (Eg. “a”) in a file. Where the letter can be defined at the prompt line (command line). [3 Marks].

Solutions

Expert Solution

1)

Collection and Map both are interfaces in java. util package but Collection is used to store objects and Map is used to store the objects for (key,value) based manner. Collection classes are used to store object in array format. and Map classes are used to store in (KEY,VALUE) pair format

2)

By defining a Interface/class generic you are making it type-safe i.e. it can act up on any datatype

We need because 1)Type check at compile time

2)code reuse

3)

Set is acollection that cann't contains dublicate elements

1)HashSet

2)LinkedHashSet

3)TreeSet

// Java program to demonstrate a Set

import java.util.*;
public class SetExample{
   public static void main(String[] args)
   {
       // Set demonstration using HashSet
       Set<String> hash_Set
           = new HashSet<String>();

       hash_Set.add("code");
       hash_Set.add("For");
       hash_Set.add("code");
       hash_Set.add("Example");
       hash_Set.add("Set");

       System.out.println(hash_Set);
   }
}

output:-

[Set, Example, code, For]

4)

List<String> trimmedStrings = new ArrayList<String>();
for(String s : originalStrings) { //Advanced for loop
trimmedStrings.add(s.trim());
}

originalStrings = trimmedStrings;

or

List<String> myList = Arrays.asList(" A", "B ", " C ");
return myList.stream().(str -> str.trim());

5)

class Myclass{

public static void main(String args[]){

int arr[]={1,2,3,4,5,6,7,8};

func(arr,arr.length);

}

public static func(int arr,int l){

for(int i=0;i<li++){

System.out.print(arr[i]);

}

}

}

6)

// C program to illustrate
// command line arguments
#include<stdio.h>

int main(int argc,char* argv[])
{
char counter;

int result;
   printf("Program Name Is: %s",argv[0]);
   if(argc==1)
       printf("\nNo Extra Command Line Argument Passed Other Than Program Name");
   if(argc>=2)
   {
counter=argv[1]

result=func(counter);

printf("%d",result);

return 0;
}

int func(char s){

int count=0;

char ch, file_name[25];
FILE *fp;

printf("Enter name of a file you wish to compare the element \n");
gets(file_name);

fp = fopen(file_name, "r"); // read mode

if (fp == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}

printf("The contents of %s file are:\n", file_name);

while((ch = fgetc(fp)) != EOF)
if(ch==s){

count++;

}

fclose(fp);
}

return count;
}
  
}


Related Solutions

IN JAVA LANGUAGE!!! These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet,...
IN JAVA LANGUAGE!!! These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each of them. Unless specified otherwise, sorted order refers to the natural sorted order...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
given a input file, parse it and answer the following frequency related questions, using java. -list...
given a input file, parse it and answer the following frequency related questions, using java. -list the most frequent word(s) in the whole file and its frequency. -list sentence(s) with the max. number of occurrences of the word “of” in the entire file and also list the corresponding frequency. program has two arguments; 1st : path to the input text file 2nd : name prefix for the output files ex. $ java assgn1 “./input.txt” “output” outputs: for each question create...
IN JAVA Objectives Practice Link list, List node, compareTo, user defined data type, interfaces Movie List...
IN JAVA Objectives Practice Link list, List node, compareTo, user defined data type, interfaces Movie List Create a link list of the movies along with the rating, number of the people who watched the movie and the genre of the movie. Required classes Movie class ListNode class MovieList class List interface Driver class Movie class implements Comparable Attributes: movie’s name, genre, rating, number of people watched Methods: constructor, getter, setter, equals, compreTo, toString ListNode class Attributes: each node has two...
N JAVA Objectives Practice Link list, List node, compareTo, user defined data type, interfaces Movie List...
N JAVA Objectives Practice Link list, List node, compareTo, user defined data type, interfaces Movie List Create a link list of the movies along with the rating, number of the people who watched the movie and the genre of the movie. Required classes Movie class ListNode class MovieList class List interface Driver class Movie class implements Comparable Attributes: movie’s name, genre, rating, number of people watched Methods: constructor, getter, setter, equals, compreTo, toString ListNode class Attributes: each node has two...
Lab Assignment Write a Java program that implements a queue in a hospital. I want your...
Lab Assignment Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue. Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S,...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Questions 6-10 are related to the following As a statistics course assignment you are to build...
Questions 6-10 are related to the following As a statistics course assignment you are to build a 95% confidence interval for the mean monthly rental for two-bedroom apartments in Marion County. A random sample of 80 apartments yields the following data. 840 1550 560 1080 970 1560 830 1220 1390 1110 920 1150 1600 1310 610 1590 1100 720 720 1350 1020 1290 710 600 830 1190 1170 1470 1320 730 800 840 1130 690 900 1520 1050 1220 1400...
In java. Write a method static <K, V> void addToMultiMap(Map<K, Set<V>> map, K key, V value)....
In java. Write a method static <K, V> void addToMultiMap(Map<K, Set<V>> map, K key, V value). addToMultiMap must add the value, if present, to the set associated with the given key, creating the set if necessary. You may assume all keys already in the map are associated with non-null values. For full credit, your method must include generic types, and must not contain unnecessary method calls or loops, even if they do not otherwise impact correctness. You may assume Map,...
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO)....
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO). Use the driver to then test each of the methods. Simply download it and place it with the other source files. Create a class GenLLQueue which has the following: Internal class ListNode which contains: Instance variable data of type T Instance variable link of type ListNode Default constructor that sets both instance variables to null Instance Variables head which is of type ListNode which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT