In: Computer Science
You will generate a People
class of object and load an ArrayList with person objects, then report the contents of that ArrayList. To do so, you must
perform the following:
(30 pts.)
A) (15/30 pts.
(line break, 11 pt)
) - Create a class file “People.java” (which will generate People.class upon compile). People.java will
have eight(8) methods to
1) read a .txt file ‘people.txt’
2) generate:
▪ List of all students AND teachers
▪ List of all students OR teachers
▪ List of all female or male students OR teachers
▪ List of female or male students OR teachers whose age is greater than or equal to given age
▪ List of female or male students OR teachers whose age is greater than or equal to given age1
and less than given age2
▪ List students OR teachers whose age is greater than or equal to given age
▪ List students OR teachers whose age is greater than or equal to given age1 and less than given
age2 (line break, 11 pt)
B) (3/30 pts.) – When you instantiate your class with new, you must use the text file name (eg. People p = new People ("people.txt"))
C) (10/30 pts.) – You are required to use “Constructor”, “ArrayList” and “Overloading” concepts.
[Example of Main()]→This is an example. I will test all cases.
public static void main(String[] args)throws FileNotFoundException { People p =new People("people.txt");
D) Once your People.class is functional, generate a file “Assg05_yourName.java”.
Assg05_yourName.java should contain only one Main() method.
p.list();
p.list("s");
p.list("t","f");
p.list("s","f", 20); // List of female students whose
age>=20
// List of students AND teachers) // List of students // List of female teachers
p.list("s","f", 15, 20); // List of female students whose age>=15 and age<=20
p.list("s", 20); // List of students whose age>=20
p.list("s", 15, 20); // List of students whose age>=15 and age<=20 }
and the people. txt is
s John m 18
t William m 42
s Susan f 18
s Jack m 19
s Jennifer f 28
t Sophia f 35
t Emma f 37
s Chloe f 14
s Racheal f 14
s Olivia f 20
t Cara f 33
t Tom m 47
s Mia f 21
s Isabella f 18
s Zoe f 21
t Lily f 22
package peoplePackage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.*;
public class peopleClass{
String fileName="";
public peopleClass(String
fileName){//Constructor
this.fileName=fileName;
}
public ArrayList<String> list(){//Student and
Teachers
ArrayList<String> list = new
ArrayList<String>();
try{
BufferedReader reader = new
BufferedReader(new FileReader(fileName));
String line =
reader.readLine();
while
(line!=null) {
list.add(line);
line = reader.readLine();
}
reader.close();
}
catch (IOException e){
System.out.println("Exception :"+e);
}
return list;
}
public ArrayList<String> list(String stu){// For
Students
ArrayList<String> list = new
ArrayList<String>();
try{
BufferedReader reader = new
BufferedReader(new FileReader(fileName));
String line =
reader.readLine();
while
(line!=null) {
String[] splited = line.split(" ");
Arrays.toString(splited);
String str = splited[0];
if(str.equals("s")){
list.add(line);
}
line = reader.readLine();
}
reader.close();
}
catch (IOException e){
System.out.println("Exception :"+e);
}
return list;
}
public ArrayList<String> list(String tec,String
gen){//For Teacher And Female
ArrayList<String> list = new
ArrayList<String>();
try{
BufferedReader reader = new
BufferedReader(new FileReader(fileName));
String line =
reader.readLine();
while
(line!=null) {
String[] splited = line.split(" ");
String str = splited[0];
String str1= splited[2];
if(str.equals(tec) &&
str1.equals(gen)){
list.add(line);
}
line = reader.readLine();
}
reader.close();
}
catch (IOException e){
System.out.println("Exception :"+e);
}
return list;
}
public ArrayList<String> list(String stu,String
gen,int age){//For Student Female and AGE
ArrayList<String> list = new
ArrayList<String>();
try{
BufferedReader reader = new
BufferedReader(new FileReader(fileName));
String line =
reader.readLine();
while
(line!=null) {
String[] splited = line.split(" ");
String str = splited[0];
String str1= splited[2];
int ag = Integer.parseInt(splited[3]);
if(str.equals(stu) && str1.equals(gen)
&& ag==age){
list.add(line);
}
line = reader.readLine();
}
reader.close();
}
catch (IOException e){
System.out.println("Exception :"+e);
}
return list;
}
public ArrayList<String> list(String stu,String
gen,int age1,int age2){
//For Given range of age
ArrayList<String> list = new
ArrayList<String>();
try{
BufferedReader reader = new
BufferedReader(new FileReader(fileName));
String line =
reader.readLine();
while
(line!=null) {
String[] splited = line.split(" ");
String str = splited[0];
String str1= splited[2];
int ag = Integer.parseInt(splited[3]);
if(str.equals(stu) && (ag>=age1
&& ag<=age2)){
list.add(line);
}
line = reader.readLine();
}
reader.close();
}
catch (IOException e){
System.out.println("Exception :"+e);
}
return list;
}
public ArrayList<String> list(String stu,int
age){// For Students
ArrayList<String> list = new
ArrayList<String>();
try{
BufferedReader reader = new
BufferedReader(new FileReader(fileName));
String line =
reader.readLine();
while
(line!=null) {
String[] splited = line.split(" ");
Arrays.toString(splited);
String str = splited[0];
int ag = Integer.parseInt(splited[3]);
if(str.equals(stu) && ag==age){
list.add(line);
}
line = reader.readLine();
}
reader.close();
}
catch (IOException e){
System.out.println("Exception :"+e);
}
return list;
}
public ArrayList<String> list(String stu,int
age,int age1){// For Students Given range age
ArrayList<String> list = new
ArrayList<String>();
try{
BufferedReader reader = new
BufferedReader(new FileReader(fileName));
String line =
reader.readLine();
while
(line!=null) {
String[] splited = line.split(" ");
Arrays.toString(splited);
String str = splited[0];
int ag = Integer.parseInt(splited[3]);
if(str.equals(stu) &&
(ag>=age&&ag<=age1)){
list.add(line);
}
line = reader.readLine();
}
reader.close();
}
catch (IOException e){
System.out.println("Exception :"+e);
}
return list;
}
}
public class people{
public static void main(String[] args){
peopleClass p =new
peopleClass("people.txt");
ArrayList<String> result =
new ArrayList<String>();
result=p.list();
System.out.println(result);
result=p.list("s");
System.out.println(result);
result=p.list("t","f");
System.out.println(result);
result=p.list("s","f", 20);
System.out.println(result);
result=p.list("s","f", 15,
20);
System.out.println(result);
result=p.list("s", 20);
System.out.println(result);
result=p.list("s", 15, 20);
System.out.println(result);
}
}
Output :
“Assg05_yourName.java”
import peoplePackage.people;
import java.io.*;
import java.util.*;
public class Assg05_yourName{
public static void main(String []args){
System.out.println("Assg05_yourName
Class");
public static void
main(String[] args){
peopleClass p =new
peopleClass("people.txt");
ArrayList<String> result =
new ArrayList<String>();
result=p.list();
System.out.println(result);
result=p.list("s");
System.out.println(result);
result=p.list("t","f");
System.out.println(result);
result=p.list("s","f", 20);
System.out.println(result);
result=p.list("s","f", 15,
20);
System.out.println(result);
result=p.list("s", 20);
System.out.println(result);
result=p.list("s", 15, 20);
System.out.println(result);
}
}
Output: