1

I've just started learning java and I'm trying to create an application to register students.

Based on this question how-would-i-create-a-new-object... I created a while loop to create an instance of a class.

    public class RegStudent {

    ArrayList<Student> studentList = new ArrayList<>();

    Scanner input = new Scanner(System.in);

    public void reggaStudent(int start) {

        while (start != 0) {
            String programNamn, studNamn;   
            int totalPoint, antalKurser;

            System.out.println("Vad heter programmet?");
            programNamn = input.nextLine();
            System.out.println("Vad heter studenten");
            studNamn = input.nextLine();
            System.out.println("Hur många poäng har studenten?");
            totalPoint = input.nextInt();
            System.out.println("Hur många kurser är studenten registrerad på?");
            antalKurser = input.nextInt();

            // Add student to list of students
            studentList.add(new Student(totalPoint, antalKurser, 
                                        programNamn, studNamn));
            System.out.println("Vill du registrera in en fler studenter? "
                                + "Skriv 1 för ja och 0 för nej");
            start = input.nextInt();
            input.nextLine();
        } // End of whileloop

    }
}

The class is:

public class Student {
    private int totalPoint;
    private int antalKurser;
    private String programNamn;
    private String studNamn;
    private static int counter;

    public Student(int totalPoint, int antalKurser, String program, String studNamn) {

        this.totalPoint = totalPoint;
        this.antalKurser = antalKurser;
        this.programNamn = program;
        this.studNamn = studNamn;
        counter++;
    }

    public int getTotalPoint() {
        return totalPoint;
    }

    public void setTotalPoint(int totalPoint) {
        this.totalPoint = totalPoint;
    }

    public int getAntalKurser() {
        return antalKurser;
    }

    public void setAntalKurser(int antalKurser) {
        this.antalKurser = antalKurser;
    }

    public String getProgramNamn() {
        return programNamn;
    }

    public void setProgramNamn(String programNamn) {
        this.programNamn = programNamn;
    }

    public String getStudNamn() {
        return studNamn;
    }

    public void setStudNamn(String studNamn) {
        this.studNamn = studNamn;
    }
    public static int getCount(){

        return counter;
    }
    @Override
    public String toString() {

        return String.format(" Namn: %s, Program: %s, Antal poäng: %d, "
        + "Antal kurser: %d\n ", studNamn, programNamn, totalPoint, antalKurser);
    }

}

How do I go about to get and set the instance variables in specific instance? I.e find the instances.

I understand it might be bad design but in that case I would appreciate some input on how to solve a case where i wanna instantiate an unknown number of students.

I've added a counter just to see I actually created some instances of the class.

2 Answers2

1

You simply query objects for certain properties, like:

for (Student student : studentList) {
  if (student.getProgramName().equals("whatever")) {
    some match, now you know that this is the student you are looking for

In other words: when you have objects within some collection, and you want to acquire one/more objects with certain properties ... then you iterate the collection and test each entry against your search criteria.

Alternatively, you could "externalize" a property, and start putting objects into maps for example.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Of course, thanks! I don't know why I was expecting a more complex solution. Read about maps but think I'm already over my head with this since I just started with java (and programming). –  Nov 16 '17 at 15:57
  • hm, still not really getting how to be able to for example set a new name. Been searching for an hour but can't find a solution. Don't I need to find the index of the specific element I want to set a new value? If I want to studentList.set(1, "Peter") then i need the index but I can't find a way get the index when the list is a object of a class, i.e contains several instance variables which is different types. –  Nov 16 '17 at 20:08
  • In my example - you already that "student" variable pointing to the current element in the collection. When you now invoke a setter method on that reference - the corresponding object gets updated. I hope that helps, if not drop me a comment and I will see what I can do tomorrow. – GhostCat Nov 16 '17 at 20:11
  • Lovely! I didn't understand that when the loop iterates it in a way "fetches" an instance of the class and makes it possible to just use "normal" methods to get/set/and-what-not the fields for that specific instance. I don't need to use the methods from –  Nov 16 '17 at 21:03
0
 studentList.add(new Student(totalPoint, antalKurser, 
                                    programNamn, studNamn));

You now have your Student objects in a list. I assume you have something like

List<Student> studentList = new ArrayList<>();

somewhere in your code. After you populate the list with Student objects, you can use it to find instances. You need to decide what criteria to use for a search. Do you want to find a student with a specific name? Do you want to find all students in a given program? Do you want to find students with more than a certain number of points?

Maybe you want to do each of these. Start by picking one and then get a piece of paper to write out some ideas of how you would do the search. For example, say you want to find a student with the name "Bill". Imagine that you were given a stack of cards with information about students. This stack of cards represents the list in your program. How would you search this stack of cards for the card with Bill's name on it? Describe the steps you need to take in words. Don't worry about how you will code this yet. The first step in writing a computer program is breaking the solution down into small steps. After you have a clear idea how you might do this by hand in the physical world, you can translate your description into Java code.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268