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

Can someone help with this for Java language please? This is an extension to the

ID: 3687156 • Letter: C

Question

Can someone help with this for Java language please? This is an extension to the class Dog example. Add a constructor to the Dog class that takes a file Scanner object as its only parameter. The file will consist of attributes of dogs. As read from the keyboard in the Dogs example. Let the new constructor read three lines from the file, using the scanner object passed by the caller, to get the attributes of a single dog, and use those values to initialize a Dog object: Name Breed Age. Remember that you will need to clear a blank line from the Scanner after reading the dog's age as an integer. Let the new constructor have the signature Dog(Scanner dogScanner) Modify the Dog class to keep track of how many Dog objects have been created since the program started. Add a static member function to return the number of Dogs in the system: public static int Get_Dog_Count() . Create a test driver called DogsFromFile. Accepts input from the keyboard for a file name. Creates a Scanner object for the file. Repeatedly invokes the new Dog constructor, passing the Scanner object as the only parameter value. Adds the resulting Dog object to an array. Upon reaching end of file, steps through the array of Dogs and outputs the attributes of each dog to the screen. Finally, invokes the new static member function of class Dog, Get_Dog_Count, to get the number of Dog objects that have been created and outputs the value to the screen. You may assume that there will be no more than 100 dogs in the file. If the file specified by the user does not exist, output an error message and let the user try again.

This is my code class Dog

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