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 of all dogs in the kennel
public void adoptDog(String n) - remove the dog with given name
Create 1 more method that uses your mutator method from the dog class somehow
Create a main test - either in the DogKennel class file or in it’s own file
Test should create a DogKennel and test each of the methods you have written
Here is the Dog Class:
public class Dog {
// Instance FIelds
String name, breed;
int age;
// Constructors
// Default Constructor
public Dog() {
name = "";
breed = "";
age = 0;
}
// Parameterized Constructor
public Dog(String n, String b, int a) {
name = n;
breed = b;
age = a;
}
// Methods
// Accessor Methods
// Method to return name of Dog
public String getName() {
return name;
}
// Method to return breed of Dog
public String getBreed() {
return breed;
}
// Method to return age of Dog
public int getAge() {
return age;
}
// Mutator Method
// Method to change breed of Dog
public void changeBreed(String b) {
breed = b;
}
// Driver method main
public static void main(String args[]) {
// Instantiate the class Dog
Dog dog1 = new Dog(); // Using
Default Constructor
Dog dog2 = new Dog("Browney",
"German Shepered", 5); // Using Parameterized Constructor
// Print the current status of
dogs
System.out.println("Dog 1:
");
System.out.println("Name: " +
dog1.getName() + " Breed: " + dog1.getBreed() + " Age: " +
dog1.getAge());
System.out.println("Dog 2:
");
System.out.println("Name: " +
dog2.getName() + " Breed: " + dog2.getBreed() + " Age: " +
dog2.getAge());
// Mutate the breed of
dog1
dog1.changeBreed("Pomerian");
// Print the changed dog
System.out.println("Dog 1:
");
System.out.println("Name: " +
dog1.getName() + " Breed: " + dog1.getBreed() + " Age: " +
dog1.getAge());
}
}
Screenshot
---------------------------------------------
Program
Dog.java
public class Dog {
// Instance FIelds
String name, breed;
int age;
// Constructors
// Default Constructor
public Dog() {
name =
"";
breed =
"";
age =
0;
}
// Parameterized
Constructor
public Dog(String n, String b, int
a) {
name =
n;
breed =
b;
age =
a;
}
// Methods
// Accessor Methods
// Method to return name of
Dog
public String getName() {
return
name;
}
// Method to return breed of
Dog
public String getBreed() {
return
breed;
}
// Method to return age of
Dog
public int getAge() {
return
age;
}
// Mutator Method
// Method to change breed of
Dog
public void changeBreed(String b)
{
breed =
b;
}
}
--------------------------------------------
DogKennel.java
import java.util.ArrayList;
public class DogKennel {
//Instance field(s) - array of Dogs + any others you
may want
private ArrayList<Dog> dog;
private String kennelType;
//Contructor - default (no parameters) - will make a
DogKennel with no dogs in it
public DogKennel() {
dog=new
ArrayList<Dog>();
}
//public void addDog(Dog d) - which adds a dog to the
array
public void addDog(Dog d) {
dog.add(new
Dog(d.getName(),d.getBreed(),d.getAge()));
}
//public int currentNumDogs() - returns number of dogs
currently in kennel
public int currentNumDogs() {
return dog.size();
}
//public double averageAge() - which returns the
average age of all dogs in the kennel
public double averageAge() {
double avg=0.0;
for(int i=0;i<dog.size();i++)
{
avg+=dog.get(i).getAge();
}
return (avg/dog.size());
}
//public void adoptDog(String n) - remove the dog with
given name
public void adoptDog(String n) {
for(int i=0;i<dog.size();i++)
{
if(dog.get(i).getName().equals(n)) {
dog.remove(dog.get(i));
return;
}
}
System.out.println(n+" named dog is
not present in the kennel!!!!");
}
//Method to change breed in kennel
public void changeBreed(String n,String br) {
for(int i=0;i<dog.size();i++)
{
if(dog.get(i).getName().equals(n)) {
dog.get(i).changeBreed(br);
return;
}
}
System.out.println(n+" named dog is
not present in the kennel!!!!");
}
//Mutator for set kennel type
public void setKennelType(String type) {
kennelType=type;
}
//Accessor
public String getKennelType() {
return kennelType;
}
//Method to print dogs details
public String toString() {
String result="";
for(int i=0;i<dog.size();i++)
{
result+="Dog
"+(i+1)+": ";
result+="Name: " + dog.get(i).getName() + " Breed: " +
dog.get(i).getBreed() + " Age: " + dog.get(i).getAge()+" ";
}
return result;
}
}
--------------------------------
DogMain.java
public class DogMain {
// Driver method main
public static void main(String
args[]) {
//Instantiate
the class DogKennel
DogKennel dg=new
DogKennel();
// Using
Parameterized Constructor create a dog
Dog dog1 =
new Dog("Browney", "German Shepered", 5);
//add a dog
into kennel
dg.addDog(dog1);
Dog dog2 =
new Dog("Tippu", "Pomerian", 5);
//add a dog
into kennel
dg.addDog(dog2);
//set kennel type
dg.setKennelType("Tall Metal 1");
//Get
kennel type
System.out.println("Type of kennel is "+dg.getKennelType());
// Print
the current status of dog's kennel
System.out.println("Number of dogs in the kennel =
"+dg.currentNumDogs());
//Display
dog details
System.out.print(dg.toString());
//Average
age of all dogs in the kennel
System.out.println("Average age = "+dg.averageAge());
//Change
breed of dog2
dg.changeBreed("Tippu","Bulldog");
//Display dog details
again for check
System.out.print(" Display After breed change:
----------------------------- "+dg.toString());
//Display dog details
again for check
dg.adoptDog("Browney");
System.out.print(" Display After remove:
----------------------------- "+dg.toString());
}
}
----------------------------------
Output
Type of kennel is Tall Metal 1
Number of dogs in the kennel = 2
Dog 1:
Name: Browney Breed: German Shepered Age: 5
Dog 2:
Name: Tippu Breed: Pomerian Age: 5
Average age = 5.0
Display After breed change:
-----------------------------
Dog 1:
Name: Browney Breed: German Shepered Age: 5
Dog 2:
Name: Tippu Breed: Bulldog Age: 5
Display After remove:
-----------------------------
Dog 1:
Name: Tippu Breed: Bulldog Age: 5
Using your Dog class from earlier this week, complete the following: Create a new class called...
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. 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...
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...
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...
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();...
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)....
Create a class called Horse. It has an instance var which is a string called "breed" create a constructor to set and an a accessor method to return back the breed. Then in a main driver class, create an ArrayList with 5 Horses. Create the following static method which returns back the total horses in the ArrayList of a certain breed. public static int getBreed(List<Horse> h, String breed);
solve it in c++ 10 note: please do not give me same answer like this 1. Define a Pet class that stores the pet's name, age, and weight. Add appropriate constructors, accessor functions, and mutator functions. Also define a function named getLifespan that returns a string with the value "unknown lifespan." Next, define a Dog class that is derived from Pet. The Dog class should have a private member variable named breed that stores the breed of the dog. Add...
java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date { private String month; private String day; private String year; public Date(String month, String day, String year) { this.month = month; this.day = day; this.year = year; } public String getMonth() { return month; } public String getDay() { return day; } public String...
JAVA QUESTION 16 Write and submit the java source for the following class specification: The name of the class is Question_16 Class Question_16 has 3 instance variables: name of type String, age of type int and height of type double. Class Question_16 has the following methods: print - outputs the data values stored in the instance variables with the appropriate label setName - method to set the name setAge - method to set the age setHeight - method to set...