Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java Code Needed is Below the Question Please Provide External File Using Program 3: Encapsulating Dogs, modify this program to create a database of

In Java

Code Needed is Below the Question

Please Provide External File

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; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions

Question

=+ How can they be incorporated into social media content?

Answered: 1 week ago