Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java constructor query

Options
  • 14-10-2016 10:55pm
    #1
    Registered Users Posts: 988 ✭✭✭


    Need help, am trying to understand constructors in java.
    Create a class called MyClass; students this should also be private.
    The class should have a constructor that initializes the variable students; it should also have methods to access and reset the value of the students variable, and a method to print the entire class/the array of students.
    It should also have a method that allows you to find a student by name.


Comments

  • Registered Users Posts: 1,148 ✭✭✭punk_one82


    manutd wrote: »
    Need help, am trying to understand constructors in java.
    Create a class called MyClass; students this should also be private.
    The class should have a constructor that initializes the variable students; it should also have methods to access and reset the value of the students variable, and a method to print the entire class/the array of students.
    It should also have a method that allows you to find a student by name.

    What problems are you having with constructors? You've just listed a homework assignment I'm guessing, so what have you tried so far and what errors are you getting? Can you show your code?


  • Registered Users Posts: 1,463 ✭✭✭Anesthetize


    Is it that time of the year again? :)


  • Closed Accounts Posts: 5,482 ✭✭✭Hollister11


    Is it that time of the year again? :)

    OP, this was me 2 years ago.

    Use stackoverflow.com, it's the best development forum out there,


  • Registered Users Posts: 2,089 ✭✭✭henryporter


    Amazing how these lecturers persist in asking students to do assignments without giving them any grounding in the basics. I guess they are of the opinion that the student will go off and research it themselves (which in a way asking on boards is :-) Check out the new boston on YouTube - he'll explain it all to you OP


  • Registered Users Posts: 988 ✭✭✭manutd


    I have got the following code
    public class MyClass {
    
    private int nostudents;
          private Student [] students;
    
    public MyClass (Student [] ss){
                 students = ss;
                  nostudents = ss.length;
    }
    
    public void printMe (){
        System.out.println("Enter student name: "+ nostudents);
           for(int i=0; i<students.length;i++) {
             System.out.print(students[i].getAge() + "\t");
             System.out.println(students[i].getName());
    }
    }
    
    public void searchMe(){
                         ???????????????
    }
    
    }
    
    

    How do i go about the method that allows you to find a student by name.
    I also have the tester class working to test the above functions.
    Not looking for people to post the code, but pointers.


  • Advertisement
  • Registered Users Posts: 1,463 ✭✭✭Anesthetize


    For the search method you will be passing in a students name. You will need to figure out how to check if a Student with that name exists in the students array, and how to return that Student.


  • Registered Users Posts: 988 ✭✭✭manutd


    For the search method you will be passing in a students name. You will need to figure out how to check if a Student with that name exists in the students array, and how to return that Student.
    Do i need to get a user input for a student name to search for or input name into the code. I have the search function working and returning if a student exists or not.
    Also to access and reset the value of the students variable is this using getters and setters


  • Registered Users Posts: 1,463 ✭✭✭Anesthetize


    manutd wrote: »
    Do i need to get a user input for a student name to search for or input name into the code. I have the search function working and returning if a student exists or not.
    Also to access and reset the value of the students variable is this using getters and setters
    Depends on what your assignment is asking you to do.


  • Registered Users Posts: 988 ✭✭✭manutd


    How do you access and reset the value of the students variable, is it by using getters and setters.


  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    Amazing how these lecturers persist in asking students to do assignments without giving them any grounding in the basics. I guess they are of the opinion that the student will go off and research it themselves (which in a way asking on boards is :-) Check out the new boston on YouTube - he'll explain it all to you OP

    Im sure the basics are explained but the students either were not in the class, missed it, or didnt understand it.

    Lecturers/Tutors cant be expected to hand feed pupils and explain it multiple times to everyone in the class.
    Students need to learn this stuff on their own time to fill in the gaps and reinforce their knowledge.

    Just like if someone was studying Law the student is not expected to learn everything about law just from the lecturers, they need to study in their own time.


  • Advertisement
  • Registered Users Posts: 2,089 ✭✭✭henryporter


    Im sure the basics are explained but the students either were not in the class, missed it, or didnt understand it.

    Lecturers/Tutors cant be expected to hand feed pupils and explain it multiple times to everyone in the class.
    Students need to learn this stuff on their own time to fill in the gaps and reinforce their knowledge.

    Just like if someone was studying Law the student is not expected to learn everything about law just from the lecturers, they need to study in their own time.

    I would have thought explaining what a constructor is falls within the realm of basics that should be conveyed to the students by the lecturer.

    It's not like a point of law buried deep in some historic case that students should rightly have to investigate by themselves.

    What you're suggesting is that lecturers should tell the students to find a book on Java, figure it all out by themselves and then come back next May to do an exam on it, without making a nuisance of themselves by asking the lecturers to do their jobs properly.


  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    I would have thought explaining what a constructor is falls within the realm of basics that should be conveyed to the students by the lecturer.

    It's not like a point of law buried deep in some historic case that students should rightly have to investigate by themselves.

    What you're suggesting is that lecturers should tell the students to find a book on Java, figure it all out by themselves and then come back next May to do an exam on it, without making a nuisance of themselves by asking the lecturers to do their jobs properly.

    No, im suggesting that the lecturer should explain this to the students.
    But if the student just isnt grasping the concept then they need to learn it on their own time.

    The lecturer cannot become a 1 to 1 tutor for each student.

    3rd level education assumes the person is mature enough to grasp concepts quickly or spend further time learning what they didnt understand in classes.


  • Registered Users Posts: 6,252 ✭✭✭Buford T Justice


    manutd wrote: »
    How do you access and reset the value of the students variable, is it by using getters and setters.

    Sort of.....
    class testClass{
    	private noOfStudents;
    
    	public void resetNoOfStudents(){
    		noOfStudents = 0;
    	}
    
    	public int getNoOfStudents(){
    		return noOfStudents;
    	}
    }
    


  • Registered Users Posts: 6,252 ✭✭✭Buford T Justice


    No, im suggesting that the lecturer should explain this to the students.
    But if the student just isnt grasping the concept then they need to learn it on their own time.

    The lecturer cannot become a 1 to 1 tutor for each student.

    3rd level education assumes the person is mature enough to grasp concepts quickly or spend further time learning what they didnt understand in classes.

    If I might interject here........

    Currently in Y2 for programming and the amount of spoon feeding that is going on is ridiculous. We're nearly 6 weeks into Semester 3 and a lot of the kids are still struggling with basic concepts that were covered in Semester 2, or even 1!!!! Instead of moving forward with the course we still do revision on a lot of stuff...... Mainly because there no one bothers to do any work outside of class - at all. Its no surprise they haven't a clue really.
    3rd level education assumes the person is mature enough to grasp concepts quickly or spend further time learning what they didnt understand in classes.
    They don't bother. they just sit there like mutes saying nothing generally - if they turn up for class


  • Closed Accounts Posts: 5,482 ✭✭✭Hollister11


    If I might interject here........

    Currently in Y2 for programming and the amount of spoon feeding that is going on is ridiculous. We're nearly 6 weeks into Semester 3 and a lot of the kids are still struggling with basic concepts that were covered in Semester 2, or even 1!!!! Instead of moving forward with the course we still do revision on a lot of stuff...... Mainly because there no one bothers to do any work outside of class - at all. Its no surprise they haven't a clue really.


    They don't bother. they just sit there like mutes saying nothing generally - if they turn up for class

    Same in my college. I'm in year 3 and the amount of people who don't know what they are writing is incredible. Stack overflow copy and paste and (hopefukly) it works. That's all well and good of it works, but when it doesn't they don't even alter it, just copy another example.


  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    If I might interject here........

    Currently in Y2 for programming and the amount of spoon feeding that is going on is ridiculous. We're nearly 6 weeks into Semester 3 and a lot of the kids are still struggling with basic concepts that were covered in Semester 2, or even 1!!!! Instead of moving forward with the course we still do revision on a lot of stuff...... Mainly because there no one bothers to do any work outside of class - at all. Its no surprise they haven't a clue really.


    They don't bother. they just sit there like mutes saying nothing generally - if they turn up for class

    I totally agree.

    There is simply no way that you can learn programming from attending classes alone even if you put in 100% attendance. You need to put in the time yourself at home.

    I wasn't a model student by any means, but i took assignments as guides on what i should know and spent my time outside of class learning the concepts that the assignment contained, reading books and writing my own code/projects.

    One of the great things i enjoyed about my development course was our Maths lecturer.
    He would give us some maths problems to solve for bonus marks every week and we could complete them on paper or by writing a program to solve the problem. I always found it strange that the majority of the class would submit there answers on paper rather than choosing to write software to solve the problem.


  • Registered Users Posts: 1,463 ✭✭✭Anesthetize


    Sort of.....
    class testClass{
    	private noOfStudents;
    
    	public void resetNoOfStudents(){
    		noOfStudents = 0;
    	}
    
    	public int getNoOfStudents(){
    		return noOfStudents;
    	}
    }
    
    I highly doubt that's what he means by reset. It doesn't make sense why you would just reset the number of students back to 0. I'm sure it's just a simple setter method to set noOfStudents to a new value.


  • Registered Users Posts: 6,252 ✭✭✭Buford T Justice


    I highly doubt that's what he means by reset. It doesn't make sense why you would just reset the number of students back to 0. I'm sure it's just a simple setter method to set noOfStudents to a new value.
    what does reset mean to you?


  • Registered Users Posts: 1,463 ✭✭✭Anesthetize


    what does reset mean to you?
    Ignore my previous post. Since it's asking to 'reset' students, which is an array, it can only mean to reset the students array to a new value. It's asking for a simple getter and setter for the students variable. There's no need for the noOfStudents variable at all, it's redundant. The number of students can be retrieved by getting the length of the students array, if it's even required that is.

    This is my rough idea of the class:
    public class MyClass {
    
        private Student[] students;
    
        public MyClass(Student [] ss){
            students = ss;
        }
    
        public Student[] getStudents() {
            return students;
        }
    
        public void setStudents(Student[] ss) {
            students = ss;
        }
    
        public int getNumberOfStudents() {
            return students.length;
        }
    
        public void printMe(){
            System.out.println("Enterent name: " + nostudents);
             for (int i = 0; i < students.length; i++) {
                  System.out.print(students[i].getAge() + "\t");
                  System.out.println(students[i].getName());
            }
        }
    
        public void searchMe(){
            // search code
        }
    
        ...
    }
    


  • Registered Users Posts: 1,148 ✭✭✭punk_one82


    manutd wrote: »
    I have got the following code
    public class MyClass {
    
    private int nostudents;
          private Student [] students;
    
    public MyClass (Student [] ss){
                 students = ss;
                  nostudents = ss.length;
    }
    
    public void printMe (){
        System.out.println("Enter student name: "+ nostudents);
           for(int i=0; i<students.length;i++) {
             System.out.print(students[i].getAge() + "\t");
             System.out.println(students[i].getName());
    }
    }
    
    public void searchMe(){
                         ???????????????
    }
    
    }
    
    

    How do i go about the method that allows you to find a student by name.
    I also have the tester class working to test the above functions.
    Not looking for people to post the code, but pointers.

    You're already iterating through the array to print out all the students names. Take the logic you applied there and check if a name parameter matches the name of the current student in the array.


  • Advertisement
Advertisement