Petros Hatzivasiliou
Petros Hatzivasiliou

Reputation: 43

Objects in Linked Lists

I try to write a code which will have a basic menu with some options. These options are the following methods: AddStudent, changeName, setGrade and so on. I have created an object called Student which has a name, a grade and an age. I want to add Students in an linked list but when I use the method add it does not work. Here is my code:

import java.util.*;
class Student {  
    int age;
    int grade;
    String name;
    static LinkedList ll = new LinkedList();
    public Student (String n) {   //we create here a student with a name and an age
        name=n;
        age=0;
        grade=0;
    }
//-------------------------------------------------------------------------
    public void p(String x) {
        System.out.println(x);
    }

    public void addStudent() {
        Scanner s = new Scanner(System.in);
        p("Enter the name that you want");
        String f = s.nextLine();
        Student a = new Student(f);
        ll.add(a);
    }

    public void changeName() {                           //this method is to change the name of a student
        Scanner s = new Scanner(System.in);
        p("Enter whose name you want to change");  
        String c = s.nextLine();
        p("Enter the name that you want");
        String b = s.nextLine();

    }

    public void setGrade(Student a) {                 //this method is to put the student's grade
        Scanner s = new Scanner(System.in);
        p("Enter the grade that you want");
        int g = s.nextInt();
        a.grade=g;
    }

    public void setAge(Student a) {                 //This method is to put the student's grade
        Scanner s = new Scanner(System.in);
        p("Enter the age that you want");
        int h = s.nextInt();
        a.age=h;
    }

    public String getName(Student a) {
        return a.name;
    }

    public int getAge(Student a) {
        return a.age; 
    }

    public int getGrade(Student a) {
        return a.grade; 
    }
}

The problem is at the method of addStudent. Is there any other ways,as well, with which I can make the same project?

Upvotes: 0

Views: 1163

Answers (2)

lkq
lkq

Reputation: 2366

This code is kind of messy because you include some code that shouldn't appear in Student class. For example, the addStudent method is not static and in order to call it, you need to first instantiate an instance of Student and call the method on it. However, the method is trying to ask user to input the information of a new Student instance, which is a bad design.

So, keep the Student class only do what it should do. For your case, Student only need to store its age, grade and name fields and define constructors to initialize these fields, and optional getter and setter methods to set and retrieve these fields upon your needs.

You will need a 'manager' class which manages your application. This class will keeps track of a list of students, and asks users to input information of a new student and then initialize the student instance and put it in the list. This Manager class can even manage an UI which you need to take user input or display information to the user. So, it will be this class's responsibility to provide a addStudent method.

Student class itself should know nothing about your application's logic like it may be a course selection program or something else. It only manages its own information while some manager class will take care of the application's logic.

Upvotes: 1

pczeus
pczeus

Reputation: 7867

Think about this logically. You have a Student class that represents a single Student. Why would a Student have a List of Student's? That makes no sense.

Wouldn't you have a program like Course or something that would hold the list of Students? That is where your List belongs. And don't use static unless you have a compelling reason (rare).

Here is a start for the Course class, that uses the Student to store student information and stores it in your LinkedList within the Course. You still need to implement the findStudent method and probably a method to print the List:

Class Course:

import java.util.LinkedList;
import java.util.Scanner;

public class Course {
    LinkedList ll = new LinkedList();
    Scanner s = new Scanner(System.in);

    public void addStudent() {
        p("Enter the name that you want");
        String f = s.nextLine();
        Student a = new Student(f);
        ll.add(a);
    }

    public void changeName() {                           //this method is to change the name of a student
        Student student = findStudent();
        p("Enter the name that you want");
        String newName = s.nextLine();
        //student.setName(newName);
    }

    public void setGrade() {                 //this method is to put the student's grade
        Student student = findStudent();
        p("Enter the grade that you want");
        int grade = s.nextInt();
        //student.setGrade(grade);
    }

    public void setAge() {                 //This method is to put the student's grade
        Student student = findStudent();
        p("Enter the age that you want");
        int age = s.nextInt();
       student.setAge(age);
    }

    public Student findStudent(){
        p("Which student did you want to change? Please enter their name:");
        String name = s.nextLine();
        //Find student in the list - left for the author
        Student student = null;
        return student;
    }

    //-------------------------------------------------------------------------
    public void p(String x) {
        System.out.println(x);
    }

    public static void main(String[] args) {
        Course course = new Course();
        course.addStudent();
        course.addStudent();
        course.changeName();
        course.setGrade();
    }
}

And the modified Student class:

import java.util.*;

public class Student {
    int age;
    int grade;
    String name;

    public Student (String n) {   //we create here a student with a name and an age
        name=n;
        age=0;
        grade=0;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName(Student a) {
        return a.name;
    }

    public int getAge(Student a) {
        return a.age;
    }

    public int getGrade(Student a) {
        return a.grade;
    }

    @Override
    public String toString(){
        return "Student(name:" + name + ", age:" + age + ", grade:" + grade + ")";
    }
}

Upvotes: 1

Related Questions