Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30
  1. #21
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    You can't have 2 public classes in the same file.

    Remove the public keyword on all classes that don't have method main, or init. (unless you're making a 1 class file that doesn't actually run and is just there to be called, even then you could still omit the public keyword i beleive.)

  2. #22
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Roselicious View Post
    im having an error message saying class is expected
    Can you send me your complete error message here? Do you've define both class in one *.java file?

  3. #23
    Roselicious is offline Member
    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    
    public class Staff extends Person
    {
        private int salary;
        private String dateHired;
        MyDate objMyDate = new MyDate(int D, int M, int Y);
        
        public Staff(String name, String nricNo, String phoneNo, int salary)
        {
            super(name, nricNo, phoneNo);
            this.salary = salary;
        }
        
        public void setSalary(int salary)
        {
            this.salary = salary;
        }
        
        public int getSalary()
        {
            return salary;
        }
        
        public String getDateHired()
        {
            return objMyDate.toString;
        }
        
        public MyDate getMyDate()
        {
            return int D, int M, int Y
        }
        
       public String toString()
       {
           super.toString();
           String s = "Salary : " + salary;
           return s;
        }
        
        
    }
    Java Code:
    import java.util.*;
    public class MyDate
    {
        private int day;
        private int month;
        private int year;
        
        public MyDate(int D, int M, int Y)
        {
             day = D;
             month = M;
             year = Y;
            
        }
        
        public void setDay(int D)
        {
            day = D;
        }
        
        public int getDay()
        {
            return day;
        }
        
        public void setMonth(int M)
        {
            month = M;
        }
        
        public int getMonth()
        {
            return month;
        }
        
        public void setYear(int Y)
        {
            year = Y;
        }
        
        public int getYear()
        {
            return year;
        }
        
        public String toString()
        {
            String s = "Date : "  + day + "/" + month + "/" + year;
            return s;
        }
     
    }
    Attached Thumbnails Attached Thumbnails How do I call a class in another class-class-expected-error.jpg  

  4. #24
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    This is wrong actually,

    Java Code:
    MyDate objMyDate = new MyDate(int D, int M, int Y);
    When you creating an object of another class, you've pass the parameters, not the initialized variables. It should be something like that,

    Java Code:
    MyDate objMyDate = new MyDate(17, 4, 12010);

  5. #25
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    And the following code segment,

    Java Code:
    public MyDate getMyDate()
        {
            return int D, int M, int Y
        }
    You try to return a MyDate class type, means that an object of class. Byt you return in the method return int D, int M, int Y. You cannot do that, you cannot return multiple values like that.

  6. #26
    Roselicious is offline Member
    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    Default

    Hey thanks alot... It worked now . Really appreciated.

  7. #27
    Roselicious is offline Member
    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    Default

    Do you guys have any shorter way to add student or staff into the person lists?

    Java Code:
    public class PersonTester
    {
        public static void main(String[] args)
        {
            Centre cr = new Centre("Vanisri"); 
          
            Student S1 = new Student("John", "S2222222H", "91234567", 3);
           Student S2 = new Student("Mary", "S870051JH", "94510557", 2);
           Student  S3 = new Student("James", "S3866218Z", "97654321", 1);
            Student  S4 = new Student("Jenny", "S5439826C", "91055679", 3);
             Student  S5 = new Student("Peter", "S7293438E", "91453077", 2);
           
           cr.addPersons(S1);
            cr.addPersons(S2);
             cr.addPersons(S3);
              cr.addPersons(S4);
               cr.addPersons(S5);
           
         
            System.out.println(S2.toString());
            System.out.println(S3.toString());
            System.out.println(S4.toString());
            System.out.println(S5.toString());
            System.out.println("");
    //          cr.searchPersonsList("John");
    //          System.out.println(S1.toString());
    //          System.out.println("");
    Java Code:
    import java.util.*;
    public class Centre
    {
        private String centreName;
        private ArrayList<Person> personsList;
        
        public Centre(String centreName)
        {
            this.centreName = centreName;
            personsList = new ArrayList<Person>();
        }
        
        public void setCentreName(String centreName)
        {
           this.centreName = centreName; 
        }
        
        public String getCentreName()
        {
            return centreName;
        }
        
        public void addPersons(Person persons)
        {
            personsList.add(persons);
        }
        
        public int getNumOfPerson()
        {
            return personsList.size();
        }
        
        public Person searchPersonsList(String name)
        {
           for(Person p: personsList)
           {
               if(p.getName().equals(name)) return p;
            }
            return null;
        }
        
        public boolean removePerson(String nricNo)
        {
            Person person = searchPersonsList(nricNo);
            if(person == null) return false;
            return personsList.remove(person);
        }
    }
    Java Code:
    import java.util.*;
    
    public class Student extends Person
    {
        private int yearOfStudy;
        
        public Student(String name, String nricNo, String phoneNo, int yearOfStudy)
        {
            super(name, nricNo, phoneNo);
            this.yearOfStudy = yearOfStudy;
        }
        
        public void setYearOfStudy(int yearOfStudy)
        {
            this.yearOfStudy = yearOfStudy;
        }
        
        public int getYearOfStudy()
        {
            return yearOfStudy;
        }
            
        public String toString()
        {
    //         super.toString();
            String s = super.toString() + " Year Of Study : " + yearOfStudy;
            return s;
        }
        
    }

  8. #28
    Roselicious is offline Member
    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    Default

    Any idea how to ?:confused:

  9. #29
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Roselicious View Post
    Hey thanks alot... It worked now . Really appreciated.
    You are welcome. :)

  10. #30
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Roselicious View Post
    Do you guys have any shorter way to add student or staff into the person lists?
    You are sroring objects in the list of Person type. So you have to define it them before added/store anyway. I prefer to store initial information in a List and use them in a loop to created relevant objects.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 03:33 PM
  2. need help to call another class
    By doha786 in forum New To Java
    Replies: 2
    Last Post: 03-08-2010, 11:40 AM
  3. How can I call method from class in other class??
    By Hisham in forum New To Java
    Replies: 6
    Last Post: 02-14-2010, 04:49 PM
  4. How do I call a class in jsp page
    By Greg in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 09-20-2009, 11:27 PM
  5. Can I call directly the *.class
    By doanhanam in forum New To Java
    Replies: 1
    Last Post: 11-16-2008, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •