Question: Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: An integer age. A string
Write a Java program containing two classes: Dog and a driver class Kennel.
A dog consists of the following information:
An integer age.
A string name. If the given name contains non-alphabetic characters, initialize to Wolfy.
A string bark representing the vocalization the dog makes when they speak.
A boolean representing hair length; true indicates short hair.
A float weight representing the dogs weight (in pounds).
An enumeration representing the type of tail (LONG, SHORT, NONE).
A dog consists of the following methods.
A default constructor.
A constructor that takes a name as argument.
A method private boolean validName(String) that returns true / false whether the given name contains non-alphabetic characters.
humanAge that computes and returns the age of the dog in human years.
speak returns the dogs bark.
Each constructor should initialize all attributes to reasonable initial values.
The main method in the Kennel class should create several dogs with each constructor and output their instance data using toString.
This is what I have so far
public class Dog { public enum tail { LONG, SHORT, NONE; } private int age; private String name; private String bark; private boolean length; private float weight; private tail tail; public Dog() { } public Dog(String name) { if (isValidName(name)) this.name = name; else this.name = "Wolfy"; } private boolean isValidName(String name) { boolean flag = true; for (int i = 0; i < name.length(); ++i) { if (!Character.isAlphabetic(name.charAt(i))) { flag = false; break; } } return flag; } private int humanAge() { return age; } private String speak() { return bark; } public String toString() { return "Dog [age=" + age + ", name=" + name + ", bark=" + bark + ", length=" + length + ", weight=" + weight + ", tail=" + tail + "]"; } }
......................................................................................
public class Kennel { public static void main (String[] args) { Dog d1 = new Dog("Stephen"); Dog d2 = new Dog("Kevin35"); Dog d3 = new Dog("ruff"); System.out.println(d1.toString()); System.out.println(d2.toString()); System.out.println(d3.toString()); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
