Write a Java program containing two classes: Dog and a driver class Kennel. A do
ID: 3593218 • Letter: W
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 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 dog’s 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 dog’s 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.
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Dog
{
private String name;
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
private String bark ;
public String getbark() {
return bark ;
}
public void setbark(String newbark) {
bark = newbark;
}
private boolean hair;
public boolean gethair() {
return hair;
}
public void sethair(boolean newhair) {
hair= newhair;
}
private float weight;
public float getweight() {
return weight;
}
public void setweight(float newweight) {
weight= weight;
}
public enum tail{ LONG, SHORT, NONE };
public Dog()
{
}
public Dog(String name)
{
setName(name);
}
public boolean validName(String name)
{
if (name.matches("^[a-zA-Z]+$"))
return true;
else
return false;
}
public String toString()
{
return("Dog name "+name);
}
}
class Kennel
{
public static void main(String args[])
{
Dog obj=new Dog();
if(obj.validName("DoberMan"))
{
Dog obj1=new Dog("DoberMan*");
System.out.println(obj1.toString());
}
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.