QuestionQuestion

1. This problem allows you to practice the inheritance concepts.
a. Write an abstract class Animal that represents the concept of a generic animal. Your class must meet the following requirements.
•         The class must have the private field String name, which should be used to hold the name (not type) of the animal (e.g. “Fido”).
•         The class must have a constructor Animal(String aName) that initializes the value of name.
•         The class must have a public abstract method void makeNoise() that is declared without implementation (since we don’t know what kind of noise a generic animal makes).
•         The class must have a public “getter” function String getName() that returns name.
•         The class must provide an override to the method public String toString(), which returns a string that reads “[name], who is a generic Animal” where [name] should be replaced by name.
b. Write two classes Dog and Cat, which should both be declared to be subclasses of Animal and satisfy the following requirements.
•         Both classes must have constructors that call their superclass constructor in order to set the name of the animal that is being create (whether a dog or a cat).
•         Both classes must provide implementations of the makeNoise() method declared in Animal. The implementation for the Dog class should print out “Woof Woof!” and the implementation for the Cat class should print out “Meow!”
•         The Dog class should introduce a new method public void playCatch() that prints out the statement “Oh Boy, I love playing fetch!”
•         Both classes should override the public String toString() method so that they return the strings “[name], who is dog” and “[name], who is a cat” respectively.
c. Write a class Chihuahua that is a subclass of Dog. This class should satisfy the following requirement.
•         The class should have a constructor that calls its superclass constructor (which in turn calls the superclass’ superclass constructor) in order to set the value of name.
•         The class should override the method makeNoise() so that it prints out “Squeak! Squeak!”
d. Use your classes above to write a sample program to help evaluate the output of the following instructions. Please keep in mind that some of these statements will lead to compile and/or runtime errors.
      i.          Dog A = new Dog(“Fido”); System.out.println(A);
      ii.   Animal B = new Cat(“Garfield”); B.makeNoise();
      iii. Animal C = new Animal(“Tweetie”); System.out.println(C);
      iv.   Animal D = new Animal(“Leo”) {
                  @Override public void makeNoise() {
                        System.out.println(“Roar!”);
                  }
            };    D.makeNoise();
      v.    Animal E = new Dog(“Rover”); E.playFetch();
      vi.   Animal F = new Dog(“Lady”); ((Dog) F).playFetch();
      vii. Animal G = new Cat(“Heathcliff”); ((Dog) G).playFetch();
      viii. Dog H = new Chihuahua(“Tiny”); H.makeNoise();
      ix.   Animal I = new Chihuaua(“Tinier”); ((Dog) I).makeNoise();
e. Based on this exercise, given a reference type that does not match the constructed object type (i.e., the reference class is a superclass of the class to which the object belongs), describe in your own words how can one determine if a class method call is legal, and assuming that it is, which implementation will be used?


2. Consider the following interface:

public interface WordWrapper
{
   void writeString(String str);
}
Write a class FileWordWrapper that implements WordWrapper with the following properties:

1.       At the very least, the class should contain fields to hold a filename (as a string) and a line length (as an int) for word wrapping purposes. You will probably also need a FileReader object somewhere.
2.       The class should contain appropriate constructors that set the fields described above.
3.       The writeString method should take its input string str and write that string to the file specified by the object’s filename field using word-wrapping assuming a maximum line length given by the line length field.

As an example, suppose filename = “bla.txt” and line_length = 10.

We may instantiate a FileWordWrapper object as follows:

WordWrapper wrapper = new FileWordWrapper(“bla.txt”,16);

Now suppose we have a string defined as follows:

String my_str = “Hello world, this is an assignment that will allow me to take a string and write it to a file using word-wrapping.”

Then, if we call wrapper.writeString(my_str), then the following should be written to bla.txt:

Hello world,
this is an
assignment that
will allow me to
take a string
and write it to
a file using
word-wrapping.

Notice how no line contains more than 16 characters and that no word is split into two lines, i.e. any word that will take the line past 16 characters will be moved to a new line. For simplicity, assume that hyphenated words such as “word-wrapping” count as a single word that should remain together on a single line. A word that ends with a period (or other punctuation mark such as comma, semi-colon, etc.) should not be split from that period.

In the case that any one word (or word plus punctuation mark) exceeds the maximum line length (16 in this example), have your method throw a custom unchecked exception object of type StringTooLongException.

4.       Write some sort of a tester class that tests your word-wrapper class.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

public class SampleProgram {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
       // TODO code application logic here
       Dog A = new Dog("Fido");
       System.out.println(A);
       Animal B = new Cat("Garfield");
       B.makeNoise();
       // abstact class cannot be instantiated
      // Animal C = new Animal("Tweetie");
      // System.out.println(C);
       Animal D = new Animal("Leo") {
                  @Override public void makeNoise() {
                        System.out.println("Roar!");
                  }
            };
       D.makeNoise();
       Animal E = new Dog("Rover");
       // invalid method call
      // E.playFetch();
       Animal F = new Dog("Lady");
       // invalid method call
      // ((Dog) F).playFetch();
       Animal G = new Cat("Heathcliff");
       // invalid method call
      // ((Dog) G).playFetch();
       Dog H = new Chihuahua("Tiny");
       H.makeNoise();
       // invlid class name
       //Animal I = new Chihuaua("Tinier");
       //((Dog) I).makeNoise();

    }   
}...
$10.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats