How can implement a perfect hashed data structure using the four basic operations? Could I do something like this or do I need to break up my code into two different classes and then test the methods from another class's main method?

public class PerfectHashed{

int ticketNumber; // keyfield

String PurchaserName; 

input(){
// key values ranges between 2000 to 100, 000 
System.out.print("Please enter a ticket number between 2000 to 100,000: " ); 
// to hold there answer.
number= nextInt(); 

}
 // Then would need the four basic operations in my application:  

// the insert direct hashed algorithm 
    pseudoKey = preProcessing(targetKey); 
  ip = pseudoKey; // direct hashing function 

// insert the new node
data[ip]= newNode.deepCopy(); 

  // the fetch algorithm 
 // access the primary storage area 

 pseudoKey = preprocessing(targetKey); 
ip = pseudoKey; // direct hashing function 
if(null[ip] = = null)
 { return null; } 
else{return   data[ip].deepCopy(); 
}

  // the delete direct hashed algorithm 

 // access the primary storage area 
 pseudoKey = preprocessing(targetKey); 
ip = pseudoKey; // direct hashing function 

if(null[ip] = = null)
 { return false; } 
else 
  { data[ip]=null;                      
      return true; }
} 

// the update direct hashed algorithm 
if(delete(targetKey) == false)
    return false; 
 else 
 {
     insert(newNode) return true;
 }

Recommended Answers

All 13 Replies

I very much doubt your code will compile.

if(null[ip] = = null)

what is that all about? to begin with?

@stultuske Sorry I meant if(data[ip]== null). Do you think you can help me?

public class PerfectHashed{

int ticketNumber; // keyfield

String PurchaserName; 

input(){
// key values ranges between 2000 to 100, 000 
System.out.print("Please enter a ticket number between 2000 to 100,000: " ); 
// to hold there answer.
number= nextInt(); 

}
 // Then would need the four basic operations in my application:  

// the insert direct hashed algorithm 
    pseudoKey = preProcessing(targetKey); 
  ip = pseudoKey; // direct hashing function 

// insert the new node
data[ip]= newNode.deepCopy(); 

  // the fetch algorithm 
 // access the primary storage area 

 pseudoKey = preprocessing(targetKey); 
ip = pseudoKey; // direct hashing function 
if(data[ip] = = null)
 { return null; } 
else{return   data[ip].deepCopy(); 
}

  // the delete direct hashed algorithm 

 // access the primary storage area 
 pseudoKey = preprocessing(targetKey); 
ip = pseudoKey; // direct hashing function 

if(data[ip] == null)
 { return false; } 
else 
  { data[ip]= null;                      
      return true; }
} 

// the update direct hashed algorithm 
if(delete(targetKey) == false)
    return false; 
 else 
 {
     insert(newNode) return true;
 }

There's no point posting code that is so far from valid Java - nobody can test it and hardly anyone will understand what it supposed to be.

At your stage in the learning process it's a waste of time to keep typing in code without stopping to see if what you are typing is valid.

Compile your code. The compiler will give you a huge number of errors, and you should fix as many as you can. You will have to repeat that process quite a few times before you have fixed all the errors you are able to fix.

Then, and only then, come back here and people will help you to move on.

I think you need to add if this is the usual homework assignment. Which was:

Implement the perfect hashed data structure and provide Java application that demonstrates that its four basic operation methods function properly. Your application should store nodes for a stadium ticket application where the ticket numbers range from 2000 to 100,000 for a 60,000 seat stadium. The ticket number will be the key field and the nodes will also store the purchaser's name.

It certainly sounds like that assignment. The answer is on the web but here, this is handled another way.
Read https://www.daniweb.com/programming/threads/435023/read-this-before-posting-a-question

@rproffitt Would I need to put all four methods into a separate class so the data fields are not shown?

@rproffitt Here's my work that I did on my own, but I am having trouble with implementing some of the methods in perfect hash class. I have looked in 4 or 5 different data structures and algorithm books, but I haven't been able to find any book that explains how I could do these methods using the perfect hash structure. But I still I have tried to implement from what I do know now. Do you think you could help further now that I have put in some effort? Please? Here's what I have done so far.

import java.util.Scanner; 

public class StadiumTickets
{

     int ticketNumber; // keyfield

     String purchaserName; 

  public void input()
  {

     Scanner input= new Scanner(System.in);

      // key values ranges between 2000 to 100,000 

      System.out.print("Please enter a ticket number between 2000 to 100,000: "); 

      // a variable to hold the answer

       ticketNumber= input.nextInt(); 

        // error checking to make sure the user doesn't enter a key outside of the lower or upper ranges

       if(ticketNumber < 2000 && ticketNumber > 100000) 
        System.out.println("This number is not a valid entry to input into the structure.");
     }

  public StadiumTickets(int ticketNumber, String purchaserName)
  {

       this.ticketNumber= ticketNumber; 
       this.purchaserName= purchaserName; 
   } 

   public StadiumTickets deepCopy()
   { 

      StadiumTickets clone= new StadiumTickets(ticketNumber, purchaserName); 
       return clone; 
   }  
}

public class PerfectHash
{

  private StadiumTickets[] data; 

  public boolean insert(StadiumTickets newStadiumTicket)
  {
      // the direct insert hashed algorithm 

       pseudoKey = preProcessing(targetKey); 
       ip = pseudoKey; // direct hashing function 

       // insert the new node
        data[ip] = newStadiumTicket.deepCopy(); 
  }

  public StadiumTickets fetch(String targetKey)
  {

    // the fetch algorithm 
    // access the primary storage area 

     pseudoKey = preprocessing(targetKey); 
     ip = pseudoKey; // direct hashing function 
    if(data[ip]== null)
     { return null; 

     } 
     else
     {
        return data[ip].deepCopy(); 
     }
   } 

   public boolean delete(String targetKey)
   { 
      // the delete direct hashed algorithm 

      // access the primary storage area 
         pseudoKey = preprocessing(targetKey); 
       ip = pseudoKey; // direct hashing function 

      if(data[ip]== null)
      { 
         return false; 
       } 
      else 
      { 
        data[ip]= null;                      
        return true; 
      }
    } 
    public boolean update(String targetKey, StadiumTickets newStadiumTicket)
    {
        // the update direct hashed algorithm 

       if(delete(targetKey) == false)
           return false; 
       else 
        {
           insert(newStadiumTicket) 
             return true;
         }
     }

}

Remember that since this is an assignment, most folk will not supply you with code. They might help you understand why a line of code or method fails but you get to write all the code. If it's something like "compute Pi" I might reply with atan(1) times 4 is a good way to get the max digits on a platform but beyond that, your assignment should be your code.

That out of the way, I found problem solving skills to be key to such problems. Often the person with the problem has not taken a step back to examine what the problem is, and then considered "can I break this problem into smaller problems."

That is, you might be considering the problem to be the assignment rather than the 100+ problems it presented for you to solve.

Crossposted here

@JameCherrill. I think they crossposted elsewhere and the mods there locked it due to forum rules there.

@Deamanjoe. The reason for you coding this for your assignment is simple. It shows you have learned what was taught to this point. It's OK to fail here since if you did supply code from the web such as what is at chegg.com then you may not have learned what is needed to pass the course. This is fine since without the failure the teacher won't know that you needed more instruction or help.

@rproffitt Can you please start referring people to some really good online resources for these types of situations?

@Deamanjoe. I took a stab at your assignment googling the your top question. Now while that finds the assignment completed on a few sites, that's going to not get you there as far as what happens next. What happens next is the next assignment may be building on what you learn here.

There are free courses in Java such as https://www.google.com/search?q=online+java+courses
And there is plenty of discussion, docs and books online. But here's the hard part. You have to research, read and build your skills. Anyone that writes the code for you deprives you of learning not only how to code this assignment but how to do research today.

commented: Absolutely right +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.