• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

problem with hashCode()

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI
i think the problem i have is with my hashCode method can anybody help me out with this? basically i have three files Student, Storage and one for the main method. It is suppose to accept user input for name and course via JOptionPane until the maximum size is met then display the list of names and courses as the end result in the student class there has to be a method to calculate the hash from the sudents name changed to upperCase it should accept the size of the Students array. and in the Storage file there is a method to addStudent to the Student object array in the method i have to get the hash index from the Student objcet passed as an argument which represents it's prefered array position in the student object array. I am struggling to find a solution for this and would much appriciate any help with this, here are the files i have at the moment:

public class Student
{

private String name;
private String course;


Student(String studentName, String studentCourse)
{
name = studentName;
course = studentCourse;
}

public int hashCode(int maxNum)
{
int addASCII= name.charAt(0);
int hash=0;
for(int i=0; i<name.length(); i++)
{
addASCII = name.charAt(i);
}
hash = addASCII % maxNum;
return hash;
}

public String getName()
{
return name;
}
public String getCourse()
{
return course;
}
}

public class Storage
{

private Student[] students;

Storage(int maxNum)
{
students = new Student[maxNum];
}

public void addStudent(Student studentObj)
{
int hash = studentObj.hashCode();
if(hash>=0 && hash<students.length)
{

students[hash]= studentObj;
}
}
public int getArraySize()
{
return students.length;
}
public void display()
{
System.out.println("Student Name\tStudent Course");
for(int i=0; i<getArraySize(); i++)
{
System.out.println(students[i]);
}
}

}

import javax.swing.*;
public class StudentTestDrive
{

/**
* @param args
*/
public static void main(String[] args)
{
Storage store = new Storage(3);

for(int i=0; i<store.getArraySize(); i++)
{
String name = JOptionPane.showInputDialog("Please enter a name!");
String course = JOptionPane.showInputDialog("Please enter a course!");
Student newStudent = new Student(name, course);
store.addStudent(newStudent);
}
store.display();
}

}

Thank you
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You didn't mention what symptom you're seeing, but I can see one problem. The hashCode() method you call in addStudent() has no args. The version you provide in Student has an int arg. Probably that's what you intended to call, but instead you're getting the zero-args version that Student inherits from Object.
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Philip
i have now put an int arg in the hasCode(3) but when i display i get null, null, and the package name with @1bc4459
im having trouble working out exactly how the hash index works when i try searching the net for some examples i only seem to find examples of hashTables which i am told i dont need for this excercise.
Thank you for your reply.
 
Let me tell you a story about a man named Jed. He made this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic