Java code problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • java4life
    New Member
    • Jun 2007
    • 2

    Java code problem

    Java 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_fou r {

    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",653 74);
    students[1]= new Student (2,"Patrick",65 374);
    students[2]= new Student (3,"Jay",65374) ;

    // retieve and display attribute values
    for (int p=0; p<students.leng th; p++) {
    System.out.prin tln(
    "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_fou r.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(aSub ject);
    students = new Vector(10); //start with Vector for 10 students
    } //............... ............... ............... ..... .....(iv)
    public void addStudentToLec turer(Student aStudent) // custom method addStudentToLec turer
    { students.addEle ment(aStudent); //............... .........(ii)
    aStudent.setLec turer(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(Stri ng 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(aLe cturer);
    lecturer.addStu dentToLecturer( 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(Lec turer aLecturer)
    { lecturer = aLecturer;}
    public int getNo() //get accessor methods
    { return no;}
    public String getName()
    { return name;}
    public Lecturer getLecturer()
    { return lecturer;}
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Code tags code tags code tags code tags code tags
    2.) Look at each error one by one and fix the program as per its suggestion. The error messages really give you very good hints.

    Comment

    Working...