0

Can someone help solving this. I want to print all the object once please

public class Student {
    private static String firstName;
    private static String lastName;
    private static int studentId;
    private static String major;
    private static double balance;


    public Student (String fName, String lName,int id,String mjr,double blce)   {
        firstName = new String(fName);
        lastName = new String(lName);
        studentId = id;
        major = new String(mjr);
        balance = blce;
    }




    public String toString ()   {
        return firstName + "\t" + lastName + "\t" + studentId + "\t" + major + "\t$" + balance;
    }


    public boolean equals (Object obj)  {
        if (obj instanceof Student) {
            Student collegeStud = (Student) obj;
            return (this.firstName.equals(collegeStud.firstName));
        } else
            return false;
    }

    public static String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public static String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public static int getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    public static String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public static double getBalance() {
        return balance;
    }
    /*
     * .commmm
     */

    public void setBalance(double balance) {
        this.balance = balance;
    }



    public static void main (String[] args) {

        Student Mike = new Student ("Mike","Versace", 99, "CS",0.00);
        Student John = new Student ("John","Sling" ,97, "Maths", 20.00);
        Student Bob = new Student ("Bob","Tomson" ,57, "Physic",5.00);

        System.out.println (Mike.toString() + "\n" + John.toString());

        if (Mike.equals(John))
        System.out.println ("Mike is John");
        else
            System.out.println ("Mike is NOT John");
    }
}

import java.io.ObjectInputStream.GetField;


public class StudentList {
    private  int numberOfStudents=0;
    private Student[] studentListArray;
    //private int studentCount = 0;



    StudentList ()  {
        numberOfStudents=0;
        studentListArray = new Student[100];    


    }
    public void createStudent(String firstName, String lastName,int studentId, String major, double balance){
        Student collegeStud = new Student(firstName, lastName, studentId, major, balance);
        addStudent(collegeStud);
        numberOfStudents++;

    }


    public void addStudent (Student collegeStud)    {
        studentListArray[numberOfStudents++]=new Student(collegeStud.getFirstName(), collegeStud.getLastName(),
            collegeStud.getStudentId(), collegeStud.getMajor(),collegeStud.getBalance());
    }


    public String toString()    {
        String result = "";
        for (int i=0; i<numberOfStudents; i++)  {
            result += studentListArray[i].toString() + "\n";
        }
        return result;
    }

    public Student[] getList()  {
        return studentListArray;
    }


    public int listSize()   {
        return numberOfStudents;
    }



public Student searchForStudent (String firstName){
    int index = 0;
    while (index < numberOfStudents) {
        if (studentListArray[index].equals(new Student(Student.getFirstName(),Student.getLastName(),Student.getStudentId(),Student.getMajor(),Student.getBalance()))) {

            return studentListArray[index];

        }
        index++;
    }
    return null;        
    }



    public static void main(String args[])  {
        StudentList theList = new StudentList();
        theList.addStudent (new Student ("John","Sling" ,97, "Maths", 20.00));
        theList.addStudent (new Student ("Mike","Versace", 99, "CS",0.00));
        theList.addStudent (new Student ("Bob","Tomson" ,57, "Physic",5.00));
        //theList.createStudent(new Student(Student.getFirstName(),Student.getLastName(),Student.getStudentId(),Student.getMajor(),Student.getBalance()));
        //theList.searchForStudent(new String());


        System.out.println (theList.toString());



    }


}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Semm
  • 1
  • 1
  • 3

1 Answers1

5

The problem is that you marked your fields as static. Remove it and the method will work as expected.

public class Student {
    //non-static fields
    private String firstName;
    private String lastName;
    private int studentId;
    private String major;
    private double balance;
    //similar for getters, setters and toString method
}

Static members are shared amongst all objects in a class rather than being one per object. Hence each new object you create is overwriting the data of the previous one.

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 1
    Good catch, L, I've just added a small explanation as the answer should be self-contained even if every other site on the internet were to disappear. – paxdiablo Sep 05 '14 at 00:39
  • @paxdiablo thanks for the editing. Yes, now it is self-contained. – Luiggi Mendoza Sep 05 '14 at 00:41
  • I revoked the Static and I having problem with the search method public Student searchForStudent (String firstName){ int index = 0; while (index < numberOfStudents) { if (studentListArray[index].equals(new Student(Student.getFirstName(),Student.getLastName(),Student.getStudentId(),Student.getMajor(),Student.getBalance()))) { return studentListArray[index]; } index++; } return null; } – Semm Sep 05 '14 at 00:45
  • @Semm that's because you're using a bad implementation for your search. Instead of creating a new `Student` while searching, just access to the right getter and use `equals` method to compare against the `firstName` parameter. It shouldn't be hard to implement. – Luiggi Mendoza Sep 05 '14 at 00:46
  • @Semm `studentListArray[index].getXyzAbc().equals(firstName)` where `getXyAbc` should be the name of the proper getter. Come on, it's not that hard. Also, if I post the relevant code I'll be doing your homework. – Luiggi Mendoza Sep 05 '14 at 00:55
  • Thanks for you help.. and your time.. I got it! – Semm Sep 05 '14 at 00:57