Help needed for beginner. Im so confused
I need help on this. Any suggestions?
--
Hashing is a way of mapping keys to addresses so that there is essentially on searching involved. The idea is to apply a hash function to the unique key value for each data entry to determine where it should go into an array (i.e. which bucket it belongs to). Unfortunately, any useful hash function will have collisions – more than one key being mapped to the same hash value. We will handle collisions by making each bucket the beginning of an ArrayList. For this assignment we will use a very simplistic hash map – mod 5.
You will have at least the following classes:
1. Node – a node will contain an integer for its unique identification number and a String to hold a name. It should be possible to get the name and key value from a node.
2. DataList – This will have an ArrayList to hold nodes that hash to the same value mod 5. The mod operator in Java is %. 7 % 2 would equal 1.This class should have a find method that returns the name belonging to a key or say the key is not on the list. It will also require an add method and a toString method which will nicely create a String which can be printed out using System.out.println();
3. Driver – The driver class will have a loop that constantly shows the user a menu. The user has 4 choices:
a. 1 – Add a new key, name pair (requires creating a Node)
b. 2 – Find the name belonging to a key (key is input name is output)
c. 3 – Display all lists
d. 4 – End
I suggest you create an array of size 5 to hold 5 ArrayLists of Nodes. Use mod to determine where a Node belongs. For example, if your array is called bins then
bins[key %5] would be the correct DataList to put the Node in.
Re: Help needed for beginner. Im so confused
Hi,
Is it a question or suggestion?
Core java
Re: Help needed for beginner. Im so confused
Suggestion: Start designing each of the classes in that you need. Decide what is each to do and what methods will it need to do it.
Re: Help needed for beginner. Im so confused
Quote:
Originally Posted by
Norm
Suggestion: Start designing each of the classes in that you need. Decide what is each to do and what methods will it need to do it.
well so far i have created 2 classes but im really lost.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Driver {
private Scanner in;
private Node[] nodes;
private int numberInArray;
private Node node;
public Driver()
{
numberInArray = 3;
nodes = new Node[5];
in = new Scanner(System.in);
menu();
}
public void menu()
{
int choice;
System.out.println("Enter 1 to add data, 2 to find data, 3 to list all data, and 4 to end");
choice = in.nextInt(); //No Error Checking.
if(choice==4)
{
System.out.println("Thank you for using the system. good bye");
}
while (choice != 4)
{
if (choice==1)
{
System.out.println("Enter the key then the name");
numberInArray++;
node= new Node(in.nextInt(), in.next() );
//nodes[]=node;
}
else if (choice==2)
{
System.out.println("Enter the key to be found");
}
else if (choice==3)
{
for (int index=0;index<numberInArray;index++)
{
System.out.println(nodes[index]);
}
}
else
{
System.out.println("You must enter one of 1, 2, 3, or 4.");
}
System.out.println("Enter 1 to add data, 2 to find data, 3 to list all data, and 4 to end");
choice = in.nextInt();
}
}
public void listing()
{
System.out.println("Here are the numbers:");
for (int index=0;index<numberInArray;index++)
{
System.out.println(nodes[index]);
}
public static void main(String[] args) {
new Driver();
}
}
public class Node {
private int id;
private String name;
public Node(int id, String name)
{
this.id=id;
this.name=name;
}
}
Re: Help needed for beginner. Im so confused
What questions do you have? Does the code compile and execute?
If you get errors, please copy and paste the full text here.
Re: Help needed for beginner. Im so confused
Please wrap your posted code in code tags: [code] to preserve its formatting. See:
BB Code List - Java Programming Forums - The Java Community