The instructions for the assignment are below followed by my code so far. This s
ID: 3690556 • Letter: T
Question
The instructions for the assignment are below followed by my code so far. This should be easy i tried to organize instructions as well as possible
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)
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.
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");
BELOW IS MY CODE
//************************************************
// Dog.java
//
// Represents a dog.
//
//************************************************
import java.util.Scanner;
public class Dog
{
private static int Dog_Count = 0;
// Instance variables
private String name;
private String breed;
private int age;
//-----------------------------------------------------
// Constructor - sets up a dog object by initializing
// the name, the breed, and the age.
//-----------------------------------------------------
public Dog(String name, String breed, int age)
{
this.name = name;
this.breed = breed;
this.age = age;
Dog_Count++;
}
//-----------------------------------------------------
// Constructor - sets up a dog object by initializing
// the name, the breed, and the age from a file.
//-----------------------------------------------------
public Dog(Scanner dogScanner)
{
this.name = dogScanner.nextLine();
this.breed = dogScanner.nextLine();
this.age = dogScanner.nextInt();
dogScanner.nextLine();
Dog_Count++;
}
//--------------------------------------------------------------
// 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;
}
//------------------------------------------------------
// Returns a string representation of a dog.
//------------------------------------------------------
public String toString()
{
return name + " "+ breed + " " + age +
" age in person years:" + ageInPersonYears();
}
public static int Get_Dog_Count()
{
return Dog_Count;
}
}
THIS IS THE TEST DRIVER
//**************************************************
// DogsFromFile.java
// A test driver for class Dog.
//**************************************************
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class DogsFromFile
{
//-----------------------------------------------
// Creates an array of Dog objects and then outputs
// the attributes of all of the dogs in the array.
//-----------------------------------------------
public static void main (String[] args)
{
Dog[] dogs = new Dog[100];
int count = 0;
String filename = "";
Boolean ok = false;
System.out.println("This is program DogsFromFile.");
System.out.println();
while (!ok)
{
// Get file name from the user
System.out.print("Please enter file name: ");
Scanner keyboardScanner = new Scanner(System.in);
filename = keyboardScanner.nextLine();
try
{
File file = new File(filename);
Scanner fileScanner = new Scanner(file);
// Get dogs from the file.
while (fileScanner.hasNext()) // Test for end of file
{
Dog dog1 = new Dog(fileScanner);
dogs[count++] = dog1;
}
System.out.println("End of file reached");
System.out.println();
// Output the dogs from the array.
for (int i = 0; i < count; i++)
{
System.out.println(dogs[i]);
}
System.out.println();
// Output the value of the static variable Dog_Count.
System.out.println("There are " + Dog.Get_Dog_Count() +
" dogs in the system.");
ok = true;
}
catch (FileNotFoundException ex)
{
System.out.println("File " + filename + " was not found.");
System.out.println("Please try again.");
}
}
System.out.println();
System.out.println("Program complete.");
}
}
Explanation / Answer
//**************************************************
//DogsFromFile.java
// A test driver for class Dog.
//**************************************************
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
public class DogsFromCSV {
// -----------------------------------------------
// Creates an array of Dog objects and then outputs
// the attributes of all of the dogs in the array.
// -----------------------------------------------
public static void main(String[] args) throws IOException {
Dog[] dogs = new Dog[100];
int count = 0;
String filename = "";
Boolean ok = false;
System.out.println("This is program DogsFromFile.");
System.out.println();
while (!ok) {
// Get file name from the user
System.out.print("Please enter file name: ");
Scanner keyboardScanner = new Scanner(System.in);
filename = keyboardScanner.nextLine();
try {
File file = new File(filename);
String outputFile = filename.replaceAll(".csv", "2.csv");
System.out.println("Output File Name: "+outputFile);
Scanner fileScanner = new Scanner(file);
String data = null;
// Get dogs from the file.
while (fileScanner.hasNextLine()) // Test for end of file
{
data = fileScanner.nextLine();
Dog dog1 = new Dog(data);
if(dog1.isValid()) {
dogs[count++] = dog1;
} else {
System.out.println("Bad dog: "+data);
}
}
//System.out.println("End of file reached");
System.out.println();
// Output the dogs from the array.
File outfile = new File(outputFile);
// creates the file
outfile.createNewFile();
// creates a FileWriter Object
FileWriter writer = new FileWriter(outfile);
for (int i = 0; i < count; i++) {
System.out.println(dogs[i]);
// Writes the content to the file
writer.write(dogs[i].getName()+","+dogs[i].getBreed()+","+(dogs[i].getAge()+1));
writer.flush();
}
writer.close();
System.out.println();
// Output the value of the static variable Dog_Count.
System.out.println("There are " + Dog.Get_Dog_Count()
+ " dogs in the system.");
ok = true;
}
catch (FileNotFoundException ex) {
System.out.println("File " + filename + " was not found.");
System.out.println("Please try again.");
}
}
System.out.println();
System.out.println("Program complete.");
}
}
// ************************************************
// Dog.java
//
// Represents a dog.
//
// ************************************************
class Dog {
private static int Dog_Count = 0;
// Instance variables
private String name;
private String breed;
private int age;
private boolean valid;
// -----------------------------------------------------
// Constructor - sets up a dog object by initializing
// the name, the breed, and the age.
// -----------------------------------------------------
public Dog(String name, String breed, int age) {
this.name = name;
this.breed = breed;
this.age = age;
Dog_Count++;
}
// -----------------------------------------------------
// Constructor - sets up a dog object by initializing
// the name, the breed, and the age from a file.
// -----------------------------------------------------
public Dog(Scanner dogScanner) {
this.name = dogScanner.nextLine();
this.breed = dogScanner.nextLine();
this.age = dogScanner.nextInt();
dogScanner.nextLine();
Dog_Count++;
}
public Dog(String dogInfo) {
//thhis is comma seperated line in csv indicating a dog info
valid = false;
System.out.println(dogInfo);
if(dogInfo != null) {
String tokens[] = dogInfo.split(",");
if(tokens != null && tokens.length == 3) {
try {
//System.out.println("Constructor ---1");
this.name = tokens[0].trim();
this.breed = tokens[1].trim();
this.age = Integer.parseInt(tokens[2].trim());
valid = true;
Dog_Count++;
//System.out.println("Constructor ---2");
}catch(NumberFormatException e) {
//invalid integer
//e.printStackTrace();
}
} else {
//System.out.println("Error");
}
}
}
// --------------------------------------------------------------
// 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;
}
// ------------------------------------------------------
// Returns a string representation of a dog.
// ------------------------------------------------------
public String toString() {
if(valid)
return name + " " + breed + " " + age + " age in person years:"
+ ageInPersonYears();
else
return " ";
}
public static int Get_Dog_Count() {
return Dog_Count;
}
public boolean isValid() {
return valid;
}
public String getName() {
return name;
}
public String getBreed() {
return breed;
}
public int getAge() {
return age;
}
}
--output-------------
This is program DogsFromFile.
Please enter file name: D:/ravi/Cheg/dogs.csv
Output File Name: D:/ravi/Cheg/dogs2.csv
Spot, Beagel, 4
Rover, spanie, 8
john, crow
Error
Bad dog:
john, crow
snoopy, beagle, 7
jean, poodle, 9
Spot Beagel 4 age in person years:28
Rover spanie 8 age in person years:56
snoopy beagle 7 age in person years:49
jean poodle 9 age in person years:63
There are 4 dogs in the system.
Program complete.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.