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

I need help with this project in a Java language.This is a variation of the Dogs

ID: 3690631 • Letter: I

Question

I need help with this project in a Java language.This is a variation of the Dogs from File program in Project 13. Write a constructor for class Dog that takes a single line of a Comma Separated Values file as its only parameter. Initializes the Dog object with information from the CSV line. Let the new constructor have the signature Dog(String dog_info). You may use the posted solution for Project 13 as a starting point for this project, if you wish. Add a serialization method to the Dog class. Returns a single String consisting of all member variables, separated by commas, if object is valid. Returns a blank line (empty string) if object is invalid. The constructor should verify that the number of comma separated items in the input line is correct. Set private boolean member variable isValid to false if the input line does not have the right number of items. Class should include an accessor method for isValid. The Dog class should include a toString method that returns a string with the dog's information in a printable format (single line). Provides an appropriate result for an invalid object. Create a test driver called DogsFromCSV.java Accepts input from the keyboard for a file name. Creates a Scanner object for the file. Reads each line from the file and invokes the new Dog constructor, passing it the CSV line. Outputs each Dog's information using println. Adds 1 to the dog's age and writes the updated information to a new CSV file having the input file name with "2" appended. Example: dogs.txt > dogs2.txt .If the file specified by the user does not exist, output an error message and let the user try again.

Here there are the previous codes:

Code#1

import java.util.Scanner;

public class Dog
{
// Instance variables
private String name;
private String breed;
private int age;
private static int noOFDogs = 0;

public Dog(Scanner dogScanner)
{
// TODO Auto-generated constructor stub

this.name = dogScanner.next();

this.breed = dogScanner.next();

this.age = dogScanner.nextInt();

noOFDogs++;

}

// -----------------------------------------------------
// Constructor - sets up a dog object by initializing
// the name, the breed, and the age.
// -----------------------------------------------------
public Dog(String newName, String newBreed, int newAge) {
name = newName;
breed = newBreed;
age = newAge;
}

// --------------------------------------------------------------
// Method ageInPersonYears that takes no parameter. The method
// should compute and return the age of the dog in person years
// (seven times the dog's age).
// --------------------------------------------------------------
public int ageInPersonYears() {
int personAge = age * 7;
return personAge;
}

public static int Get_Dog_Count() {
return noOFDogs;

}

// ------------------------------------------------------
// Returns a string representation of a dog.
// ------------------------------------------------------
public String toString() {
return name + " " + breed + " " + age + " age in person years:"
+ ageInPersonYears();
}

}

Code #2

import java.io.File;
import java.util.Scanner;

public class DogsFromFile
{
public static void main(String[] args)
{

Scanner scanner = null, fileScanner = null;
try
   {
scanner = new Scanner(System.in);
System.out.print("Enter the File name:");
Dog dogs[] = new Dog[10];
String fileName = scanner.next();
fileScanner = new Scanner(new File(fileName));
while (fileScanner.hasNext())
       {

dogs[Dog.Get_Dog_Count()] = new Dog(fileScanner);

}
for (int i = 0; i < Dog.Get_Dog_Count(); i++)
System.out.println(dogs[i]);

}
   catch (Exception e)
   {
// TODO: handle exception
e.printStackTrace();
}

}
}

Explanation / Answer

import java.util.Scanner;

public class Dog {
   // Instance variables
   private String name;
   private String breed;
   private int age;
   private static int noOFDogs = 0;
   private boolean isValid;

   public Dog(Scanner dogScanner) {
       // TODO Auto-generated constructor stub
       this.name = dogScanner.next();
       this.breed = dogScanner.next();
       this.age = dogScanner.nextInt();
       noOFDogs++;
   }

   public Dog(String dog_info) {
       // TODO Auto-generated constructor stub
       String lineArr[] = dog_info.split(",");
       if (lineArr.length == 3) {
           this.name = lineArr[0];
           this.breed = lineArr[1];
           this.age = Integer.parseInt(lineArr[2]);
           isValid = true;
           noOFDogs++;
       } else {
           isValid = false;
       }
   }

   // -----------------------------------------------------
   // Constructor - sets up a dog object by initializing
   // the name, the breed, and the age.
   // -----------------------------------------------------
   public Dog(String newName, String newBreed, int newAge) {
       name = newName;
       breed = newBreed;
       age = newAge;
   }

   public boolean getIsValid() {

       return isValid;
   }

   public String getDogInfo() {

       if (isValid)
           return name + "," + breed + "," + age;
       else
           return "";
   }

   // --------------------------------------------------------------
   // Method ageInPersonYears that takes no parameter. The method
   // should compute and return the age of the dog in person years
   // (seven times the dog's age).
   // --------------------------------------------------------------
   public int ageInPersonYears() {
       int personAge = age * 7;
       return personAge;
   }

   /**
   * @return
   */
   public static int Get_Dog_Count() {
       return noOFDogs;
   }

   /**
   * @param age
   */
   public void setAge(int age) {

       this.age = age;
   }

   public int getAge() {

       return age;
   }

   // ------------------------------------------------------
   // Returns a string representation of a dog.
   // ------------------------------------------------------
   public String toString() {
       return name + " " + breed + " " + age + " age in person years:"
               + ageInPersonYears();
   }
}

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

public class DogsFromCSV {
   public static void main(String[] args) {
       Scanner scanner = null, fileScanner = null;
       try {
           scanner = new Scanner(System.in);
           System.out.print("Enter the File name:");
           Dog dogs[] = new Dog[10];
           String fileName = scanner.next();
           fileScanner = new Scanner(new File(fileName + ".csv"));
           File file = new File(fileName + "2.csv");
           // if file doesnt exists, then create it
           if (!file.exists()) {
               file.createNewFile();

           }

           FileWriter fw = new FileWriter(file.getAbsoluteFile());
           BufferedWriter bw = new BufferedWriter(fw);
           while (fileScanner.hasNext()) {
               dogs[Dog.Get_Dog_Count()] = new Dog(fileScanner.nextLine());
           }

           for (int i = 0; i < Dog.Get_Dog_Count(); i++) {
               dogs[i].setAge(dogs[i].getAge() + 1);
               bw.write(dogs[i].getDogInfo() + " ");

           }
           System.out.println("Data writen to " + fileName + "2.csv file");
           bw.close();
       } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
           System.out.println("Try again");
       } finally {

           scanner.close();
           fileScanner.close();
       }
   }
}

dogs.csv
name1,breed1,24
name2,breed2,29
name3,breed3,28
name4,breed4,27
name5,breed5,26
name6,breed6,25

dogs2.csv
name1,breed1,25
name2,breed2,30
name3,breed3,29
name4,breed4,28
name5,breed5,27
name6,breed6,26

OUTPUT:
Enter the File name:dogs
Data writen to dogs2.csv file