public class Student
{
private int[] moduleArray;
private String studentName;
private String number;
private Module mod;
public Student(String name, String studentNumber)
{
moduleArray = new int[12];
studentName = name;
number = studentNumber;
}
public void addModule(Module m)
{
mod = m;
moduleArray.add(mod);
}
public void removeModule(Module m)
{
}
public class Module
{
private String moduleCode;
private String moduleTitle;
private int semester1;
private int semester2;
private int courseworkWeighting;
public Module(String code, String title, int sem1, int sem2, int
courseworkWeight)
{
moduleCode = code;
moduleTitle = title;
semester1 = sem1;
semester2 = sem2;
courseworkWeighting = courseworkWeight;
}
First mistake, you are trying to add "m", which is a Module into an
array of ints.....
Anyways, even if you create an array of Modules, the array does not
define an "add" method. You'd like to check the ArrayList class:
http://java.sun.com/j2se/1.3/docs/api/java/util/ArrayList.html
Good luck
this needs to be a store of type Module, not int.
> private String studentName;
> private String number;
> private Module mod;
>
> public Student(String name, String studentNumber)
> {
> moduleArray = new int[12];
> studentName = name;
> number = studentNumber;
>
> }
>
> public void addModule(Module m)
> {
> mod = m;
> moduleArray.add(mod);
the method of actually adding the new one to the store will depend on
the storage you choose.
arrays, although objects in java, have no built in methods to add
items.
the syntax is
moduleArray[index] = m
index will start at zero and be up to 11 as you defined it [12]
you will need to maintain the index position in code.
You might try looking instead at the Collections framework. These
classes are purpose built to define storage of objects like this. Look
at online documentation for the classes ArrayList, HashSet and Vector.
All have strengths and weaknesses. You pays your money and you takes
your choice.
e.g.
import java.util.Vector;
Vector mods = new Vector();
. mods.add(m);
.
.
mods.remove(m)
Thanks a lot. I'm more on the right lines now. I've made the class so that
the user can add objects made through the module class. I know how to remove
integer elements from arrays, but I can't find anything on removing objects
from another class. I realise using the forms of storage you suggested is
easier, but I'm supposed to do it using a normal array.
public class Student
{
private Module[] moduleArray;
private String studentName;
private String number;
private Module mod;
private Module mods;
public Student(String name, String studentNumber)
{
moduleArray = new Module[12];
studentName = name;
number = studentNumber;
}
public void addModule(Module m)
{
mod = m;
moduleArray[0] = mod;
moduleArray[1] = mod;
moduleArray[2] = mod;
moduleArray[3] = mod;
moduleArray[4] = mod;
moduleArray[5] = mod;
moduleArray[6] = mod;
moduleArray[7] = mod;
moduleArray[8] = mod;
moduleArray[9] = mod;
moduleArray[10] = mod;
moduleArray[11] = mod;
}
public void removeModule(Module m)
{
mod = m;
}
>
> Thanks a lot. I'm more on the right lines now.
Well, I have the feeling you are not....
> I realise using the forms of storage you suggested is
> easier, but I'm supposed to do it using a normal array.
>
Why?.....
> public class Student
> {
> private Module[] moduleArray;
> private String studentName;
> private String number;
> private Module mod;
> private Module mods;
>
> public Student(String name, String studentNumber)
> {
> moduleArray = new Module[12];
> studentName = name;
> number = studentNumber;
>
> }
>
> public void addModule(Module m)
> {
> mod = m;
you don't need to assign m to mod... just use m inestead.
> moduleArray[0] = mod;
> moduleArray[1] = mod;
> moduleArray[2] = mod;
> moduleArray[3] = mod;
> moduleArray[4] = mod;
> moduleArray[5] = mod;
> moduleArray[6] = mod;
> moduleArray[7] = mod;
> moduleArray[8] = mod;
> moduleArray[9] = mod;
> moduleArray[10] = mod;
> moduleArray[11] = mod;
> }
A "for" loop is perfect for this kind of things... anyways, it doesn't
seem to be very useful an array whose members are all the same.... just
use a single variable.
> public void removeModule(Module m)
> {
> mod = m;
> }
>
Sergio....
Thanks. You're right, this is a total mess. The array is supposed to allow
the user to add different object he/she makes using the Module class. I
can't work out how to all different objects to be added.
> Thanks. You're right, this is a total mess. The array is supposed to allow
> the user to add different object he/she makes using the Module class. I
> can't work out how to all different objects to be added.
You construct new objects instead of reusing the same object.