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