Question

In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

In Java

Code Needed is Below the Question

Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program.

You will create 3 classes for this assignment.

  1. The first class: Create a Class representing the information that is associated with one item (one dog) of your database.
    1. Name this class Dog.
    2. Using Lab 3, this will include the information in regard to one dog. The information for one dog must include the breed, the name, the age, and the age in people years.
    3. The breed, name, and age must be private.
    4. Within the Class for one item (one dog), include the following methods:
      1. A constructor that accepts the information about one item (a dog) and assigns it to the instance data within this Class.
      2. The method that prints the information about one dog of your database. This method will keep the name of toString. NOTE: It is important that the name of this method is toString because it's one of Java's built-in methods that allows you print out information regarding your Object (in this case - a dog).
      3. All of the sets and gets for the breed, name, and age of the dog.
  1. The second class: Create another Class with an array of Objects. Name this class Kennel. Each Object within the array is the Class of one item (one dog). Using the class example of LibraryBooks, create the following methods in Kennel:
  1. Create an array of the Objects from the Class of one item.
  2. Create a constructor to initialize the number of items in the array to zero.
  3. Create a method to add one item at a time to the array.
  4. Create a method to display the contents of the entire array. Call this method toString.
  5. Create a method to increase the size of the array if it is getting full.
  6. Create a method to open an existing file and read the data into the array. You must use the File I/O methods introduced in this module.
  7. Create a method to save the data back to the same file.  You must use the File I/O methods introduced in this module.
  1. The third class: Create a Driver Class with a Menu with the following options:
    1. Add an item (a dog)
    2. Print out a list of all items (the list of all dogs)
    3. Exit.
  2. Instantiate an instance of Kennel within the Driver.
    1. Use ONLY the methods within Kennel to add a dog or print out the dogs.  The Driver can only access the methods within Kennel.
    2. DO NOT instantiate an instance of Dog within the Driver.  
  3. When your program is first executed, open the external file, read the data from it and place it in your array, then close the file.  
    • To populate the external file for the first time, hard code writing the data to the external file. You will only need to do this once - just when you've created the external file for the first time.
    • You must use the File I/O methods introduced in this module.
  4. When the Exit option is chosen from the menu in the Driver, open back up the same external file, save the array to the file (DO NOT USE toString), and then close the file.
    • You must use the File I/O methods introduced in this module.   
  5. You will submit the three classes and the external file with program.
  6. One warning - do not confuse formatting the data for printing to the screen with how you format the data within the array. When working with extracting data from an external file, you must save the data to the file in the EXACT same way you read the data from the file.  

NOTE 1: You must use the File I/O methods introduced in this module.

NOTE 2:
1. Declare all variables within the data declaration section of each class and method.
2   Do not get input on the same line as a variable declaration.  
3. Do not place an equation for computation on the same line as declaring a variable.  

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

MAIN

public class Dog
{

// private instance variables
private String dogBreed;
private String dogName;
private int dogAge;
// public instance variables
public int peopleAge;
public String description;


public Dog(String dogBreed, String dogName, int dogAge)
{
this.dogBreed = dogBreed;
this.dogName = dogName;
this.dogAge = dogAge;
// update the person years instance variable
personYears();
// update the description
toString();
}


public void setBreed(String dogBreed)
{
this.dogBreed = dogBreed;
// update the description
toString();
}


public void setName(String dogName)
{
this.dogName = dogName;
// update the description
toString();
}


public void setAge(int dogAge)
{
this.dogAge = dogAge;
// change the person years instance variable
// on age update
personYears();
// change the description accordingly
toString();
}


public String getBreed()
{
return dogBreed;
}
public String getName()
{
return dogName;
}
public int getAge()
{
return dogAge;
}


public int personYears()
{
peopleAge = dogAge * 7;
return peopleAge;
}

/**
* Method to return the dog description
*/
@Override public String toString()
{
description = "Dog Name: " + dogName +
", Breed: " + dogBreed +
", Age: " + dogAge +
", Age in people years: " + peopleAge;
return description;
}

}

DRIVER

public class Kennel
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
Dog dog1 = new Dog("Chihuahua", "Kennel", 4);
int choice = printMenuAnddGetChoice();
while(choice != 9)
{
if(choice == 1)
{
String newName;
System.out.print("Enter the new name of the dog: ");
newName = input.nextLine();
dog1.setName(newName);
}
else if(choice == 2)
{
int newAge;
System.out.print("Enter the new age of the dog: ");
newAge = Integer.parseInt(input.nextLine());
dog1.setAge(newAge);
}
else if(choice == 3)
{
String newBreed;
System.out.print("Enter the new breed of the dog: ");
newBreed = input.nextLine();
dog1.setBreed(newBreed);
}
else if(choice == 4)
{
System.out.println("Dog's name is: " + dog1.getName());
}
else if(choice == 5)
{
System.out.println("Dog's age is: " + dog1.getAge() + " year(s)");
}
else if(choice == 6)
{
System.out.println("Dog's breed is: " + dog1.getBreed());
}

else if(choice == 7)
{
System.out.println("Dog's age in people years is: " + dog1.peopleAge + " years");
}
else if(choice == 8)
{
System.out.println(dog1.description);
}
System.out.println();
choice = printMenuAnddGetChoice();
if(choice == 9)
{
System.out.println("GoodBye...");
}
System.out.println();
}

}

/*
* Helper method to print the Menu and collect the choice from user and return it
*/
public static int printMenuAnddGetChoice()
{
int choice = 0;
Scanner input = new Scanner(System.in);
while(choice < 1 || choice > 9)
{
System.out.println("MENU");
System.out.println(" ");
System.out.println("1. Update the dog's name.");
System.out.println("2. Update the dog's age.");
System.out.println("3. Update the dog's breed.");
System.out.println("4. Display the dog's name only.");
System.out.println("5. Display the dog's age only.");
System.out.println("6. Display the dog's breed only.");
System.out.println("7. Display the dog's age in people years.");
System.out.println("8. Display the description of the dog.");
System.out.println("9. Exit.");
System.out.println(" ");
System.out.print("Enter the choice: ");
choice = Integer.parseInt(input.nextLine());
if(choice < 1 || choice > 9)
{
System.out.println("Invalid choice! Please re-try.");
System.out.println();
}

}
return choice;
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student,

I have written the code as per instructions. I have modified the above code when needed. I am pasting the code below, Please test and let me know If you find any issues. It's working as expected. You may check once again modify the code if necessary. Also, I am attaching the sample input file for your testing. Please let me know if you have any further.

Note: Please update file path as per your directory in the following variable.

dogFileLoc in Kennel class. You have to update them in two places(readDogDataFromFile and writeDogDataFile functions to)

Paste the below content in dog.txt file.

pug snoopy 3
pug puppy 4
pug bunny 5

Best wishes.

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

package test;

public class Dog {

// private instance variables
   private String dogBreed;
   private String dogName;
   private int dogAge;
// public instance variables
   public int peopleAge;
   public String description;

   public Dog(String dogBreed, String dogName, int dogAge) {
       this.dogBreed = dogBreed;
       this.dogName = dogName;
       this.dogAge = dogAge;
// update the person years instance variable
       personYears();
// update the description
       toString();
   }

   public void setBreed(String dogBreed) {
       this.dogBreed = dogBreed;
// update the description
       toString();
   }

   public void setName(String dogName) {
       this.dogName = dogName;
// update the description
       toString();
   }

   public void setAge(int dogAge) {
       this.dogAge = dogAge;
// change the person years instance variable
// on age update
       personYears();
// change the description accordingly
       toString();
   }

   public String getBreed() {
       return dogBreed;
   }

   public String getName() {
       return dogName;
   }

   public int getAge() {
       return dogAge;
   }

   public int personYears() {
       peopleAge = dogAge * 7;
       return peopleAge;
   }

   /**
   * Method to return the dog description
   */
   @Override
   public String toString() {
       description = "Dog Name: " + dogName + ", Breed: " + dogBreed + ", Age: " + dogAge + ", Age in people years: "
               + peopleAge;
       return description;
   }

}
-----------------------------------------------------------------------------------

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Kennel {

   int maxDogs=2;
   Dog[] dogs=new Dog[maxDogs];
   int dogCount;// available dogs at present

   public Kennel() {// constructor
       dogCount = 0;

   }

   public void addDog(Dog dog) {// adding the dogs from input file.
       // dogCount = dogs.length;
       dogCount = 0;
       for (int i = 0; i < dogs.length; i++) {// checking how many dogs are available at the moment
           if (dogs[i] != null)
               dogCount++;
       }
       if (dogCount >= maxDogs) {
           incrementDogsArray();
           maxDogs++;
       }
       dogs[dogCount] = dog;

   }

   public void displayAllDogs() {// display dogs to the console
       for (Dog dog : dogs) {
           if (dog != null)
               System.out.println(dog);
       }

   }

   private void incrementDogsArray() {// incrementing the array when needed
       Dog[] dogsCopy = new Dog[dogCount + 1];
       for (int i = 0; i < dogs.length; ++i) {
           dogsCopy[i] = dogs[i];
       }

       dogs = dogsCopy;
   }

   void readDogDataFromFile() throws IOException {// read from text file when program is started
       String dogFileLoc = "";
       dogFileLoc = "C:\\Users\\bgunda\\desktop\\dog.txt";
       File dogFile = new File(dogFileLoc);
       FileReader fileReader = new FileReader(dogFile);
       BufferedReader bufferedReader = new BufferedReader(fileReader);
       String line = null;
       while ((line = bufferedReader.readLine()) != null) {
           String[] splitLine = line.split(" ");
           String dogAgeString = splitLine[1].toString();
           Dog dog = new Dog(splitLine[0], splitLine[1], Integer.parseInt(splitLine[2]));
           addDog(dog);
       }

       bufferedReader.close();
       fileReader.close();

   }

   void writeDogDataFile() throws IOException {// writing back to the text file
       String dogFileLoc = "";
       dogFileLoc = "C:\\Users\\bgunda\\desktop\\dog.txt";
       FileWriter writer = new FileWriter(dogFileLoc);
       BufferedWriter buffer = new BufferedWriter(writer);

       for (int iter = 0; iter < dogs.length; iter++) {
           buffer.write(dogs[iter].getBreed() + " " + dogs[iter].getName() + " " + dogs[iter].getAge());
           buffer.write("\n");
       }
       if (buffer != null)
           buffer.close();

       if (writer != null)
           writer.close();

   }

   public void addDog(String breed, String name, int age) {// adding dog to the array when reading from the console
       Dog dog = new Dog(breed, name, age);
       addDog(dog);
   }

}

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

package test;

import java.io.IOException;
import java.util.Scanner;

public class DogDriver {
   public static void main(String args[]) throws IOException {
       Kennel kennel = new Kennel();

       kennel.readDogDataFromFile();
       int choice = printMenuAnddGetChoice();//Getting input from user
       Scanner input = new Scanner(System.in);
       while (choice < 3&&choice>=1) {
           if (choice == 1) {

               System.out.println("Enter breed");
               String breed = "";
               breed = input.next();
               System.out.println("Enter Name");

               String name = "";
               name = input.next();
               System.out.println("Enter Age");

               int age = 0;
               age = Integer.parseInt(input.next());

               kennel.addDog(breed, name, age);

           } else if (choice == 2) {
               kennel.displayAllDogs();
           }
           System.out.println();
           choice = printMenuAnddGetChoice();
       }
       if (choice == 3) {//when exit is requested
           System.out.println("GoodBye...");
           kennel.writeDogDataFile();//writing back to data file
       }

   }

   public static int printMenuAnddGetChoice() {
       int choice = 0;
       Scanner input = new Scanner(System.in);
       while (choice < 1 || choice > 3) {
           System.out.println("MENU");
           System.out.println(" ");
           System.out.println("1 Add dog details");
           System.out.println("2. Display dog details");
           System.out.println("3 Exit ");
           System.out.print("Enter the choice: ");
           choice = Integer.parseInt(input.nextLine());
           if (choice < 1 || choice > 9) {
               System.out.println("Invalid choice! Please re-try.");
               System.out.println();
           }

       }
       return choice;
   }
}

Add a comment
Know the answer?
Add Answer to:
In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and...

    Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and name variables (do not include topTrick). Note: The type refers to what the breed typically does; for example, a corgi would be a “cattle herding dog.” A Shiba Inu would be a “hunting dog.” Create the setTopTrick() mutator method Dog is parent class Corgi and Driver are subclasses Complete the Corgi class: Using the UML Class diagram, declare the instance variables. Create the two...

  • Chapter 5 Assignment Read directions: In JAVA design and implement a class called Dog that contains...

    Chapter 5 Assignment Read directions: In JAVA design and implement a class called Dog that contains instance data that represents the dog's name and dog's age. Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age. Include a method to compute and return the age of the dog in "person years" (seven times age of dog. Include a toString method that returns a one-time description of the dog (example below)....

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • LAB: Pet information (derived classes)

    LAB: Pet information (derived classes)The base class Pet has private fields petName, and petAge. The derived class Dog extends the Pet class and includes a private field for dogBreed. Complete main() to:create a generic pet and print information using printInfo().create a Dog pet, use printInfo() to print information, and add a statement to print the dog's breed using the getBreed() method.Ex. If the input is:Dobby 2 Kreacher 3 German Schnauzerthe output is:Pet Information:     Name: Dobby    Age: 2 Pet Information:     Name: Kreacher    Age: 3    Breed: German SchnauzerPetInformation.javaimport java.util.Scanner; public class PetInformation {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       Pet myPet = new Pet();       Dog myDog = new Dog();              String petName, dogName, dogBreed;       int petAge, dogAge;       petName = scnr.nextLine();       petAge = scnr.nextInt();       scnr.nextLine();...

  • Could someone help me out. I am not sure what I should be doing. Seeing it...

    Could someone help me out. I am not sure what I should be doing. Seeing it worked out will allow me to understand what I should be doing and then I can complete it on my own. Usando 2. Complete the Dog Class: a. Using the UML Class diagram to the right declare the instance variables. A text version is available: UML Class Diagram Text Version b. Create a constructor that incorporates the type, breed, and name variables (do not...

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT