I have problem with my Java program. Can all of you help me..
This is the program.
Introduction
On the website "MukaBuku", every user has the following property:
Name, the name of the user
Posts, a number of works published post
Friends, a number of existing friends
Of the property, Mr. C want some sort DOWN as follows:
Compare user based on the large number of existing Friends. If there are two
users who have the same number of Friends who then continue the process of sorting the points 2
Compare user based on the number of Posts that exist. If there are two users who
have the same amount Posts sorting process then proceed to point 3
Last is to compare two users based on their name in a letter of the alphabet.
Input Format
Input consists of N +1 lines. On the line to - 1 represents the number of users that exist in
website "MukaBuku", where 1 ≤ N ≤ 1000. Then in row - 2 to row
Output Format
The output consists of N lines.
example
input :
6
Budi 13 14,
Andi 90 87,
Fawwaz 39 109,
Bayu 41 9,
Ani 77 77,
Ano 77 77,
output :
Total Sorting : 8
Fawwaz 109 39
Andi 87 90
Ano 77 77
Ani 77 77
Budi 14 13
Bayu 9 41
In addition to what the above post says, have your MukabuMember implement the Comparable<MukabuMember> interface. then override public int compareTo(MukabuMember m) with the comparison process that you explained.
Once this is done, you can call Arrays.sort(yourlist), and it will be sorted your way.
Have a class(say MukabuMember) with all the propreties that u've mentioned as fields. Now have a main class and create N objects for the MukabuMember class. Create a list with MukabuMember class as parameter. Use an iterator to traverse through the list for sorting. Instead of a single compare stmt, you need three compares (in a nested if fashion) to sort according to ur requirements. This should do enough.
Here are the steps you can follow:
Create a class called MukabukuMember that has the fields name, numberOfPosts *numberOfFriends*
Make this class implement the Comparable interface.This interface has a single method compareTo where-in you can write all the comparison logic.
Mkae sure you also override the equals and hashCode method in the MukabukuMember class.A special care you need to take is to ensure that the logic to determine that two MukabukuMember classes are equal are the same in both the compareTo and equals
Once you are done with the above three steps, you can either store these objects within a List and iterate over it. Within each iteration you can simply then test two objects of the MukabukuMember class say obj1 and obj2 as follows:
obj1.compareTo(obj2) - Returns 1 if obj1 > obj2; 0 if obj1 == obj2 and -1 if obj1 < ob
obj1.equals(obj2) - Returns true if obj1 == obj2
Related
Write a method to randomly generate a set of m integers from an array of size n. Each element must have equal probability of being chosen.
This is a fairly well known question - featured in multiple books and interviews - but either I am not reading the question correctly, or to me the requirements of this question cannot actually all be fulfilled at the same time in Java.
Let's say have an array of size n=3
Integer[] ar = {1,1,5}
if w chose m=2 for our randomly generated set, I don't see how we can guarantee an equal probability for each element to be chosen.
In other words, asking for a Java set of 2 integers from the given array of size 3 makes it impossible to ensure an equal probability for each element. To illustrate, if we call the [0] a, [1] b, [2] c, then the all the 2 element combinations chosen at random, with removal, will look like this:
ab
ba
ac
bc
ca
cb
Since choices 1) and 2) would automatically invalidate a unique element requirement of a set in Java, in this particular situation element 'c' i.e. number 5 will always end up with a probability of 100% if we are to end up with a set of 2 elements.
I guess it is even easier to illustrate this issue if the array contains only duplicates i.e. {1,1,1}, and then a set of m=2 integers would simply be impossible.
Is there something I am misreading or misinterpreting with regards to this question.
I have some JSON coming to my Java program. It has a particular field with six fixed numbers: 0, 30, 60, 120, 240 or 480. Is it possible in Java to choose a better data type than short? Maybe by using an enum in some form or by representing input in bits by taking advantage of knowing the fixed input in advance?
Regarding enums, they seem to be made for a different use case, from Oracle Java docs for enum, it looks like if I use an enum, it will still end up creating an int internally, so I don't see any advantage ultimately in speed or memory. Is there anything I am missing?
I tried to google but couldn't get an appropriate answer yet.
First, observe that the numbers from your example follow a certain pattern - they are constructed from powers of two multiplied by 30:
0 - 0 - 0*30
1 - 20 - 1*30
2 - 21 - 2*30
4 - 22 - 4*30
8 - 23 - 8*30
16 - 24 - 16*30
If you store a small number between 0 and 5, inclusive, you can compute the target number back either with a look-up table, or with a simple bit shifting expression:
byte b = ... // Store the value in a variable of type "byte"
int num = b!=0 ? 30*(1<<(b-1)): 0;
Note: Since enum is a full-blown class, it would generally use as much or more space than a primitive number.
Suppose we have a set of keys: <54, 18, 10, 25, 28, 36, 38, 41, 12,
90>. Use the hashing function key % N to map each key into the
following array. If there is a collision, use the separate chaining
technique.
And below is just the pictorial of the array with the array labelled A and it is of size 13 so the picture is the array cells listed 0-12. N=13.
My understanding so far of hashing for this problem is that I need to arrange the keys given into the array using the function key % 13 (N being equal to 13). But my book doesn't give examples of different functions. The only example it uses is an alphabetizing one with first letters of last names.
Can anyone give me a brief explanation without just giving me the answer?
As you mentioned,your hash function is h=key%13;
Suppose there is Memory location starting from address 0 to 20.
So apply this function for every element in your array.
1) h1= 54 % 13 = 2 => This will go to the 2nd address location.
2) h2= 18 % 13 = 5 => This will go to the 5th location.
3) h3= 10 % 13 = 10 => This will go to 10th location.
4) h4= 25 % 13 = 14 => This will go to 14th location.
5) h5= 28 % 13 = 2 => Here Collision occurred as 54 is already present at 2nd location.
Now Solution is to use Separate Chaining.
Separate Chaining means just adding this current element to the next location in the Linked List of 2nd Location. Means a new Linked List is meantained at every location when there is Collision.
Below is pictorial ex. of separate chaining.
Hope You got a answer.In above figure elements are different but it will work same.
For more details go to this link : enter link description here
You appear to understanding the general process of inserting a value into a hash. All you need to do is relate your textbook example to your homework assignment question.
Determine which bucket you need to put the value in based on the hashing function. In the textbook example, the hashing function is taking the first letter of the last name. In your assignment, the hashing function is N % 13.
Resolve any collisions and perform the actual insertion. You don't mention what your textbook example uses as a collision resolution strategy, but your assignment asks you to use separate chaining.
I'd like to perform a function on a a list of data in at text file but I'm not sure how to go about it. My understanding of how to manipulate arrays and strings is weak, that is why I'm doing this project.
My text file looks like:
0, 0. 50 0.5 66;
1, 0. 69 2. 70;
2, 0.5 48 0.5 71;
3, 1. 47 1. 75;
4, 2. 52 1. 74;
5, 2. 71 1. 80;
6, 3. 65 1. 61;
7, 4. 62 1. 68;
I'm looking for a result like this:
0, 0. 50 0.5 66 0. 69 2. 70;
1, 0.5 48 0.5 71;
2, 1. 47 1. 75;
3, 2. 52 1. 74 2. 71 1. 80;
4, 3. 65 1. 61;
5, 4. 62 1. 68;
The second elements of the first two entries is the same (0.)and I'd like to combine these entries onto the same line. The same for lines 4 and 5.
As I understand I must read my file into an array list, but I don't know how to query the array list to find all entries that have the same value for the second element, and then merge the entire entry with another entry.
I have no code to show because I don't know where to begin.
I appreciate any input.
I wouldn't use a list, i would use a map. A Map has a method
put(key, value)
So, if you are interested in the second column of a given line, I would do something like (pseudocode)
Map<String, String> lookup = new HashMap<String, String>();
for (...) { // loop over the lines in the file
String key = getSecondFieldFromLine(line); // implement this
String val = lookup.get(key);
if (val != null ) {
merge(val, line); // implement this according to whatever merge means
}
lookup.put(key, val);
}
You might have to tweak this (for example, store the result of the merge to put in the lookup), what you are doing here is leveraging the fact that for a Map, put and get operations are really fast (a HashMap uses the hashCode method that is on every object). So you can easily determine if you already have a line with the field you are looking for, since you use the field of interest as the key, to look up the like that the field is in.
I would parse the input into a list of lists (or array of arrays or whatever collection you prefer). I would implement a Comparator that compares based on the second element in a list. You can then sort your main list or array. Finally, you can iterate through your list of lists, combining adjacent lists using whatever your combinatorial logic is, producing a new list as the result.
On the other hand, if your input is always going to be sorted as you are showing, then you can skip the comparator implementation and sorting altogether and just iterate through your collection performing the combinations
I am doing an application that will compute all 2 size frequent itemset from a set of transactions. That is the application will have as input a data file (space delimited text file - with the items encoded as integers) and a percentage, given as an integer (e.g. input 2 represents 2%). The application will output in a distinct file each pair of numbers that appears together in the same transaction (a transaction is represented by one line in the file) in more than 2% of all transactions (where 2% is the percentage given in the input). The output file will contain each pair of items in a line together with their support (the number of transactions where they appear) also the application will output (on the screen on in a file) the duration (the time needed to execute the task).
the data file will be like
55 22 33 123 231 414
21 43 432 435 231 4324 534
22 21 33 123 231 534 666 222
...
each line is called a transaction and the input file contains thousands of transactions.
I am thinking about using the data mining rule first to find all the single numbers whose appear frequency is larger than 2% in each transaction, and then form pairs for each transaction and at last compare each pair and generate the output file.
anyone has some ideas or code for this please help, if you have code(better in java) for this that will be very helpful Thanks a lot.
Here's one way to count the integers.
public class IntCount {
public static void main(String[] args) {
count("123 234 456 678 789 234 234 123");
}
public static void count(String transactionLine) {
String[] parts = transactionLine.split(" ");
Map<String, Integer> hashTable = new HashMap<String, Integer>();
// Count duplicates
for (String s : parts) {
if (hashTable.get(s) == null) hashTable.put(s, 1);
else hashTable.put(s, hashTable.get(s) + 1);
}
for (String s : hashTable.keySet()) {
System.out.println("s: " + s + " count: " + hashTable.get(s));
}
}
}
Now you can start working through determining the 2% part.
Do each transaction one at a time. For each transaction, find all numbers that are paired. Put them in a HashTable<Integer,Integer> with the number as the key and a value of 1. If there is already an entry, increment the value.
Once you have processed all transactions, go through the HashMap and look for values greater than 2% the total number of transactions. These are your winners.
They can be output directly to a file, or stored in another data structure for sorting first.
What you want to do, is basically find all all frqequent 2-itemsets. And itemset that has 'k' elements is called k-itemset.
The easiest way for your task would be to modify any open source apriory implementation in java to stop enumerating itemsets, after finding all frequent 2-itemsets. It wouldn't be that difficult, because Apriori, starts by counting all the 1 itemsets, then it takes all the frequent 1-itemsets, Generates candidate 2-itemsets using them, scans the database again, counts the support for those candidate 2-itemsets, chooses the frequent ones, generates candidate 3 itemsets and so on...
For example, suppose that frequent 1 itemsets are the following
a, c, d
Then, the algorithm generates all possible 2 itemsets as following
ac, ad, cd
Counts their support by scanning the database again and filters out infrequent ones.
You could just create a 2-dimensional array of size n x n where n is the number of items.
The matrix would store the support of each pair of items.
Then you scan the transactions and increase the count in the matrix.
After finishing reading the database, you have all itemsets of size 2 and their frequency in the matrix.
Note that, for efficiency, a triangular matrix is generally used.