Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a Java (C++ is not allowed) program that creates and maintains a hashfil

ID: 3727076 • Letter: D

Question

Develop a Java (C++ is not allowed) program that creates and maintains a hashfile with the following specifications:

1) record size is fixed. The program must add student records consisting of (name, ID number and major), records gets added from keyboard input (student after student) .

2) bucket (bin) size is fixed, number of buckets is fixed (M)

3) hash function is fixed (key mod M) the key is student ID number

4) for collision use overflow area ( at the end of the file)

**I've asked this questions before and so please don't copy/paste the answer from chegg/google for it, this is the second time i've reposted the question . So Please ask if anything is ambiguous and i'll respond immediately. :)

Explanation / Answer


package com.chegg.vinoth;

import java.util.Scanner;

public class HashFile {

public static int BUCKET_SIZE = 0;
public static int NO_OF_BUCKET = 0;

public class Bucket{
  String bucketNo;
  int recordCount = 0;
  Object[] objData;
  
  Bucket(int bucketSize,String name){
   objData = new Object[bucketSize];
   bucketNo = name;
  }
  
  
  
  public String getBucketNo() {
   return bucketNo;
  }

  public void setBucketNo(String bucketNo) {
   this.bucketNo = bucketNo;
  }

  public Object[] getObjData() {
   return objData;
  }

  public void setObjData(Object[] objData) {
   this.objData = objData;
  }

  public int getRecordCount() {
   return recordCount;
  }

  public void setRecordCount(int recordCount) {
   this.recordCount = recordCount;
  }

  void incrementRecordCount() {
   recordCount++;
  }
  
  void putDataInBucket(Object obj){
  
   objData[recordCount] = obj;
  }
}

class Student{
  String name;
  int id;
  String major;
  
  Student(){
   
  }
  
  Student(String n,int id,String m){
   name = n;
   this.id = id;
   this.major = m;
  }
  
  @Override
  public String toString() {
   return name +" " +id +" " +major;
  }
}

public static void main(String[] args) {
  
  Scanner scan = new Scanner(System.in);
  System.out.print("Enter bucket size: ");
  BUCKET_SIZE = scan.nextInt();
  System.out.print("Enter no of buckets: ");
  NO_OF_BUCKET = scan.nextInt();
  
  Bucket[] bucket = new Bucket[NO_OF_BUCKET];
  //Create Overflow bucket
  Bucket overflow = new HashFile().new Bucket(100,"Overflow");
  for(int j=0;j<100;j++) {
   overflow.getObjData()[j] = new HashFile().new Student();
  }
  for(int i=0;i<NO_OF_BUCKET;i++) {
   bucket[i] = new HashFile().new Bucket(BUCKET_SIZE,"Bucket"+(i));
   for(int j=0;j<BUCKET_SIZE;j++) {
    bucket[i].getObjData()[j] = new HashFile().new Student();
   }
  }
  
  do {
   
   System.out.print("Enter Name of the student: ");
   String name = scan.next();
   System.out.print("Enter ID of the student: ");
   int id = scan.nextInt();
   System.out.print("Enter major of the student: ");
   String major = scan.next();
   
   Student stud = new HashFile().new Student(name,id,major);
   
   int bucketNo = id%NO_OF_BUCKET;
   int recordCount = bucket[bucketNo].getRecordCount();
   if(recordCount==BUCKET_SIZE) {
    int overFlowBuckCnt = overflow.getRecordCount();
    overflow.getObjData()[overFlowBuckCnt] = stud;
    overflow.incrementRecordCount();
    
   }else {
    bucket[bucketNo].getObjData()[recordCount] = stud;
    bucket[bucketNo].incrementRecordCount();
   }
   printHashFile(bucket, overflow);
   
  }while(true);
  

}

public static void printHashFile(Bucket[] bucket,Bucket overFlow) {
  for(int i=0;i<NO_OF_BUCKET;i++) {
   System.out.println(bucket[i].getBucketNo());
   int recordCount = bucket[i].getRecordCount();
   for(int j=0;j<recordCount;j++) {
    Student stud = (Student) bucket[i].getObjData()[j];
    //System.out.println(bucket[i].getBucketNo());
    System.out.println(stud.toString());
   }
   System.out.println(" ");
  }
  System.out.println(" ");
  System.out.println(overFlow.getBucketNo());
  int overFlowRecCnt = overFlow.getRecordCount();
  for(int i=0;i<overFlowRecCnt;i++) {
   Student stud = (Student) overFlow.getObjData()[i];
   //System.out.println(bucket[i].getBucketNo());
   System.out.println(stud.toString());
  }
}

}

=======================================
Output
Enter bucket size: 3
Enter no of buckets: 3
Enter Name of the student: Vinoth
Enter ID of the student: 8
Enter major of the student: MCA
Bucket0


Bucket1


Bucket2
Vinoth 8 MCA


Overflow
Enter Name of the student: Gayathri
Enter ID of the student: 7
Enter major of the student: Medicine
Bucket0


Bucket1
Gayathri 7 Medicine


Bucket2
Vinoth 8 MCA


Overflow
Enter Name of the student: John
Enter ID of the student: 9
Enter major of the student: Science
Bucket0
John 9 Science


Bucket1
Gayathri 7 Medicine


Bucket2
Vinoth 8 MCA


Overflow
Enter Name of the student: Peter
Enter ID of the student: 9
Enter major of the student: Aero
Bucket0
John 9 Science
Peter 9 Aero


Bucket1
Gayathri 7 Medicine


Bucket2
Vinoth 8 MCA


Overflow
Enter Name of the student: Christiano
Enter ID of the student: 9
Enter major of the student: Football
Bucket0
John 9 Science
Peter 9 Aero
Christiano 9 Football


Bucket1
Gayathri 7 Medicine


Bucket2
Vinoth 8 MCA


Overflow
Enter Name of the student: Leo
Enter ID of the student: 9
Enter major of the student: Football
Bucket0
John 9 Science
Peter 9 Aero
Christiano 9 Football


Bucket1
Gayathri 7 Medicine


Bucket2
Vinoth 8 MCA


Overflow
Leo 9 Football
Enter Name of the student: