Results 1 to 5 of 5
  1. #1
    Pinky Punch is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default How To Connet Between Two Classes

    Hello!!
    I Have A Question
    I Feel It is Little Stupid :)
    But I wanna To Know It

    I wanna to know howa to connect between two classes
    I have 3 examples Iwanna To know How to do it please :(

    First example :

    I Have Class Student and Class Courses

    And the class Course need some variables of class Student ,How Can I Do that ???!!!



    Second Example :
    Also If i wanna to make a third class its name is grade class that connect between the course and student classes In other Words I wanna to make an object of class student and courses in the grade class But i wanna This object to be refrence for the both classes not A new one bcz I will Add values to both classes through that class



    Third Example:
    Last thing If i make class(Student ) and i want to make an array of that class at onther class(Department) What i should do ???

    HOpe U Understand me
    Im waiting for Ur Help
    THnx In Advance

  2. #2
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default First example

    Divide and conquer... I'll take the first one.

    Define your class Student first. Define the data that is private to Student. Then define public get/set methods that other classes will use to access Student's data. For example:

    Java Code:
    public class Student {
        private String name;
    ...
    
        public String getName() {
            return name;
        }
        
        public void setName(String s) {
            name = s;
        }
    ...
    }
    In class Courses:
    Java Code:
    public class Courses {
    
    
    ...
    
        public void addStudent(Student s) {
    
            ...
            System.out.println("You added:  " + s.getName());
        }
    
    ...
    
    }

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default

    Please get rid of that huge font, the silly formatting, the purple subheaders and those stupid smilies; your question reads like a little kid's birthday party invitation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    Please get rid of that huge font, the silly formatting, the purple subheaders and those stupid smilies
    Seconded. Also, use proper words.

    db

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Lol, and I have expected the "i"s to be written with love-hearts.

    As for your second example, make a package and put all 3 classes in the same package.

    Java Code:
    package school;
    
    public class Grade {
    
        private java.util.List<Student> studentList = new java.util.ArrayList<Student>();
    
        Grade(Student[] students) {
            addStudents(students);
        }
    
        Grade(java.util.List<Student> students) {
            addStudents(students);
        }
    
        public void addStudent(Student s) {
            studentList.add(s);
        }
    
        public void addStudents(java.util.List<Student> s) {
            studentList.addAll(s);
        }
    
        public void addStudents(Student[] s) {
            studentList.addAll(java.util.Arrays.asList(s));
        }
    
    }

    All three are connected because Grade contains Students who have Courses

Similar Threads

  1. Help with classes
    By Exothesis in forum New To Java
    Replies: 11
    Last Post: 10-11-2010, 03:00 AM
  2. classes
    By exose in forum New To Java
    Replies: 2
    Last Post: 09-30-2010, 05:21 PM
  3. Cant run my classes
    By Assaf A in forum Eclipse
    Replies: 1
    Last Post: 04-22-2008, 03:31 PM
  4. Using a JAR from other classes
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-02-2008, 08:08 PM
  5. Replies: 0
    Last Post: 12-18-2007, 07:07 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
  •