0% found this document useful (0 votes)
41 views

Aggregation With and Without Using Generics

This document contains code for an aggregation example program in Java that defines SportsPerson and Country classes. The Country class aggregates SportsPerson objects and stores them in an ArrayList. The main method creates a Country object for India, adds 3 SportsPerson objects to it, and prints the Country and SportsPerson details.

Uploaded by

Madhu Charan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Aggregation With and Without Using Generics

This document contains code for an aggregation example program in Java that defines SportsPerson and Country classes. The Country class aggregates SportsPerson objects and stores them in an ArrayList. The main method creates a Country object for India, adds 3 SportsPerson objects to it, and prints the Country and SportsPerson details.

Uploaded by

Madhu Charan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

AGGREGATION EXAMPLE JAVA PROGRAM ON COUNTRY AND SPORTSPERSON CLASSES

package pack1;
import java.util.ArrayList;

class SportsPerson
{
private String name;
private int age;
private double h;
private double w;

public String getName( ){ return name; }


public int getAge( ){ return age; }
public double getH( ) { return h; }
public double getW( ) { return w; }
public void setName(String n) { name = n; }
public void setAge(int a) { age = a; }
public void setH(double h) { this.h = h; }
public void setW(double w) { this.w=w; }

public String toString( )


{
String str;
str = String.format("Sports Person Details :%n"
+ "Name :%s%nAge :%d%nHeight :%f%n"
+ "Weight :%f%n%n", getName( ),getAge( ),getH( ),getW( ));
return str;
}

class Country
{
private String name;
private int area;
private double population;
private String capital;

ArrayList<SportsPerson> spobj = new ArrayList<SportsPerson>();

Country(String n,int a,double p,String c)


{
name = n;
area = a;
population = p;
capital = c;
}

public String getName( ) { return name; }


public int getArea( ) { return area; }
public double getPopulation( ){ return population;}
public String getCapital( ) { return capital; }

public void setSportsPerson(String n,int a,double h,double w)


{
SportsPerson obj = new SportsPerson( );
obj.setName(n);
obj.setAge(a);
obj.setH(h);
obj.setW(w);
spobj.add(obj);
}

public String toString( )


{
String str;
str = String.format("Country Details :%n"
+ "Name :%s%nArea :%d Sq. Km.%nPopulation%f lakhs"
+ "%nCapital :%s%n", getName( ),getArea( ),getPopulation( ),
getCapital( ));
return str;
}
}

public class Demo


{

public static void main(String[] args)


{

Country cobj = new Country("India",12938,135,"Delhi");

cobj.setSportsPerson("Dhoni",38,159,82);
cobj.setSportsPerson("Kholi",34,171,80);
cobj.setSportsPerson("Rohit",35,167,84);

System.out.println(cobj);

for(SportsPerson obj: cobj.spobj)


System.out.println(obj);

}
Aggregation Program without using Generics

package pack2;

class SportsPerson
{
private String name;
private int age;
private double h;
private double w;

public String getName( ){ return name; }


public int getAge( ){ return age; }
public double getH( ) { return h; }
public double getW( ) { return w; }
public void setName(String n) { name = n; }
public void setAge(int a) { age = a; }
public void setH(double h) { this.h = h; }
public void setW(double w) { this.w=w; }

public String toString( )


{
String str;
str = String.format("Sports Person Details :%n"
+ "Name :%s%nAge :%d%nHeight :%f%n"
+ "Weight :%f%n%n", getName( ),getAge( ),getH( ),getW( ));
return str;
}

class Country
{
private String name;
private int area;
private double population;
private String capital;

SportsPerson spobj[ ] = new SportsPerson[3];


static int i=0;

Country(String n,int a,double p,String c)


{
name = n;
area = a;
population = p;
capital = c;
}

public String getName( ) { return name; }


public int getArea( ) { return area; }
public double getPopulation( ){ return population;}
public String getCapital( ) { return capital; }

public void setSportsPerson(String n,int a,double h,double w)


{
SportsPerson obj = new SportsPerson( );
obj.setName(n);
obj.setAge(a);
obj.setH(h);
obj.setW(w);
spobj[i] = obj;
}

public String toString( )


{
String str;
str = String.format("Country Details :%n"
+ "Name :%s%nArea :%d Sq. Km.%nPopulation%f lakhs"
+ "%nCapital :%s%n", getName( ),getArea( ),getPopulation( ),
getCapital( ));
return str;
}
}

public class Demo


{

public static void main(String[] args)


{

Country cobj = new Country("India",12938,135,"Delhi");


cobj.setSportsPerson("Dhoni",38,159,82);
cobj.setSportsPerson("Kholi",34,171,80);
cobj.setSportsPerson("Rohit",35,167,84);
System.out.println(cobj);
for(SportsPerson obj: cobj.spobj)
System.out.println(obj);
}
}

You might also like