Question

In: Computer Science

You will generate a People class of object and load an ArrayList with person objects, then...

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)

  1. 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"))

  2. 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


use java program

Solutions

Expert Solution

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:


Related Solutions

Person class You will implement the Person Class in Visual Studio. A person object may be...
Person class You will implement the Person Class in Visual Studio. A person object may be associated with multiple accounts. A person initiates activities (deposits or withdrawal) against an account that is captured in a transaction object. A short description of each class member is given below: Person Class Fields -   password : string Properties +   «C# property, setter private» IsAuthenticated : bool +   «C# property, setter absent» SIN : string +   «C# property, setter absent» Name : string Methods...
Write a for-each loop that prints all of the Student objects in an ArrayList<student> object called...
Write a for-each loop that prints all of the Student objects in an ArrayList<student> object called roster. Write this piece in Java. Write the correct code to execute.
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance Variables Declare an ArrayList of BabyName objects. Constructor public BabyNamesDatabase () - instantiate the ArrayList of BabyName objects. You will not insert the items yet. This method will be one line of code. Mutator Methods public void readBabyNameData(String filename) - open the provided filename given as input parameter, use a loop to read all the data in the file, create a BabyName object for...
Objectives: Sort data objects Compare the CPU efficiency between the object array and ArrayList used in...
Objectives: Sort data objects Compare the CPU efficiency between the object array and ArrayList used in the sorting operation. Input file: Data.csv (file is to big;I cannot update, you can use any data file) there are less than 20,000 records in Data.csv Your program will skip/reject dirty data lines. For example, if a line contains 2 fields, it is considered as a dirty data line and will be rejected/skipped by your program. You are required to develop a Java program...
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Develop a C++ class called PrimeNumberGenerator. An object of this class will generate successive prime numbers...
Develop a C++ class called PrimeNumberGenerator. An object of this class will generate successive prime numbers on request. Think of the object as being similar to the digital tasbeeh counter. It has a reset button which makes the counter return to 0. There is also a button to get the next prime number. On the tasbeeh, pressing this button increments the current value of the counter. In your object, pressing this button would generate the next prime number for display....
We have created an ArrayList of Person class. write a method called push that pushes all...
We have created an ArrayList of Person class. write a method called push that pushes all the people with the even length last name to the end of the ArrayList Content of the ArrayList before push [alex Bus, Mary Phillips, Nik Lambard, Rose Rodd, Esa khan, Jose Martinex, Nik Patte] content of the ArrayList after the push method [alex Bus, Nik Lambard, Nik Patte, Mary Phillips, Rose Rodd, Esa khan, Jose Martinex] import java.util.*; class Person { private String name;...
Make a class whose objects each represent a battery. The only attribute an object will have...
Make a class whose objects each represent a battery. The only attribute an object will have is the battery type (one letter). Also, the class has to include the following services for its objects: -determine if two batteries are equal -change battery type -get battery type display battery information the program must include: 1.All / Part 2.Overloading of operators 3.Builder 4.Copy constructor 5.Destroyer 6.Dynamic memory C++
Consider the following program that creates an ArrayList of objects of a type A, and sorst...
Consider the following program that creates an ArrayList of objects of a type A, and sorst them. Supply the missing code. Sample output when you run the program is shown below. import java.util.*; class A { int i, j, k; public A(int i, int j, int k){ this.i=i; this.j=j; this.k=k; } public String toString(){ return "A("+i+","+j+","+k+")"; } } class SortA { public static void main(String[] args){ ArrayList aL=new ArrayList(); Random rand= new Random(1000); //1000 is a seed value for (int...
How algorithms address object-oriented classes and objects. What is the File object? How are File objects...
How algorithms address object-oriented classes and objects. What is the File object? How are File objects used in algorithms? 175 words minumum please :)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT