This lab’s purpose is to provide students with experience in file I/O. It is recommended that students use command line tools and editors for this lab (though it is not strictly speaking required). This lab will require students to build on their previous lab experience, in which a version of the cowsay utility was created.
Students will update the driver program class (Cowsay) and also add one new class – FileCow. The FileCow class should extend the Cow class. As before, HeiferGenerator.java is provided for you – but updated to handle the new FileCow class. (Please refer to specification for previous lab for a refresher.) Students may implement protected attributes and methods if they chose to do so. This is not required – it is purely optional. No public attributes / methods should be added to the specification!
Cowsay Class (Program Driver)
Your program must accept command line arguments as follows:
java cowsay -l Lists the available cows
java cowsay MESSAGE Prints out the MESSAGE using the default Cow java cowsay -n COW MESSAGE Prints out the MESSAGE using the specified built-in Cow java cowsay -f COW MESSAGE Prints out the MESSAGE using the specified file Cow
In addition, this version of the utility handles a special set of Cow-derived FileCow objects. The HeiferGenerator will automatically create FileCow objects (using the FileCow constructor) from files in the “cows” directory.
>java Cowsay -l
Regular cows available: heifer kitteh dragon ice-dragon File cows available: moose turkey turtle tux
The following classes, developed previously, are required for this lab to function.
|public Cow(String name)||// Constructor|
|public String getName()||// Returns name of this cow object|
|public String getImage()||// Return image for this cow object|
|public void setImage(String image)
Dragon (extends Cow)
|// Sets the image for this cow object to image|
public Dragon(String name, String image) // Constructor public boolean canBreatheFire() // Defaults to true
IceDragon (extends Dragon)
public IceDragon(String name, String image) // Constructor public boolean canBreatheFire() // Returns false
The FileCow class must be derived from the Cow class. In addition, FileCow must add the following behavior:
public FileCow(String name, String filename)
Constructor; creates a new FileCow object with the given name and an image loaded from filename.
If the file cannot be loaded, it should throw a new RuntimeException with the message “MOOOOO!!!!!!” This should be the only public constructor for the FileCow class!
public void setImage()
Should immediately throw a new RuntimeException with the message “Cannot reset FileCow Image”.
NOTE: Your output must match the example output *exactly*. If it does not, you will not receive full credit for your submission!
Files: Cowsay.java, Cow.java, Dragon.java, IceDragon.java, FileCow.java, HeiferGenerator.java Method: Submit on Canvas
>javac Cowsay.java >java Cowsay Hello World!
>java Cowsay -n kitteh Moew-Moew! Moew-Moew!
(“`-‘ ‘-/”) .___..–‘ ‘ “`-._
` *_ * ) `-. ( ) .`-.__. `)
(_Y_.) ‘ ._ ) `._` ; “ -. .-‘
_.. `–‘_..-_/ /–‘ _ .’ ,4
( i l ),-” ( l i),’ ( ( ! .-‘
>java Cowsay -l
Regular cows available: heifer kitteh dragon ice-dragon
File cows available: moose turkey turtle tux
>java Cowsay -n ninja Hello world!
Could not find ninja cow!
>java Cowsay -f tux Do you have any herring?
Do you have any herring? .–.
// \ \
(| | )
>java Cowsay -f alien Earth is ours!
Could not find alien cow!
>java Cowsay -f kitteh MEOW!!!
Could not find kitteh cow!
>java Cowsay -n tux How about tuna? Could not find tux cow!