Question

In: Computer Science

Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....


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.

Solutions

Expert Solution

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:


Related Solutions

java Write our Test Driver program that tests our Classes and Class Relationship How we calculate...
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate Net Pay after calculating taxes and deductions taxes: regNetPay = regPay - (regPay * STATE_TAX) - (regPay * FED_TAX) + (dependents * .03 * regPay ) overtimeNetPay = overtimePay - (overtimePay * STATE_TAX) - (overtimePay * FED_TAX) + (dependents * .02 * overtimePay ) Example printPayStub() output: Employee: Ochoa Employee ID: 1234 Hourly Pay: $25.00 Shift: Days Dependents: 2 Hours Worked: 50 RegGrossPay: $1,000.00...
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write a java program with the following classes: Class Player Method Explanation: play : will use...
Write a java program with the following classes: Class Player Method Explanation: play : will use a loop to generate a series of random numbers and add them to a total, which will be assigned to the variable score. decideRank: will set the instance variable rank to “Level 1”, “Level 2”, “Level 3”, “Level 4” based on the value of score, and return that string. getScore : will return score. toString: will return a string of name, score and rank....
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears...
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears and fish. The ecosystem consists of a river, which is modeled as a relatively large array. Each cell of the array should contain an Animal object, which can be a Bear object, a Fish object, or null. In each time step, based on a random process, each animal either attempts to move into an adjacent array cell or stay where it is. If two...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) check that the only characters in a line are ones and zeros. If there is no such command-line argument or no such file, or if any of the checks fail, your...
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
Write these java classes: 1) DynArray.java: a class that models some of the functionality of the...
Write these java classes: 1) DynArray.java: a class that models some of the functionality of the Java ArrayList. This class is not complete and must be modified as such: Write the method body for the default constructor Write the method body for the methods: arraySize(), elements(), grow(), shrink(). The incomplete code is provided here: public class DynArray { private double[] array; private int size; private int nextIndex;    public int arraySize() { }    public int elements() { } public...
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n...
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n of type int. Constructor initializes instance variable n by using input parameter n. public NumberUtility(int n) Class also has the following methods:   public int getN()                              // Returns instance variable n public boolean isOdd()                  // Returns true if number n is odd and returns false otherwise. public boolean isEven()               // Returns true if number n is even and returns false if it is odd.      // Implement method by...
This is for Java programming. Please use ( else if,) when writing the program) Write a...
This is for Java programming. Please use ( else if,) when writing the program) Write a java program to calculate the circumference for the triangle and the square shapes: The circumference for: Triangle = Side1 + Side2 +Sid3 Square = 4 X Side When the program executes, the user is to be presented with 2 choices. Choice 1 to calculate the circumference for the triangle Choice 2 to calculate the circumference for the square Based on the user's selection the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT