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

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...
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++
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 :)
java please Write a program that creates an ArrayList and adds 5 circle objects to the...
java please Write a program that creates an ArrayList and adds 5 circle objects to the list , and display all elements in the list by invoking the object’s toString() method.
You can follow the discussion of the java.util package's ArrayList class from the textbook or the...
You can follow the discussion of the java.util package's ArrayList class from the textbook or the lecture, but I'm hoping you will be prompted to review the Java online documentation at: https://docs.oracle.com/en/java/javase/11/docs/api/allclasses.html (Links to an external site.) You can do this exercise mentally, but it might be better if you write a Java program to implement an ArrayList instance of type String by performing the following operations: Instantiate an ArrayList containing type String members. add "a" add "day" add "bad"...
Demonstrate the following: Usage of Eclipse Variable Assignment Looping (for, while etc) ArrayList (using object) Object...
Demonstrate the following: Usage of Eclipse Variable Assignment Looping (for, while etc) ArrayList (using object) Object based programming User Interaction with multiple options Implementing Methods in relevant classes Instructions Start with below java files: Item.java Pages //add package statement here public class Item { private int id; private double price; public Item(int id, double price){ this.id = id; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double...
Develop the getSuggestions(ArrayList wordCountList) method as the base method of the class. Develop the countWords(ArrayList wordList)...
Develop the getSuggestions(ArrayList wordCountList) method as the base method of the class. Develop the countWords(ArrayList wordList) method to find the frequencies of all words in the text file. Develop the getWordList(File[] fileArray) method to get all words in the text file. Ignore the words that have 3 or less characters. Your customer wants you to develop a method that will find the sentences that contain a specific word. This is basically a word search, but your customer needs the list...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date object, a ClockWithAudio object, a BMI object, a Day object, and a FigurePane object. Then display all elements in the list. Assume that all classes (i.e. Date, Account, etc.) have their own no-argument constructor.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT