DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3

Thread: Help!!!

  1. #1
    Join Date
    Mar 2005
    Posts
    3

    Question Help!!!

    Hi i am a new to java and having a little trouble can anybody tell me what is wrong with this code, only Person compiles and runs, any help will be greatly appreciated.


    class Student extends Person
    {

    // instance variables

    protected String course;
    protected int year;


    // constructors

    public Student(String nme, char sex, Date dob, String insceNumber,String S, int yr)
    {

    super(nme,sex,dob,insceNumber);
    course=S;
    year=yr;


    }


    public Student ()
    {

    course="Unknown";
    year=0;
    }

    // transformers

    public void setYear(int yr)
    {
    year=yr;
    }

    public void setCourse(String S)
    {
    course=S;
    }

    public void copy(Student other)
    {
    super.copy(other);
    course=other.course;
    year=other.year;

    }

    //accessors



    public boolean sameAs(Student other)
    {
    return super.equals(other) && year==other.year && course==other.course;
    }

    public String toString()
    {
    return (super.toString()+"Year:"+year+"Course:"+course);
    }
    }

    public class Person
    {
    // instance variables

    protected String name;
    protected String gender;
    protected Date dateofBirth;
    protected String address;
    protected String natInsceNo;


    // class variable

    private static int counter;

    //class methods

    public static int count()
    {
    return counter;
    }

    //constructors

    public Person(String nme, char sex, Date dob,String insceNumber, String add)
    {
    name=nme;
    //setGender(sex);
    dateofBirth=dob;
    natInsceNo=insceNumber;
    address=add;
    counter++;
    }

    //transformers

    public void setGender (char sex)
    {
    if (sex=='m' || sex=='M')
    gender="Male";
    else if (sex=='f' || sex=='F')
    gender = "Female";
    else gender="Unknown";
    }

    public void setdateofBirth(Date dob)
    {
    dateofBirth=dob;
    }

    public void setName (String nme)
    {
    name=nme;
    }

    public void setnatInsceNo (String insceNumber)
    {
    natInsceNo=insceNumber;
    }

    public void setaddress (String add)
    {
    address=add;
    }

    public void copy(Person other)
    {
    name=other.name;
    gender=other.gender;
    dateofBirth=other.dateofBirth;
    address=other.address;
    natInsceNo=other.natInsceNo;
    }


    //accessors

    public String toString()
    {
    return "\n"+"Name: "+name+"\n"+"Date Of Birth: "+dateofBirth+"Gender: "+gender+"\n"+"Address : "+address+"\n"+"National Insurance No: "+natInsceNo+"\n";
    }

    public boolean equals(Person other)
    {
    if(name == other.name && natInsceNo==other.natInsceNo)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    public Person()
    {
    name="unknown";
    address="unknown";
    natInsceNo="unknown";
    counter++;
    }

    public String getName()
    {
    return name;
    }

    public String getGender()
    {
    return gender;
    }

    public String getAddress()
    {
    return address;
    }

    public String getNatInsceNo()
    {
    return natInsceNo;
    }

    public Date dateofBirth()
    {
    return dateofBirth;
    }
    }


    class Store
    {
    private Person[] list;
    private int count;
    private int maxSize;

    //constructor

    Store(int max)
    {
    maxSize = max;
    list= new Person[maxSize];
    count=0;
    }

    //transformer

    public void add(Person p)
    {
    if(count != maxSize)
    {
    list [count] = p;
    System.out.println("Person "+p.name+" stored at "+count+" in store");
    count++;
    }
    else
    {
    System.out.println("Person "+p.name+" Store Full");
    }
    }

    //accessors

    public boolean isFull()

    {
    if (count == maxSize)
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    public int getCount()
    {
    return count;
    }

    public void displayAll()
    {
    int nCount=0;
    while (nCount < count)
    {

    System.out.println("Location:"+nCount+"\n"+"Contains:"+"\n"+list[nCount].toString()+"\n");
    nCount++;
    }
    }
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Its this statement in the Student constructor:
    Code:
    super(nme,sex,dob,insceNumber);
    the statement invokes the Person constructor, and that signature is like:
    Code:
    public Person(String nme, char sex, Date dob, String insceNumber, String add)
    You have forgotten the address parameter, and, I think you need to import something
    to support your Date object, like:

    import java.sql.Date;

    You should also write the equals method in Person like:
    Code:
    public boolean equals(Object ob) {
        if (!(ob instanceof Person)) return false;
        Person other=(Person)ob;
        if (name == other.name && natInsceNo == other.natInsceNo) {
          return true;
        }
        else {
          return false;
        }
    }
    ... and change the sameAs method in Student to

    Code:
      public boolean equals(Object ob) {
        if (!(ob instanceof Student)) return false;
        Student other=(Student)ob;
        return super.equals(other) && year == other.year && course == other.course;
      }
    The reason is that the
    public boolean equals (Object element)
    method is vital in many of the collection classes utilization.

    I have tried to explain it in this example:

    Code:
    import java.util.*;
    class Student {
      private int id=-1;
      private String name="";
      public Student (int id, String name) {
        this.id=id;
        this.name=name;
      }
      public Student (String name) {
        this.name=name;
      }
      public String getName() {  return name; }
      public int getID () { return id; }
    
      public String toString() {
        return "Student: ID: "+id+", name: "+name;
      }
      public boolean equals(Object ob) {
        if (!(ob instanceof Student)) return false;
        Student aStudent=(Student)ob;
        return name.trim().equals(aStudent.getName().trim());
      }
    }
    public class VectorExample {
      Vector v=null;
      public VectorExample() {
        v=new Vector();
      }
      public void test() {
        v.addElement(new Student(1,"John"));
        v.addElement(new Student(2,"Mary"));
        v.addElement(new Student(3,"Peter"));
    
        System.out.println("Vector contains "+v.size()+" objects");
    
        //lookup-find by object
        if (!find ("Mary")) System.out.println("Student: Mary not found");
        if (!find ("Peter")) System.out.println("Student: Peter not found");
        if (!find ("John")) System.out.println("Student: John not found");
        if (!find ("Butthead")) System.out.println("Student: Butthead not found");
        v.remove(new Student("Mary"));
        System.out.println("Vector contains "+v.size()+" objects");
        if (!find ("Mary")) System.out.println("Student: Mary not found");
        int ix=v.indexOf(new VectorExample());
        System.out.println(ix);
      }
      public boolean find(String name) {
        int ix=v.indexOf(new Student(name));
        if (ix >= 0) {
          System.out.println("Index: " + ix + " contains: " + v.elementAt(ix));
          return true;
        }
        else
          return false;
      }
      public static void main(String[] args) {
        VectorExample ve = new VectorExample();
        ve.test();
      }
    }
    PS: please use the code tag around big blocks of code, I think it will increase your
    chances of getting responses...
    Last edited by sjalle; 03-17-2005 at 03:16 PM.
    eschew obfuscation

  3. #3
    Join Date
    Mar 2005
    Posts
    3
    Thanks for your help!!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links