Click here to Skip to main content
16,002,236 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
<pre lang="java">
C#
package aggregationlatest;

public class AggregationLATEST {

    public static void main(String[] args) {

        Student student1=new Student("JOHN SNOW",88,1);
        School s1= new School("XYZ","Torronto",12,1000,student1);
        Bus b1=new Bus("CA 1101","START","END",50,200);      

        s1.createClass();
        s1.display();
        student1.busDetail(b1);


        }

}

class School
{
String name;
String address;
int maxClass;
int strength;
Class c1;
Student s1;                                    //school have student Aggregation

School(String name,String address,int maxClass,int strength,Student first)
{
this.name=name;
this.address=address;
this.maxClass=maxClass;
this.strength=strength;
s1=first;
}
        class Class                            //since class is dependend on school hence composition 
       {
           String name;
           int Boysstrength;
           int girlsStrength;
           String classTeacher;

           Class(String name,int Boysstrength,int girlsStrength,String classTeacher)
            {
           this.name=name;
           this.Boysstrength=Boysstrength;
           this.girlsStrength=girlsStrength;
           this.classTeacher=classTeacher;
           }

           void totalStrength()
           {
               System.out.println("total strengthis " + (Boysstrength + girlsStrength));
           }

        }


           void createClass()
           {
           c1=new Class("1st",12,12,"CERSI LANISTER");  //can be mltiple object 

           }

           void display()
           {

           System.out.println(name+" "+address+" "+maxClass+" "+strength);
           System.out.println(c1.name+" "+c1.Boysstrength+" "+c1.girlsStrength+" "+c1.classTeacher);
           System.out.println(s1.name+" "+s1.lastPERCENTAGE+" "+s1.rollNo);
           c1.totalStrength();
           }
}



class Student
{

    String name;
    int lastPERCENTAGE;
    int rollNo;
    Student(String name,int lastPERCENTAGE,int rollNo)
    {
    this.name=name;
    this.lastPERCENTAGE=lastPERCENTAGE;
    this.rollNo=rollNo;

    }

    void busDetail(Bus b1)                        //student uses bus (association)
    { 

        System.out.println(b1.busLicencePlate+" "+b1.endPoint+" "+b1.maxStrength+" "+b1.monthlyCharge);

    }

}


class Bus
{
    String busLicencePlate;
    String startingPoint;
    String endPoint;
    int maxStrength;
    int monthlyCharge;

    Bus(String busLicencePlate,String startingPoint,String endPoint,int maxStrength,int monthlyCharge)
    {
    this.busLicencePlate=busLicencePlate;
    this.startingPoint=startingPoint;
    this.endPoint=endPoint;
    this.maxStrength=maxStrength;
    this.monthlyCharge=monthlyCharge;        

    }

}



What I have tried:

I have tried to implement aggregation, association and composition .Look at the comment to see which is where .

Question
1.Is this class model wright?
2.Is the implementation is wright?
3.what will be your suggestion to improvement this code ?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900