L02b

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 the Payable interface as required for A02. Create two classes, Worker and Manager that implement the interface as follows: (You don't need to create constructors, they're not meant to represent real data types; only to show that you can make classes that implement an interface!)

Test your classes using this code:

Payable guy = new Worker(); Payable boss = new Manager(); if (!guy.getName().equals("Enya")) { System.out.println("FAIL guy's name"); } if (guy.payForWeek() != 500.0) { System.out.println("FAIL guy's weekly pay"); } if (guy.payForYear() != 26000.0) { System.out.println("FAIL guy's weekly pay"); } if (!boss.getName().equals("Eustace")) { System.out.println("FAIL boss's name"); } if (boss.payForWeek() != 1000.0) { System.out.println("FAIL boss's weekly pay"); } if (boss.payForYear() != 52000.0) { System.out.println("FAIL boss's weekly pay"); }
You should, of course, not see any FAIL messages when you run it.
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