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

Need some help with some java code i need to add a constructor to the Dog class

ID: 3686989 • Letter: N

Question

Need some help with some java code i need to 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. 3 Assignment 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() 4 Assignment 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. 5 DogsFromFile.java (continued) 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. 6 DogsFromFile.java 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.

These are the two given codes to start with

-------------------------------------

Explanation / Answer

############# Dog.java ################

import java.util.Scanner;

public class Dog {
  
   // Instance variables
    private String name;
    private String breed;
    private int age;  

  
   private static int dogCount = 0; // count number of dogs
  
   public Dog(Scanner dogScanner) {
      
       name = dogScanner.nextLine();
       breed = dogScanner.nextLine();
       age = Integer.parseInt(dogScanner.nextLine().trim());
      
       dogCount++;
   }
  
   public int ageInPersonYears()
    {
        int personAge = age*7;
        return personAge;
    }
   
    public String toString()
    {
        return name + " "+ breed + " " + age +
            " age in person years:" + ageInPersonYears();
    }
  
   public static int Get_Dog_Count(){
       return dogCount;
   }

}

################### DogdFromFile ################

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

public class DogsFromFile {

   public static void main(String[] args) throws FileNotFoundException {

       Scanner input = new Scanner(new File("dogs.txt"));
      
       Dog[] dogs = new Dog[100]; // max
       int i=0;
       while (input.hasNextLine()) {
          
           dogs[i] = new Dog(input);
           i++;
       }
      
       input.close();
      
       //printing dog count
       System.out.println("Total number of dogs: "+Dog.Get_Dog_Count());
      
       // printing dog attributes
       for(int j=0; j<Dog.Get_Dog_Count(); j++)
           System.out.println(dogs[j].toString());
   }

}

I am reading dogs information from dogs.txt file:

content of dogs.txt file is:

name1

brred1

age1

name2

breed2

age2

..... etc

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote