Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a JAVA program that reads records of type PetRecord from the file created

ID: 643688 • Letter: W

Question

Write a JAVA program that reads records of type PetRecord from the file created below and outputs the following information to the screen: the name and weight of the largest pet, the name and weight of the smallest pet, the name and age of the youngest pet, and the name and age of the oldest pet. Could there be pets with the same name, weight and age?

/***
Class for basic pet records: name, age, and weight.
*/
public class PetRecord
{
private String name;
private int age; //in years
private double weight; //in pounds

public void writeOutput()
{
System.out.println("Name: " + name);
System.out.println("Age: " + age + " years");
System.out.println("Weight: " + weight + " pounds");
}

public PetRecord(String initialName, int initialAge,
double initialWeight)
{
name = initialName;
if ((initialAge < 0) || (initialWeight < 0))
{
System.out.println("Error: Negative age or weight.");
System.exit(0);
}
else
{
age = initialAge;
weight = initialWeight;
}
}

public void set(String newName, int newAge, double newWeight)
{
name = newName;
if ((newAge < 0) || (newWeight < 0))
{
System.out.println("Error: Negative age or weight.");
System.exit(0);
}
else
{
age = newAge;
weight = newWeight;
}
}

public PetRecord(String initialName)
{
name = initialName;
age = 0;
weight = 0;
}

public void setName(String newName)
{
name = newName;
}

public PetRecord(int initialAge)
{
name = "No name yet.";
weight = 0;
if (initialAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = initialAge;
}

public void setAge(int newAge)
{
if (newAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = newAge;
}

public PetRecord(double initialWeight)
{
name = "No name yet";
age = 0;
if (initialWeight < 0)
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = initialWeight;
}

public void setWeight(double newWeight)
{
if (newWeight < 0)
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = newWeight;
}

public PetRecord()
{
name = "No name yet.";
age = 0;
weight = 0;
}

public String getName()
{
return name;
}

public int getAge()
{
return age;
}

public double getWeight()
{
return weight;
}
}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;

public class PetRecordFile {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int option;
String name = "";
int age = 0;
double weight = 0.0;
System.out.println("1. read from file");
System.out.println("2. write to file");
System.out.print("choose an option: ");
option = in.nextInt();
System.out.print("Enter file name: ");
String fileName = in.next();
if (option == 1) {
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String strLine;
PetRecord p = new PetRecord();
while ((strLine = br.readLine()) != null) {
String StringArr[] = strLine.split(" ");
for (int i = 0; i < StringArr.length; ++i) {
if (i == 0) {
name = StringArr[i];
} else if (i == 1) {
age = Integer.parseInt(StringArr[i]);
} else if (i == 2) {
weight = Double.parseDouble(StringArr[i]);
}
}
p.set(name, age, weight);
p.writeOutput();
System.out.println();
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
} else if (option == 2) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
in.nextLine();
while (true) {
System.out.print("Enter Pet's Name: ");
name = in.nextLine();
System.out.print("Enter " + name + "'s Age: ");
age = in.nextInt();
System.out.print("Enter " + name + "'s Weight: ");
weight = in.nextDouble();
bw.write(String.format("%s %d %.2f ", name, age, weight));
System.out.print("Do you want to enter one more record('yes' if yes)");
in.nextLine();
String choice = in.nextLine();
if (!choice.equalsIgnoreCase("Yes")) {
break;
}
}
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.exit(0);
}
}
}

Explanation / Answer

Program code:

/***

Class for basic pet records: name, age, and weight.

*/

public class PetRecord

{

     private String name;

     private int age; //in years

     private double weight; //in pounds

     public void writeOutput()

     {

          System.out.println("Name: " + name);

          System.out.println("Age: " + age + " years");

          System.out.println("Weight: " + weight + " pounds");

     }

     public PetRecord(String initialName, int initialAge,

              double initialWeight)

     {

          name = initialName;

          if ((initialAge < 0) || (initialWeight < 0))

          {

              System.out.println("Error: Negative age or weight.");

              System.exit(0);

          }

          else

          {

              age = initialAge;

              weight = initialWeight;

          }

     }

     public void set(String newName, int newAge, double newWeight)

     {

          name = newName;

          if ((newAge < 0) || (newWeight < 0))

          {

              System.out.println("Error: Negative age or weight.");

              System.exit(0);

          }

          else

          {

              age = newAge;

              weight = newWeight;

          }

     }

     public PetRecord(String initialName)

     {

          name = initialName;

          age = 0;

          weight = 0;

     }

     public void setName(String newName)

     {

          name = newName;

     }

     public PetRecord(int initialAge)

     {

          name = "No name yet.";

          weight = 0;

          if (initialAge < 0)

          {

              System.out.println("Error: Negative age.");

              System.exit(0);

          }

          else

              age = initialAge;

     }

     public void setAge(int newAge)

     {

          if (newAge < 0)

          {

              System.out.println("Error: Negative age.");

              System.exit(0);

          }

          else

              age = newAge;

     }

     public PetRecord(double initialWeight)

     {

          name = "No name yet";

          age = 0;

          if (initialWeight < 0)

          {

              System.out.println("Error: Negative weight.");

              System.exit(0);

          }

          else

              weight = initialWeight;

     }

     public void setWeight(double newWeight)

     {

          if (newWeight < 0)

          {

              System.out.println("Error: Negative weight.");

              System.exit(0);

          }

          else

              weight = newWeight;

     }

     public PetRecord()

     {

          name = "No name yet.";

          age = 0;

          weight = 0;

     }

     public String getName()

     {

          return name;

     }

     public int getAge()

     {

          return age;

     }

     public double getWeight()

     {

          return weight;

     }

}

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.util.Scanner;

import java.util.*;

public class PetRecordFile

{

     public static void main(String args[])

     {

          Scanner in = new Scanner(System.in);

          //create an arraylist of PetRecord objects

          //so that to compare or to retrieve the data will be easy

          ArrayList<PetRecord> pr=new ArrayList<PetRecord>();

          int option;

          String name = "";

          int age = 0;

          double weight = 0.0;

          System.out.println("1. read from file");

          System.out.println("2. write to file");

          System.out.print("choose an option: ");

          option = in.nextInt();

          System.out.print("Enter file name: ");

          String fileName = in.next();

          if (option == 1)

          {

              try

              {

                   BufferedReader br = new BufferedReader(new FileReader(fileName));

                   String strLine;

                   PetRecord p = new PetRecord();

                   while ((strLine = br.readLine()) != null)

                   {

                        String StringArr[] = strLine.split(" ");

                        for (int i = 0; i < StringArr.length; ++i)

                        {

                             if (i == 0)

                             {

                                  name = StringArr[i];

                             } else if (i == 1)

                             {

                                  age = Integer.parseInt(StringArr[i]);

                             } else if (i == 2)

                             {

                                  weight = Double.parseDouble(StringArr[i]);

                             }

                        }

                        p.set(name, age, weight);

                        p.writeOutput();

                        //after setting the data for a single PetRecord object

                        //add the object to the array list

                        pr.add(p);

                        System.out.println();

                   }

                   br.close();

              }

              catch (Exception e)

              {

                   e.printStackTrace();

              }

          }

          else if (option == 2)

          {

              try

              {

                   BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));

                   in.nextLine();

                   while (true)

                   {

                        System.out.print("Enter Pet's Name: ");

                        name = in.nextLine();

                        System.out.print("Enter " + name + "'s Age: ");

                        age = in.nextInt();

                        System.out.print("Enter " + name + "'s Weight: ");

                        weight = in.nextDouble();

                        bw.write(String.format("%s %d %.2f ", name, age, weight));

                        System.out.print("Do you want to enter one more record('yes' if yes)");

                        in.nextLine();

                        String choice = in.nextLine();

                        if (!choice.equalsIgnoreCase("Yes"))

                        {

                             break;

                        }

                   }

                   bw.close();

              }

              catch (Exception e)

              {

                   e.printStackTrace();

              }

          }

          else

          {            

              System.exit(0);

          }

          //call the functions to print the largest and smallest weight

          //youngest and oldest pet

          //and with same data

          System.out.println("The smallest weight pet is: ");

          printSmallestPetData(pr);

          System.out.println("The largest weight pet is: ");

          printLargestPetData(pr);

          System.out.println("The youngest weight pet is: ");

          printYoungestPetData(pr);

          System.out.println("The oldest weight pet is: ");

          printOldestPetData(pr);

          System.out.println("The pet with same weight, name, age is: ");

          printSamePetData(pr);

     }

     //printSmallestPetData contains the logic to find the smallest weight

     //by passing the ArrayList objects

     public static void printSmallestPetData(ArrayList<PetRecord> p)

     {

          double smallWeight=p.get(0).getWeight();

          int index=0;

          for(int i=0;i<p.size();i++)

          {

              if(p.get(i).getWeight()< smallWeight)

              {

                   smallWeight=p.get(i).getWeight();

                   index=i;

              }

          }

          p.get(index).writeOutput();

     }

     //printLargestPetData contains the logic to find the largest weight

     //by passing the ArrayList objects

     public static void printLargestPetData(ArrayList<PetRecord> p)

     {

          double largeWeight=p.get(0).getWeight();

          int index=0;

          for(int i=0;i<p.size();i++)

          {

              if(p.get(i).getWeight()> largeWeight)

              {

                   largeWeight=p.get(i).getWeight();

                   index=i;

              }

          }   

          p.get(index).writeOutput();

     }

     //printYoungestPetData contains the logic to find the youngest weight

     //by passing the ArrayList objects

     public static void printYoungestPetData(ArrayList<PetRecord> p)

     {

          double youngAge=p.get(0).getWeight();

          int index=0;

          for(int i=0;i<p.size();i++)

          {

              if(p.get(i).getAge()< youngAge)

              {

                   youngAge=p.get(i).getAge();

                   index=i;

              }

          }

          p.get(index).writeOutput();

     }

     //printOldestPetData contains the logic to find the oldest weight

     //by passing the ArrayList objects

     public static void printOldestPetData(ArrayList<PetRecord> p)

     {

          double oldAge=p.get(0).getWeight();

          int index=0;

          for(int i=0;i<p.size();i++)

          {

               if(p.get(i).getAge()> oldAge)

              {

                   oldAge=p.get(i).getAge();

                   index=i;

              }

          }

          p.get(index).writeOutput();

     }

     //printSamePetData contains the logic to find the similar weight, age, and name

     //by passing the ArrayList objects

     public static void printSamePetData(ArrayList<PetRecord> p)

     {

          int index=0, index1=0;

          boolean flag=false;

          for(int i=0;i<p.size();i++)

          {

              for(int j=1;j<p.size();j++)

              {

                   if(p.get(i).getName().equalsIgnoreCase(p.get(j).getName()))

                   {

                        if(p.get(i).getAge()==p.get(j).getAge())

                        {

                             if(p.get(i).getWeight()==p.get(j).getWeight())

                             {

                                  index=i;

                                  index1=j;

                                  flag=true;

                                  break;

                             }

                        }

                   }

              }

          }

          if(!flag)

              System.out.println("Sorry! No pet is found.");

          else

          {

              p.get(index).writeOutput();

              p.get(index1).writeOutput();

          }

     }

}

Sample Input file: PetsRecord.txt

Cat-Rubby 4    6

Dog-Millar    2    23

Cat-Chow 3    8

Parrot-Mick   1    3

Cat-Toddel    4    10

Dog-Optmus    5    40

Dog-Gibber    2    15

Parrot-Minu   6    3

Dog-Gibber    2    15

Sample Output:

1. read from file

2. write to file

choose an option: 1

Enter file name: PetsData.txt

Name: Cat-Rubby

Age: 4 years

Weight: 6.0 pounds

Name: Dog-Millar

Age: 2 years

Weight: 23.0 pounds

Name: Cat-Chow

Age: 3 years

Weight: 8.0 pounds

Name: Parrot-Mick

Age: 1 years

Weight: 3.0 pounds

Name: Cat-Toddel

Age: 4 years

Weight: 10.0 pounds

Name: Dog-Optmus

Age: 5 years

Weight: 40.0 pounds

Name: Dog-Gibber

Age: 2 years

Weight: 15.0 pounds

Name: Parrot-Minu

Age: 6 years

Weight: 3.0 pounds

Name: Dog-Gibber

Age: 2 years

Weight: 15.0 pounds

The smallest weight pet is:

Name: Dog-Gibber

Age: 2 years

Weight: 15.0 pounds

The largest weight pet is:

Name: Dog-Gibber

Age: 2 years

Weight: 15.0 pounds

The youngest weight pet is:

Name: Dog-Gibber

Age: 2 years

Weight: 15.0 pounds

The oldest weight pet is:

Name: Dog-Gibber

Age: 2 years

Weight: 15.0 pounds

The pet with same weight, name, age is:

Name: Dog-Gibber

Age: 2 years

Weight: 15.0 pounds

Name: Dog-Gibber

Age: 2 years

Weight: 15.0 pounds

Note:

The added data is highlighted in the bold letters.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote