Question
In Java Code Needed is Below the Question (don't change the code given please) Using Program 3: Encapsulating Dogs, modify this program to create a
In Java
Code Needed is Below the Question (don't change the code given please)
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.
- The first class: Create a Class representing the information that is associated with one item (one dog) of your database.
- Name this class Dog.
- 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.
- The breed, name, and age must be private.
- Within the Class for one item (one dog), include the following methods:
- A constructor that accepts the information about one item (a dog) and assigns it to the instance data within this Class.
- 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).
- All of the sets and gets for the breed, name, and age of the dog.
- 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:
- Create an array of the Objects from the Class of one item.
- Create a constructor to initialize the number of items in the array to zero.
- Create a method to add one item at a time to the array.
- Create a method to display the contents of the entire array. Call this method toString.
- Create a method to increase the size of the array if it is getting full.
- 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.
- Create a method to save the data back to the same file. You must use the File I/O methods introduced in this module.
- The third class: Create a Driver Class with a Menu with the following options:
- Add an item (a dog)
- Print out a list of all items (the list of all dogs)
- Exit.
- Instantiate an instance of Kennel within the Driver.
- Use ONLY the methods within Kennel to add a dog or print out the dogs. The Driver can only access the methods within Kennel.
- DO NOT instantiate an instance of Dog within the Driver.
- 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.
- 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.
- You will submit the three classes and the external file with program.
- 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; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started