L02a

Due by the end of this meeting

Starter Files:


SUBMIT   /   Submission Summary

Review of CSCI 1228

Activity 1
Consider the Animal class (provided). Add a method isValidName(String name) that returns false if the given name is null or empty.

Revise the constructor and (name) setter so that it calls that method on each of its arguments and throws an IllegalArgumentException if the name is not valid.

Test your revisions on the following code:

Animal myPet; try { myPet = new Animal(Animal.DOG, ""); System.out.println("FAIL -- empty name"); } catch (IllegalArgumentException iae) { System.out.println("OK -- empty name"); } try { myPet = new Animal(Animal.DOG, null); System.out.println("FAIL -- null name"); } catch (IllegalArgumentException iae) { System.out.println("OK -- null name"); } try { myPet = new Animal("", "Okie Dokie"); System.out.println("FAIL -- empty species"); } catch (IllegalArgumentException iae) { System.out.println("OK -- empty species"); } try { myPet = new Animal(null, "Weird"); System.out.println("FAIL -- null species"); } catch (IllegalArgumentException iae) { System.out.println("OK -- null species"); } try { myPet = new Animal("penguin", "Happy"); System.out.println("OK -- SO FAR!"); myPet.setName(""); System.out.println("FAIL -- empty new name"); } catch (IllegalArgumentException iae) { System.out.println("OK -- empty new name"); } try { myPet = new Animal("penguin", "Happy"); System.out.println("OK -- SO FAR!"); myPet.setName(null); System.out.println("FAIL -- null new name"); } catch (IllegalArgumentException iae) { System.out.println("OK -- null new name"); }

You should, of course, see no FAIL messages at all.

Activity 2
Create three data type classes, Cat, Dog and GoldFish, that extend Animal (provided). In each case the constructor takes the animal's name and fills in the appropriate species.

Test your classes using the following code:

Cat kitty = new Cat("Bill"); Dog puppy = new Dog("Snoopy"); Goldfish iWantedADog = new GoldFish("Lassie"); System.out.println(kitty.getName() + " is a " + kitty.getSpecies()); System.out.println(puppy.getName() + " is a " + puppy.getSpecies()); System.out.println(iWantedADog.getName() + " is a " + iWantedADog.getSpecies());

You should see the following output:

Bill is a cat Snoopy is a dog Lassie is a goldfish
Submit your data type classes by the end of this recitation. (You do not need to submit your testing code; we know what your code is supposed to do!)

Your grade will be based on the following rubric:

Submit this/these files:


SUBMIT   /   Submission Summary