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

Write a JAVA program that allows you to write and read records of type PetRecord

ID: 643531 • Letter: W

Question

Write a JAVA program that allows you to write and read records of type PetRecord to a file. The program asks the user to choose between reading and writing from a file. In either case, the program next asks for the file name. If the user asked to write to a file, they can enter as many records as desired. If the user asked to read from a file, they are shown all of the records in the file. Be sure that the records do not scroll by so quickly that the user cannot read them. Redefine the class PetRecord so that it is serializable.

/***
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;
}
}

Explanation / Answer

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);
       }
   }
}

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