vijayinani
BAN USER- 1of 1 vote
AnswersMultiple threads are running in my program. Exception occurs in one of the thread. There is no exception handling done.
- vijayinani in India
What will happen to my program? Will it terminate the complete program? What will happen to other threads?| Report Duplicate | Flag | PURGE
Credit Suisse Software Engineer / Developer Java - 0of 0 votes
Answersroot, directory, 128, admin, NONE
- vijayinani in India for Java
users, directory, 512, admin, root
santana, directory, 1024, santana, users
santana.jpg, photo, 128000, santana, santana
Project.doc, document, 256000, santana, santana
Incredibles.mpg, movie, 4123456123, santana, santana
marcus, directory, 128, marcus, users
Resume.doc, document, 256000, marcus, marcus
This contains information about files stored in a file system. Each line corresponds to one file, and the fields are separated by commas. The first field contains the filename, the second contains the file type, the third field is the size of the file in bytes, the fourth field is the username of the owner of the file, and the last field is the name of the parent directory of this file (i.e. the name of the directory in which this file is located.) Note: the special parent directory name NONE indicates that this file is the root directory of the filesystem. Also, for the purposes of this program, assume that all file/directory names are unique in the system.
write a Java program which reads data in this format from a file, parses it, and figures out the total size of storage consumed by each directory in this system. The size of storage consumed by any directory is defined as the sum of the size of this directory, sizes of all the files in this directory and the total storage sizes consumed by all the directories in this directory. Your program should write the name of each directory, and the total storage consumed by it. The output should have one directory per line, and the format should be dirname: size.
Output should be:
santana: 4123841147
marcus: 256128
root: 4124097915
users: 4124097787
Provide the best algorithm and java program.| Report Duplicate | Flag | PURGE
Wipro Technologies Applications Developer Algorithm
import java.util.*;
public class SiteMap {
public static class UserPage {
private String user;
private int page;
public UserPage(String user, int page) {
this.user = user;
this.page = page;
}
}
public static void main(String[] args) {
UserPage[] userPages = {
new UserPage("A", 1),
new UserPage("B", 5),
new UserPage("A", 2),
new UserPage("A", 1),
new UserPage("B", 2),
new UserPage("C", 7),
new UserPage("C", 3),
new UserPage("A", 3),
new UserPage("C", 1)
};
Map<String, Integer> latestPageMap = new HashMap<>();
Map<Integer, Set<Integer>> result = new TreeMap<>();
for (UserPage userPage : userPages) {
int value = userPage.page;
if (!latestPageMap.containsKey(userPage.user)) {
latestPageMap.put(userPage.user, value);
if (!result.containsKey(value)) {
result.put(value, new HashSet<>());
}
} else {
int previousLatest = latestPageMap.get(userPage.user);
latestPageMap.put(userPage.user, value);
if (!result.containsKey(previousLatest)) {
result.put(previousLatest, new HashSet<>());
}
Set<Integer> tempSet = result.get(previousLatest);
tempSet.add(value);
result.put(previousLatest, tempSet);
}
}
for (int finalValue : result.keySet()) {
System.out.print(finalValue + " -> ");
Set<Integer> linkedPages = result.get(finalValue);
for (int linkedPage : linkedPages) {
System.out.print(linkedPage + " ");
}
System.out.println();
}
}
}
My attempt:
public class MergeAndSort {
public static void main(String[] args) {
Node n11 = new Node(5);
Node n12 = new Node(11);
Node n13 = new Node(9);
Node n14 = new Node(11);
Node n15 = new Node(10);
n11.next = n12;
n12.next = n13;
n13.next = n14;
n14.next = n15;
Node n21 = new Node(3);
Node n22 = new Node(19);
Node n23 = new Node(12);
Node n24 = new Node(4);
Node n25 = new Node(6);
Node n26 = new Node(11);
Node n27 = new Node(4);
n21.next = n22;
n22.next = n23;
n23.next = n24;
n24.next = n25;
n25.next = n26;
n26.next = n27;
Node temp = null;
Node current = null;
Node head = temp = n11;
int count = 0;
while (temp != null) {
current = temp;
temp = temp.next;
count++;
}
temp = current.next = n21;
while (temp != null) {
temp = temp.next;
count++;
}
temp = head;
while (temp != null) {
System.out.print(temp.value + " ");
temp = temp.next;
}
System.out.println();
System.out.println("Count : " + count);
Node finalHead = mergeSort(head);
temp = finalHead;
while (temp != null) {
System.out.print(temp.value + " ");
temp = temp.next;
}
temp = current = finalHead;
while (temp != null && temp.next != null) {
boolean considered = false;
while (temp.value == temp.next.value) {
temp = temp.next;
considered = true;
}
if (considered) {
current.next = temp;
}
current = temp;
temp = temp.next;
}
System.out.println();
temp = finalHead;
while (temp != null) {
System.out.print(temp.value + " ");
temp = temp.next;
}
}
public static Node mergeSort(Node headOriginal) {
if (headOriginal == null || headOriginal.next == null)
return headOriginal;
Node a = headOriginal;
Node b = headOriginal.next;
while ((b != null) && (b.next != null)) {
headOriginal = headOriginal.next;
b = (b.next).next;
}
b = headOriginal.next;
headOriginal.next = null;
return merge(mergeSort(a), mergeSort(b));
}
public static Node merge(Node a, Node b) {
Node temp = new Node();
Node head = temp;
Node c = head;
while ((a != null) && (b != null)) {
if (a.value <= b.value) {
c.next = a;
c = a;
a = a.next;
} else {
c.next = b;
c = b;
b = b.next;
}
}
c.next = (a == null) ? b : a;
return head.next;
}
}
public class Node {
int value;
Node next;
public Node(int value) {
this.value = value;
}
public Node() {
}
}
Step 1: Sort the array.
Step 2: Find the difference between the next index element and the current index element. If the difference is 0, then the number either at current or next index is the repeating number.
Example:
public static void findRepeatingNumber() {
int[] intArray = new int[10];
intArray[0] = 27;
intArray[1] = 91;
intArray[2] = 84;
intArray[3] = 71;
intArray[4] = 65;
intArray[5] = 71;
intArray[6] = 31;
intArray[7] = 71;
intArray[8] = 71;
intArray[9] = 71;
System.out.println("Original Array: " + Arrays.toString(intArray));
Arrays.sort(intArray);
System.out.println("Sorted Array: " + Arrays.toString(intArray));
int diff = 0;
for (int i = 0; i < 9; i++) {
diff = intArray[i + 1] - intArray[i];
if (diff == 0) {
System.out.println("Repeating number: " + intArray[i]);
break;
}
}
}
i1 != i2
i1 and i2 are objects created on different memory locations in the heap as new operator is used. As the hashCode's are different, != returns true.
i1 <= i2 and i1 >= i2
Here, i1 is Integer and so is i2. While comparison, they are unboxed to int and hence, return true.
Repstephenroach41, Android Engineer at Alliance Global Servies
Experienced and professional facilities coordinator with a commitment to customer service. An analytical business person as well as a practiced ...
Repnnelsonvance, Animator at Cognzant Technology Solutions
I am the learning and development manager and play a key role in coordinating all corporate learning and development activities ...
- vijayinani July 12, 2016