Sorry but I dont understand this.Add a method to the Student class to add Module objects to the array and call that method 4 times from main.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Sorry but I dont understand this.Add a method to the Student class to add Module objects to the array and call that method 4 times from main.
Which part don't you understand?
1)add a method? see the display method. its a method that you pass a Module object to
2)add objects to the array. See the Student class's constructor. It adds Module elements to the array
The only trick is the index to the array. The method that adds the elements to the array will be responsible for incrementing the index. The index must be a class variable so it keeps its value over calls to the add method.
Am I heading in the right direction with this?
package Assignment6; public class Student { protected String name; int id; String programme; public Module[] modules; public Student(String name,int id,String programme, int arraysize) { this.name= name; this.id= id; this.programme = programme; modules = new Module[arraysize]; } public void objectadd(String code, String title ,float result) { for(int i=0;i<4;i++) { modules[i] = new Module(code,title,result); } } public void display() { System.out.println("Name: " + name); System.out.println("ID No.: " + id); System.out.println("Programme: " + programme); for(int i=0;i<4;i++) { Module.display(modules[i]); } } public static void main(String[] args) { Student a = new Student("John Johnson",123456,"Electronic Engineering", 4); a.objectadd("EE219","OOP",57.2f); a.objectadd("EE203","Maths", 90.00f); a.objectadd("EE223","Digital Electronics",75.8f); a.objectadd("EE215","Solid State Electronics",45.7f); a.display(); } }
What happens when you execute it?
Look at the second part of post #25
Here is my code as it stands now;
package Assignment6; public class Module { protected String code; String title; float result; public Module(String acode,String atitle, float aresult) { code = acode; title = atitle; result = aresult; } public void display() { System.out.println("Module Code:" + code); System.out.println("Moule Title:" + title); System.out.println("Result:" + result + "%"); } public static void main(String[] args) { } }
package Assignment6; public class Student { protected String name; int id; String programme; public Module[] modules; public Student(String name,int id,String programme, int arraysize) { this.name= name; this.id= id; this.programme = programme; modules = new Module[arraysize]; } public void objectadd(String code, String title ,float result) { for(int i=0;i<4;i++) { modules[i] = new Module(code,title,result); } } public void display() { System.out.println("Name: " + name); System.out.println("ID No.: " + id); System.out.println("Programme: " + programme); for(int i=0;i<4;i++) { modules[i].display(); } } public static void main(String[] args) { Student a = new Student("John Johnson",123456,"Electronic Engineering", 4); a.objectadd("EE219","OOP",57.2f); a.objectadd("EE203","Maths", 90.00f); a.objectadd("EE223","Digital Electronics",75.8f); a.objectadd("EE215","Solid State Electronics",45.7f); a.display(); } }
My output is now the same as before (output the same module 4 times)!
I cant thank you enough for all your help!
Why the loop in objectadd()? It should add one object. The caller of object will call it for as many times as it wants to add a new Module to the student.
In display you should use the length of the array to control the loop and NOT have a hardcoded value of 4. In the next test there could be 6 modules added to the student.
Im sorry but I dont understand this.How will it move to the next slot in the array to add the next module?Why the loop in objectadd()? It should add one object. The caller of object will call it for as many times as it wants to add a new Module to the student.
If at any time you call the objectadd method to add an element to the array, where will it put that one element? How will it keep track of where the next empty element is located?How will it move to the next slot in the array to add the next module?
I'm thinking I have to make a counter and increment it when a module is assigned but I dont know how or where to put it?
Make it a class variable in the class where the array is. Its initial value of 0 will be where the first element is to go in the array then increment for the next element. Its value at any time will be the number of elements in the array.
Dr.HughMan (November 30th, 2011)
Finally got it to work! Thanks a million!