Hi
When i run this program it only prints to the screen the last name and course enterd, it all compiles ok, could somebody have a look at the code for me and if possible point out where i have gone wrong.
public class Student
{
// Private members
private
String name;
private String course;
// constructor for new student object
public Student(String newStudent, String newCourse)
{
name = newStudent;
course = newCourse;
}
// methods for retrieving name and course
public String getName()
{
return name;
}
public String getCourse()
{
return course;
}
}
public class Storage
{
// private members
private Student[] students;
private int maxNum;
// constructor for new student array
public Storage(int numOfStudents)
{
students = new Student[numOfStudents];
maxNum = numOfStudents;
}
//method for adding student to array
public void addStudent(Student studentObject)
{
for(int i=0; i<students.length; i++)
students[i]= studentObject;
}
public int getSize()
{
return students.length;
}
//display the results of the students list
public void displayAll()
{
System.out.println("Studnets names\tStudents courses");
for(int i=0; i<getSize(); i++)
{
System.out.println(students[i].getName() + "\t\t" + students[i].getCourse());
}
}
}
import javax.swing.*;
public class Main
{
/**
* @param args
*/
public static void main(String[] args)
{
int maxNum = 5;
Storage store = new Storage(maxNum);
for(int i=0; i<store.getSize()-1; 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.displayAll();
}
}
much appriciated.