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
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
public class Dog { // 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 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; } //------------------------------------------------------ // Returns a string representation of a dog. //------------------------------------------------------ public String toString() { return name + "\t"+ breed + "\t" + age + " age in person years:" + ageInPersonYears(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
