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

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...
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.
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...
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,...
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...
Answer each of the following questions related to managing labor relations. This unit's discussion board assignment...
Answer each of the following questions related to managing labor relations. This unit's discussion board assignment consists of the following activities: 1) Why do employees choose to join labor unions? 2) How have unions improved working conditions for US workers? 3) Give two examples for each question listed above.
This assignment must be completed on a Word Document and submitted. All questions related to the...
This assignment must be completed on a Word Document and submitted. All questions related to the case study must be answered completely in order to receive full credit. This case study assesses SLO #2: "On completion of this course, student will describe steps taken to prevent and manage medical emergencies in the dental office." You have just seated your patient to have a tooth prepared for a crown. It has already been a hectic morning and the dentist has urged...
Using existing Stack Java Collection Framework, write Java Code segment to do the following.   You may...
Using existing Stack Java Collection Framework, write Java Code segment to do the following.   You may write this in jGrasp Create a Stack of String called, myStacks Read input from keyboard, 10 names and then add to myStacks As you remove each name out, you will print the name in uppercase along with a number of characters the name has in parenthesis. (one name per line).   e.g.     Kennedy (7) Using existing Stack Java Collection Framework, write Java Code segment to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT