Create a text file named “pets.txt” with 10 pets. The format is the same format
ID: 3695757 • Letter: C
Question
Create a text file named “pets.txt” with 10 pets. The format is the same format used in the original homework problem. Each line in the file should contain:
· pet name (String)
· comma
· pet’s age (int) in years
· comma
· pet’s weight (double) in pounds
Use the PetRecord.java program from the original problem. (Download it from Blackboard).
Perform the following operations in the sequence given below:
1. Create a driver program in a separate source file which contains the main() method.
2. Declare and create an array of 10 PetRecord objects
3. Open the “pets.txt” file (stored in the same file as your programs’ “class” files)
4. Read each line in the file, instantiate a PetRecord object, and assign to the next unused element in the array of PetRecord objects.
5. Close the “pets.txt” file
6. Print a report showing the name, weight, and age of all of the pets in the array:
a. Do not use the toString() method in PetRecord.
b. Format the report using nicely-aligned columns,
i. The pet names should all be left-aligned
ii. The weights and ages of the pets should all be right-aligned.
iii. The weights should be displayed to 1 decimal point to the right
7. Determine and print the name and weight of the heaviest pet and the oldest pet
8. Determine and print the name of the pet with the longest name
Do not determine or print any of the information requested in steps 6, 7, or 8 as you are reading in the data. These must be determined and printed only after all pets have been read from the file and placed into the array.
Also, don’t make any changes to the PetRecord class. You may create as many methods in your driver program as you wish.
PetRecord class
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author prmsh
*/
public class Driver {
public static void main(String args[]) {
//declaring arrylist
ArrayList<PetRecord> list = new ArrayList<PetRecord>();
try {
File file = new File("pets.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
String str = scanner.nextLine();
String[] data = str.split(",");
PetRecord obj = new PetRecord(data[0],Integer.parseInt(data[1]),Double.parseDouble(data[2]));
list.add(obj);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//printingn data
for(int i=0;i<list.size();i++){
System.out.println("Name: "+list.get(i).getName()+" Age: "+list.get(i).getAge()+" Weight: "+list.get(i).getWeight()+" ");
}
//calculating heaviest and weight
String heaviest = "", oldest = "";
double weight = 0;
int age = 0;
for(int i=0;i<list.size();i++){
if(list.get(i).getWeight() > weight){
heaviest = list.get(i).getName();
}
if(list.get(i).getAge()> age){
oldest = list.get(i).getName();
}
}
//printing results
System.out.println("Heaviest: "+heaviest);
System.out.println("Oldest: "+oldest);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.