Question

In: Computer Science

Create a program that creates a sorted list from a data file. The program will prompt...

Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file.

Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array.

The group class should have the following private data elements:

first name ,last name age

The group class should have the following functions:

Constructor(s)

Destructor - optional

Get - read first name, last name, and age from an input stream

Put - write last name, first name, and age to an output stream

The group class should have the following operators:

  • > < == compare the group to another group, using Last name, First name, then age, and return a bool

Your program should do the following:

  • Prompt the user for the name of the file
  • Open the file
  • Read the data into the array of group objects (maximum size 20)
  • Close the file
  • Sort the array
  • Display the array

==============

text file

Ann ember 70
jacob Mark 68
David smith 45
Frank lee 37
John doe 30
Kathleen honor 34
bob ember 42
bob ember   13
Richard start 47
Susan hox 36

Solutions

Expert Solution

Hi as there is no specific progamming language mentioned I am writing it with python.

class ReadFile:
   def __init__(self):
       self.path = input("Enter file name: ")
       self.group = []

   def readData(self):
       with open(self.path, 'r') as f:
           for line in f:
               user = line.split(" ")
               userObj = {
                   "firstName": user[0],
                   "lastName": user[1],
                   "age": user[2].replace('\n', '')
               }
               # print(user)
               self.group.append(userObj)

   def sortData(self):
       self.sortedGroup = sorted(self.group, key=lambda k: (k['lastName'], k['lastName'], k['age']))
  
   def printGroup(self):
       for group in self.sortedGroup:
           print(group["firstName"] + " " + group["lastName"] + " " + group["age"])


if __name__ == "__main__":
   reader = ReadFile()
   reader.readData()
   reader.sortData()
   reader.printGroup()


Related Solutions

Write a program that creates a concordance. There will be two ways to create a concordance. The first requires a document to be read from an input file, and the concordance data is written to an output file.
Concepts tested by this program            Hash Table,            Link List,hash code, buckets/chaining,exception handling, read/write files (FileChooser)A concordance lists every word that occurs in a document in alphabetical order, and for each word it gives the line number of every line in the document where the word occurs.Write a program that creates a concordance. There will be two ways to create a concordance. The first requires a document to be read from an input file, and the concordance data is written to...
Create a program that will loop and prompt to enter the highlighted data items in the...
Create a program that will loop and prompt to enter the highlighted data items in the structure below. This is every item except customerNumber , isDeleted and newLine; const int NAME_SIZE = 20; const int STREET_SIZE = 30; const int CITY_SIZE = 20; const int STATE_CODE_SIZE = 3; struct Customers { long customerNumber; char name[NAME_SIZE]; char streetAddress_1[STREET_SIZE]; char streetAddress_2[STREET_SIZE]; char city[CITY_SIZE]; char state[STATE_CODE_SIZE]; int zipCode;     char isDeleted;     char newLine; }; Always set the item isDeleted to 'N' and...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
// priorityList.java // a priority queue based on a sorted list // to run this program:...
// priorityList.java // a priority queue based on a sorted list // to run this program: C>java PiorityQApp //////////////////////////////////////////////////////////////// class Link { public long dData; // data item public Link next; // next link in list // ------------------------------------------------------------- public Link(long dd) // constructor { dData = dd; } // ------------------------------------------------------------- public void displayLink() // display this link { System.out.print(dData + " "); } } // end class Link //////////////////////////////////////////////////////////////// class SortedList { private Link first; // ref to first item...
Add an item to a Text File / C++ the program will prompt for a filename...
Add an item to a Text File / C++ the program will prompt for a filename AND then prompt for a text file item. text file items always contain an unknown amount of spaces, (see getline). Allow the user to put both the filename and the extensions, like groceries.txt or retirementToDo.clist Don't add any directory information to the filenames, use them as typed. If the file does exist, the item will be APPENDED to the end of the file. If...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100 random integers between -50 and +50 (inclusive) to the file. Be sure to handle any file IO exceptions. Remember to close the file. Write a program that opens rand_nums.txt for input. Create two output files pos.txt and neg.txt. Read through the input file, one line at a time, converting each line into an integer (no exception handling, yet). If the number is positive, write...
Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
Create in java a program that will prompt the user to enter a weight for a...
Create in java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates infusion rates based on weight of patient in an interactive GUI application, label it HEPCALC. The patients’ weight will be the only entry from the user. To calculate the infusion rate you will multiply 12 times the weight divided by 50 for a total number. The end result will need to round up or down the whole number....
Using a minimum of 2 classes create a java program that writes data to a file...
Using a minimum of 2 classes create a java program that writes data to a file when stopped and reads data from a file when started. The data should be in a readable format and the program should work in a way that stopping and starting is irrelevant (e.g. all data doesn't have to save just the important elements.) Program should be unique and semi-complex in some way.
Grocery List Program Write a program that keeps track of the user's grocery list items. Prompt...
Grocery List Program Write a program that keeps track of the user's grocery list items. Prompt the user if they'd like to (each action is a function): See the list Display all items (if any) in the list Add item to their list Confirm with the user before adding item (y/n or yes/no) If they enter a duplicate item, notify them the item already exists Remove items from their list Confirm with the user before removing item (y/n or yes/no)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT