Testprogram problem
I need to generate the following output with a TestProgram.
Lecturer 65374 has 3 students:
Student number 1 has name of Amanda
Student number 2 has name of Patrick
Student number 3 has name of Jay
The Student and Lecturer classes are below.
Meanwhile, here is my attempetd test program:
public class TestProgram_four {
public static void main(String args[])
{
// create an array to hold three student references
Student students[] = new Student[3];
students[0]= new Student (1,"Amanda",65374);
students[1]= new Student (2,"Patrick",65374);
students[2]= new Student (3,"Jay",65374);
// retieve and display attribute values
for (int p=0; p<students.length; p++) {
System.out.println(
"Student number "+
students[p].getNo() +
" has name of " +
students[p].getName()
);
}
}
}
Below are the Lecturer and Student scripts for which I need to create a TestProgram:
But first here is my attempt at the TestProgram. I am getting three error messages saying TestProgram_four.java:15: cannot resolve symbol
symbol : constructor Student (int,java.lang.String,int)
location: class Student
students[0]= new Student (1, "Susan", 65374);
^
There are other two error similar to above for Patrick and Jay
Code: ( java )
import java.util.*;
public class Lecturer // Lecturer class to illustrate 1 to many association with Student
{ // attributes........................................ ........(iii)
private int id;
private String subject;
private Vector students; // implement student association with Vector class
private Lecturer(int anId, String aSubject) // constructor
{ setId(anId);
setSubject(aSubject);
students = new Vector(10); //start with Vector for 10 students
} //.................................................. .....(iv)
public void addStudentToLecturer(Student aStudent) // custom method addStudentToLecturer
{ students.addElement(aStudent); //........................(ii)
aStudent.setLecturer(this); // connect student to lecturer (1..1)
}
public Vector getStudents() // custom method to return vector of students.......(i)
{ return students;}
public void setId(int anId) // set accessor methods
{ id = anId;}
public void setSubject(String aSubject)
{ subject = aSubject;}
public int getId() // get accessor methods
{ return id;}
public String getSubject()
{ return subject;}
public class Student // Student with reference variable and accessors
{ private int no; //attributes
private String name;
private Lecturer lecturer;
public Student(int aNo, String aName, Lecturer aLecturer) //constructor with 2 parameters plus lecturer reference
{ setNo(aNo); //invoke accessors to populate attributes
setName(aName);
setLecturer(aLecturer);
lecturer.addStudentToLecturer(this); //tell lecturer to associate with this student
}
public void setNo(int aNo) //set accessor methods
{ no = aNo;}
public void setName(String aName)
{ name = aName;}
public void setLecturer(Lecturer aLecturer)
{ lecturer = aLecturer;}
public int getNo() //get accessor methods
{ return no;}
public String getName()
{ return name;}
public Lecturer getLecturer()
{ return lecturer;}
}>
Your Student constructor takes three parameters. An int, a String and a Lecturer. So you can't just try int, String, int and expect it to work.
Thanks. What do you suggest I do. i.e. what do I put in place of a Lecturer?
> Thanks. What do you suggest I do. i.e. what do I put> in place of a Lecturer?read it again, slowlyYour Student constructor takes three parameters. An int, a String and a Lecturer.so you should pass it a Lecturer, not something else
I have used the following but no luck. Any ideas
// create an array to hold three student references
Student students[] = new Student[3];
lecturer = new Lecturer (9009339, "DS04011");
students[0]= new Student (1,"Susan",lecturer);
students[1]= new Student (2,"Peter",lecturer);
students[2]= new Student (3,"Muswaki",lecturer);
lol everyone is so nice with a girl..did'nt say anything about code formatting tag.Message was edited by: lrngjava
> lol everyone is so nice with a girl..did'nt say
> anything about code formatting tag.
jealous? not even reading teh code.
@barbie: "no luck" means ****
what is the error you get?
and btw, no-one is likely to raed your code if you don't use code tags (use Teh Button above the text box where you post)
> > lol everyone is so nice with a girl..did'nt say
> > anything about code formatting tag.
>
> jealous? not even reading teh cod
>
> @barbie: "no luck" means ****
> what is the error you get?
> and btw, no-one is likely to raed your code if you
> don't use code tags (use Teh Button above the text
> box where you post)
babrbie go lick some nuts and then come back and think a little more ;--)
and yea one more thing. when you are storing everything in an array then why create a vector? I mean are you using any vectors?
Thank you. Here is the code for the test program. I have also included a tag for the Student and Lecturer program below.
/*
Test program for Lecturer and Student
*/
import java.lang.*;
import java.util.*;
public class TestProgram_six
{
//private Lecturer lecturer;
public static void main(String args[])
{
// create an array to hold three student references
Student students[] = new Student[3];
Lecturer lecturer = new Lecturer (9009339, "DS04011");
//Lecturer lecturer = new Lecturer (9009339, "DS04011");
students[0]= new Student (1,"Susan",lecturer);
students[1]= new Student (2,"Peter",lecturer);
students[2]= new Student (3,"Muswaki",lecturer);
for (int p=0; p<students.length; p++)
{
System.out.println(
"Student number "+
//students[p].getStudent().getNo() +
students[p].getNo() +
" has name of " +
//students[p].getStudent().getName()
students[p].getName()
);
}
}
}
This is the Lecturer program
import java.util.*;
public class Lecturer // Lecturer class to illustrate 1 to many association with Student
{ // attributes................................................(iii)
private int id;
private String subject;
private Vector students; // implement student association with Vector class
private Lecturer(int anId, String aSubject) // constructor
{ setId(anId);
setSubject(aSubject);
students = new Vector(10); //start with Vector for 10 students
} //.......................................................(iv)
public void addStudentToLecturer(Student aStudent) // custom method addStudentToLecturer
{ students.addElement(aStudent); //........................(ii)
aStudent.setLecturer(this); // connect student to lecturer (1..1)
}
public Vector getStudents() // custom method to return vector of students.......(i)
{ return students;}
public void setId(int anId) // set accessor methods
{ id = anId;}
public void setSubject(String aSubject)
{ subject = aSubject;}
public int getId() // get accessor methods
{ return id;}
public String getSubject()
{ return subject;}
}
This is the Student program
public class Student // Student with reference variable and accessors
{ private int no; //attributes
private String name;
private Lecturer lecturer;
public Student(int aNo, String aName, Lecturer aLecturer) //constructor with 2 parameters plus lecturer reference
{ setNo(aNo); //invoke accessors to populate attributes
setName(aName);
setLecturer(aLecturer);
lecturer.addStudentToLecturer(this); //tell lecturer to associate with this student
}
public void setNo(int aNo) //set accessor methods
{ no = aNo;}
public void setName(String aName)
{ name = aName;}
public void setLecturer(Lecturer aLecturer)
{ lecturer = aLecturer;}
public int getNo() //get accessor methods
{ return no;}
public String getName()
{ return name;}
public Lecturer getLecturer()
{ return lecturer;}
}
>
Barbie your code is a big mess. But may be this might help you a little bit.
Student Class
package org.eclipsebook.chapter3;
public class Student {
private int id;
private String name;
private int number;
public Student(int id, String name, int number) {
this.id = id;
this.name = name;
this.number = number;
}
public int getId(){return id;}
public String getName(){return name;}
public int getNumber(){return number;}
public String toString() {
return getId() + "\t" + getName() + "\t" + getNumber();
}
}
StudentTest class
package org.eclipsebook.chapter3;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Arrays;public class StudentTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student students[] = new Student[3];
students[0] = new Student(1,"Amanda", 1234);
students[1]= new Student (2,"Patrick",65374);
students[2]= new Student (3,"Jay",65374);
//convert the array into vector
Vector vector = new Vector(Arrays.asList(students));
//just like a for loop
Enumeration e = vector.elements();
while(e.hasMoreElements()) {
System.out.println(e.nextElement());
}
}
}
output
1Amanda1234
2Patrick65374
3Jay65374
Message was edited by:
fastmike
Thank you fastmike. The Student and Lecturer program were predesigned and I am only expected to write a test program to generate the following output given the Student and Lecturer program:
Lecturer 65374 has 3 students:
Student number 1 has name of Amanda
Student number 2 has name of Patrick
Student number 3 has name of Jay
The first student is with Lecturer 65374 of the subject JAV001