0

I have created a linked list with objects. The object is called Student and has three parameters: name, age, grade. Here are my classes.

Link

class Link {
    public Object s;
    public Link next;

    public Link(Object Student) {
        s=Student;
        next=null;
    }
}

Student

class Student {
    private static String name;
    private static int age;
    private static int grade;

    public Student (String n, int a, int g) {
        setName(n);
        setGrade(g);
        setAge(a);
    }

    public static void setName(String n) {
        name=n;
    }

    public static void setGrade(int g) {
        grade=g;
    }

    public static void setAge(int a) {
        age=a;
    }

    public int getAge() {
        return age;
    }

    public int getGrade() {
        return grade; 
    }

    public static String getName() {
        return name;
    }

    public String toString() {
        return "Name: " +name+ " Age: " +age+ " Grade: " +grade; 
    }
}

List

import java.util.*;
class List {
    private static LinkedList<Student> ll = new LinkedList<Student>();
    private static Link first;
    public List() {
        first=null;
    }

    public static void addStudent() {
        Scanner s = new Scanner(System.in);
        System.out.println("What is the student's name;");
        String n = s.nextLine();
        System.out.println("What is the student's age;");
        int b = s.nextInt();
        System.out.println("What is the student's grade;");
        int g = s.nextInt();
        Student nStudent = new Student(n, b, g);
        Link newStudent = new Link(nStudent);
        newStudent.next=first;
        first=newStudent;
        ll.add(nStudent);
    }

    public static Link findStudent() {
        Scanner sc = new Scanner(System.in);
        System.out.println("What is the student's name?");
        String n = sc.nextLine();
        Link current = first;
        String name = Student.getName();
        while (name!= n) {
            if (current.next==null)
                return null;
            else
                current=current.next;
        }
        return current;
    }

    public static void change () {
        Scanner sc = new Scanner(System.in);
        Link h = findStudent();
        System.out.println("If you want to change his name press 1;");
        System.out.println("If you want to change his age press 2;");
        System.out.println("If you want to change his grade press 3;");
        int l = sc.nextInt();
        if (l==1) {
            System.out.println("What name do you want the student to have?");
            String newname = sc.nextLine();
            Student.setName(newname);

        } else if (l==2) {
            System.out.println("What age do you want the student to have?");
            int j = sc.nextInt();
            Student.setAge(j);
        }
        else if (l==3) {
            System.out.println("What grade do you want the student to have?");
            int e = sc.nextInt();
            Student.setGrade(e);
        }
    }

    public static void show() {
        System.out.println(ll);
    }
}

Course

import java.util.Scanner;
public class Course {
    public static void main (String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int v = sc.nextInt();
        if (v==1) {
            List.addStudent();
        }else if (v==2) {
            List.change();
        }else if (v==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int a = sc.nextInt();
        if (a==1) {
            List.addStudent();
        }else if (a==2) {
            List.change();
        }else if (a==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int r = sc.nextInt();
        if (r==1) {
            List.addStudent();
        }else if (r==2) {
            List.change();
        }else if (r==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int c = sc.nextInt();
        if (c==1) {
            List.addStudent();
        }else if (c==2) {
            List.change();
        }else if (c==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int d = sc.nextInt();
        if (d==1) {
            List.addStudent();
        }else if (d==2) {
            List.change();
        }else if (d==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int x = sc.nextInt();
        if (x==1) {
            List.addStudent();
        }else if (x==2) {
            List.change();
        }else if (x==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int h = sc.nextInt();
        if (h==1) {
            List.addStudent();
        }else if (h==2) {
            List.change();
        }else if (h==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int p = sc.nextInt();
        if (p==1) {
            List.addStudent();
        }else if (p==2) {
            List.change();
        }else if (p==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int m = sc.nextInt();
        if (m==1) {
            List.addStudent();
        }else if (m==2) {
            List.change();
        }else if (m==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int y = sc.nextInt();
        if (y==1) {
            List.addStudent();
        }else if (y==2) {
            List.change();
        }else if (y==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int q = sc.nextInt();
        if (q==1) {
            List.addStudent();
        }else if (q==2) {
            List.change();
        }else if (q==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int w = sc.nextInt();
        if (w==1) {
            List.addStudent();
        }else if (w==2) {
            List.change();
        }else if (w==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int f = sc.nextInt();
        if (f==1) {
            List.addStudent();
        }else if (f==2) {
            List.change();
        }else if (f==3) {
            List.show();
        }
    }
}

You will notice that in the Course class, I have written the same code multiple times but that is because I could not find a way with which after I execute a command, I would get back to the main menu and the content of the list would not be lost. Apparently, my add method is wrong and when I execute the program the output is this:

Output

What do you want to do?
To add a student press 1
To change something press 2
To see your students press 3
1
What is the student's name;
Tom
What is the student's age;
16
What is the student's grade;
90
What do you want to do?
To add a student press 1
To change something press 2
To see your students press 3
1
What is the student's name;
Eric
What is the student's age;
16
What is the student's grade;
91
What do you want to do?
To add a student press 1
To change something press 2
To see your students press 3
3
[Name: Eric Age: 16 Grade: 91, Name: Eric Age: 16 Grade: 91]
What do you want to do?
To add a student press 1
To change something press 2
To see your students press 3

Why when I add a Student in the class, it duplicates?

3 Answers3

0

I think your problem, is that your Student.class is static.

private static String name;
private static int age;
private static int grade;

This part tells java, that you only need one variable/Class. So your Linked list just stores 2 reference to the same static fields.

You need to remove your static keyword.

JSONStatham
  • 353
  • 4
  • 18
  • But if I remove the static reference, I will have other multiple errors – Petros Hatzivasiliou Apr 12 '16 at 21:47
  • Don't worry, the IDE is helping you along the way. You have to remove the static qualifiers from the setter/getter functions aswell. for e.g.: public static void setGrade It's important, that you grasp what static really means, take your time. – JSONStatham Apr 12 '16 at 21:50
0

Because you are using static fields in your Student class.

private static String name;
private static int age;
private static int grade;

Static fields don't belong to an instance but belong to the class per se. It doesn't matter how many Student instances you create (using the new keyword), the value for the static fields will be shared between them.

You have to remove the static keyword in the Student class. Please refer to the official documentation: Understanding Class Members

user0626
  • 21
  • 2
  • But If I remove the static references, I have errors such as: **non-static method can be referenced to static content** – Petros Hatzivasiliou Apr 12 '16 at 21:53
  • You have to remove the static keyword from the getters and setters. That is because you can't access a non-static field from a static method. If you are interested in know more about the static keyword you can refer to this documentation: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – user0626 Apr 12 '16 at 21:55
0

All of the problems result from the decision to use static variables in your Student class. This means that there is only one copy of each of those variables for the entire class, no matter how many Student objects you create.

Make them non-static. This means you'll have to remove static off your getters and setters in the Student class.

In your List class, you access the getters from a static context, e.g. Student.getName(). You will need to get the object stored in the Link and cast it to a Student. Then you will be able to call getName() on it.

As an aside, the Link class should be a generic class, e.g. class Link<T>, so that when you use it in your List class, you can use it as Link<Student>, so you don't have to cast it when referring to the Object it holds (it would hold a T).

In change, you will need to do something similar -- regarding the Student referred found by findStudent, call your setters on that student object, instead of calling it in a static context.

Additionally, in findStudent, use equals to compare String values, not != or ==.

rgettman
  • 176,041
  • 30
  • 275
  • 357