Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

package week5;

import java.io.FileWriter;
import java.util.ArrayList;

public class lecture5cw {

public static void main(String[] args) throws Exception{

Country s1=new Country("India",13000000,3300000,2.2);


Country s2=new Country("Pakistan",11000000,1100000,2.3);
Country s3=new Country("Egypt",105000000,1500000,2.3);
Country s4=new Country("Australia",25000000,4000000,1.6);

ArrayList<Country> listC=new ArrayList<>();

listC.add(s1);
listC.add(s2);
listC.add(s3);
listC.add(s4);

for (Country elem:listC )


{ System.out.println(elem.toString());}

FileWriter outp=new FileWriter("Country.txt");


for (Country elem:listC )
{ outp.write(elem.toString()); }
outp.close();

System.out.println("Success Now");
}

}
package week5;

public class Country {


private String name;
private int population;
private int area;
private double fertilityRate;

public Country (String name, int pop, int area, double FR)
{this.name=name;
this.population=pop;
this.area=area;
this.fertilityRate=FR;
}// Constructor

public String toString() {


String str=name+" "+population+" "+area+" "+fertilityRate+'\n';
return str;

}//toString
}//class

You might also like