Can someone help me with this project in a Java language please. This is a varia
ID: 3690486 • Letter: C
Question
Can someone help me with this project in a Java language please. 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.
Implementations Tips: Use Integer.parseInt() to convert the string representation of an integer to an integer. Example: int i = Integer.parseInt("1234"); sets the variable i to the integer value 1234. To create the output file name you can write String outfile_name = infile_name.replace(".csv", "2.csv");
*****************************************************************************************************************************************************************
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;
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();
}
}
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();
}
}
}
input.txt
dogname1
breed1
23
dogname2
breed2
24
dogname3
breed3
25
dogname4
breed4
26
OUTPUT:
Enter the File name:input.txt
dogname1 breed1 23 age in person years:161
dogname2 breed2 24 age in person years:168
dogname3 breed3 25 age in person years:175
dogname4 breed4 26 age in person years:182