In: Computer Science
Writing Classes I
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.
Code:
//Class Dog
class Dog{
private int age; //An integer age.
private String name,bark; //A string name and a string bark
representing the vocalization the dog makes when they
‘speak’.
private boolean hairlen; //A boolean representing hair length; true
indicates short hair.
private float weight; // A float weight representing the dog’s
weight (in pounds).
public static enum tail{ LONG, SHORT, NONE };
private tail t; //An enumeration representing the type of tail
(LONG, SHORT, NONE).
//A default constructor.
public Dog()
{
age=0;
name="Wolfy";
bark="owww";
hairlen=false;
weight=0;
t = tail.NONE;
}
//A constructor that takes a name as argument.
public Dog(int age,String name,String bark,float weight,boolean
hairlen,tail t)
{
if(validName(name))
this.name=name;
else //If the given name contains non-alphabetic characters,
initialize to Wolfy.
this.name="Wolfy";
this.age=age;
this.bark=bark;
this.hairlen=hairlen;
this.weight=weight;
this.t = t;
}
//A method private boolean validName(String) that returns true /
false whether the given name contains non-alphabetic
characters.
private boolean validName(String name)
{
for(int i=0;i<name.length();i++)
{
//System.out.println(Character.isLetter(name.charAt(i)));
if(!Character.isLetter(name.charAt(i)))
return false;
}
return true;
}
//Method that computes and returns the age of the dog in “human
years.”
public int humanAge()
{
return 7*age;
}
//Method that returns the dog’s bark.
public String speak()
{
return bark;
}
//toString to display dog info
public String toString()
{
return "Name: "+name+", age: "+age+", bark: "+speak()+", weight:
"+weight;
}
}
// Kennel class
public class Kennel {
//Main() method
public static void main(String[] args) {
//Creating multiple Dog class objects
Dog d1,d2,d3,d4,d5;
d1 = new Dog(10,"Tommy","Wuff",10,true,Dog.tail.NONE);
d2 = new Dog(14,"Ralph","Purr",40,false,Dog.tail.LONG);
d3 = new Dog(12,"Buck","Oww",14,false,Dog.tail.SHORT);
d4 = new Dog(5,"89kl","Huff",16,true,Dog.tail.NONE);
d5 = new Dog(12,"John","Puff",23,false,Dog.tail.SHORT);
System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
System.out.println(d4);
System.out.println(d5);
}
}
Sample Run: