Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java, with the programs provided please take the main method out of the Kennel class and create a driver class that contains the main

In Java, with the programs provided please take the main method out of the Kennel class and create a driver class that contains the main method... so there will be 3 total classes. Also please include the external file containing dog information.

Dog class:

package Dog;

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

}

Kennel class:

package dog;

import Dog.Dog; import java.io.*; import java.util.Scanner;

public class Kennel { //Create an array of the Objects from the Class of one item. private Dog[] dogs; // file to read from and write to private static String FILE_PATH = "D:\\dogs.txt";

public Kennel() { //Create a constructor to initialize the number of items in the array to zero. dogs = new Dog[0]; }

//Create a method to add one item at a time to the array. public void addAdog(Dog dog) { int i = 0; for (; i < dogs.length; i++) { if (dogs[i] == null) { dogs[i] = dog; return; } } resize(); for (int j = 0; j < dogs.length; j++) { if (dogs[j] == null) { dogs[j] = dog; return; } } }

//Create a method to display the contents of the entire array. Call this method toString. public void displayAllDogs() { int i = 0; while (dogs[i] != null) { System.out.println(dogs[i++]); } }

//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. public void readDogDataFromFile() {

File file = new File(FILE_PATH); try { Scanner scanner = new Scanner(file); while (scanner.hasNext()) {

String[] dogData = scanner.nextLine().split("\\s+"); String dogName = dogData[0]; String dogBreed = dogData[1]; int dogAge = Integer.parseInt(dogData[2]);

Dog aDog = new Dog(dogName, dogBreed, dogAge);

addAdog(aDog); }

} catch (FileNotFoundException e) { e.printStackTrace(); }

}

//Create a method to save the data back to the same file. You must use the File I/O methods introduced in this module. public void writeDogDataToFile() {

try { BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH)); int i = 0; while (dogs[i] != null) { String name = dogs[i].getName(); String breed = dogs[i].getBreed(); int dogAge = dogs[i].getAge(); String line = name + " " + breed + " " + dogAge + " "; writer.write(line); }

} catch (IOException e) { e.printStackTrace(); }

}

//Create a method to increase the size of the array if it is getting full. public void resize() { Dog[] newDogArray = new Dog[dogs.length + 10]; for (int j = 0; j < dogs.length; j++) { newDogArray[j] = dogs[j]; } dogs = newDogArray; }

public static void main(String args[]) { Scanner input = new Scanner(System.in); Dog dog1 = new Dog("Chihuahua", "Kennel", 4); int choice = Menu(); 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 = Menu(); 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 Menu() { 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 your 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

Step: 3

blur-text-image

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

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions