Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By christopherx

Thread: please help - don't see the right output?

  1. #1
    Zahara is offline Member
    Join Date
    Feb 2012
    Location
    Scotland, United Kingdom
    Posts
    14
    Rep Power
    0

    Default please help - don't see the right output?

    hi,

    here's my question;

    The following describe the steps to set up a simple student enrolment system.
    1. Create a Student class with a single String attribute name and a constructor that gets the name from the keyboard and a display method that displays the name.

    2. Create a StudentList class to maintain an array of Student objects. The constructor should get the size of the array from the keyboard. Write an add method that adds a new Student to the array (assuming there is space in the array) and a display method that iterates through the array displaying the student details (via a call to the display method of the Student class).

    3. Create an EnrolTest class that has a main method that instantiates a StudentList object and offers options to add a student, display students and quit.

    4. Run the program to ensure that students can be added/displayed successfully. Make the array suitably small say 3) to check that a “list full” situation can be recognized.

    and here's my answer;


    public class Student {
    private String name;

    public Student(){
    this.name=Input.getString("input name: ");
    }
    public String getName(){
    return this.name;
    }

    }

    public class Studentlist{
    private Student[] students;
    private int count;
    public Studentlist(){
    int size=Input.getInteger("input list size: ");
    this.students=new Student[size];
    }
    public void add(){
    // check space available
    if (this.count<this.students.length){
    Student students=new Student();
    boolean identifierMatches=false;
    int index=0;
    // check identifier is unique
    while (!identifierMatches && index<this.count)
    if (this.students[index++].equals(students))
    identifierMatches=true;
    if (identifierMatches)
    System.out.println("identifier already in use");
    else
    this.students[this.count++]=students;
    }
    else
    System.out.println("list full");
    }
    public void display(){
    int index=0;
    while(index<this.count)
    System.out.println(this.students[index++]);
    }
    }


    public class Enroltest{
    public static void main(String[] args){
    Studentlist students=new Studentlist();
    int option;
    do{
    System.out.println("0: quit, 1: add, 2: display");
    option=Input.getInteger("input option: ");
    switch (option){
    case 0: break;
    case 1: students.add();
    break;
    case 2: students.display();
    break;

    }
    } while (option !=0);
    }
    }

    And this is my output;

    input list size: 1
    0: quit, 1: add, 2: display
    input option: 1
    input name: zaara
    0: quit, 1: add, 2: display
    input option: 2
    Student@9304b1
    0: quit, 1: add, 2: display
    input option:

    when i choose the display option, it displays "Student@9304b1" instead of my name "zaara". Can any1 advise on what changes shud i make to get the right output please?

    thanks

  2. #2
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: please help - don't see the right output?

    For future reference, please enclose code in [code] tags!

    Java Code:
    public void display(){
    int index=0;
    while(index<this.count)
    System.out.println(this.students[index++]);
    }
    }
    So, you have this above method to display the student in a list. Where is the name stored in the student class? Are you asking for the right variable?

  3. #3
    Zahara is offline Member
    Join Date
    Feb 2012
    Location
    Scotland, United Kingdom
    Posts
    14
    Rep Power
    0

    Default Re: please help - don't see the right output?

    sorry about the tags, erm, am gonna ask the user to input the student names? Am I not asking for the right variable? am not sure, am new to java :S

  4. #4
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: please help - don't see the right output?

    That's perfectly alright buddy! Look at the little code snippet. You're printing out the Student object, but the value of the student object isn't "some name", the student object contains within it, a name. I don't want to give the answer away, because this is a crucial part of Java programming. :)
    DarrylBurke likes this.

  5. #5
    Zahara is offline Member
    Join Date
    Feb 2012
    Location
    Scotland, United Kingdom
    Posts
    14
    Rep Power
    0

    Default Re: please help - don't see the right output?

    hey, my lecturer helped me out with the answer lol thanks though :)

  6. #6
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: please help - don't see the right output?

    Ah, well I'm okay at Java, but I'm nothing compared to a guy with a doctorate! Congratulations :)

Similar Threads

  1. need help with my output
    By pds8475 in forum New To Java
    Replies: 0
    Last Post: 01-22-2011, 08:06 PM
  2. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  3. Why do I get this output?
    By Allspark in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 08:07 AM
  4. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  5. Why the output is always zero
    By mehrotra.chitij in forum New To Java
    Replies: 12
    Last Post: 04-25-2008, 04:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •