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++
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"...
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...
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...
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.
Which charging method for objects involves touching a neutral object with a charged object and results...
Which charging method for objects involves touching a neutral object with a charged object and results in both objects sharing the same charge? Select one: a. zap charging b. Inductive charging c. Contact charging d. Frictional charging Which charging method for objects involves bringing a neutral conductor near but not touching a charged object and then grounding one side of the conductor and removing the ground then the charged object resulting in the conductor having a net charge? Select one:...
If the size and shape of two objects A & B are the same, but object...
If the size and shape of two objects A & B are the same, but object B has a smaller mass: (a) for which object would the beta factor be larger? Justify your answer. (b) Which object would have a larger terminal velocity? Justify your answer. (c) Using a-b: if you dropped styrofoam ball and a steel ball of the same size/shape off a tall building, which would land land first, discounting air resistance? How about including air resistance? beta=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT