11. Quiz B
-
Clothesinterface hasint fashion()andint warmth(). -
HeadCoveringis a class that implements theClothesinterface. Its constructor sets fashion and warmth, in that order. -
WoolClothesis a class that implements theClothesinterface. It takes in aClothesobject in its constructor. It adds 5 to the warmth provided by the Clothes because they are made of wool. -
Personis an abstract class with aString namefield and aString getName()function, as well as abstract methodsint getWarmth()andint getFashion(). Constructor sets the name. -
PlainPersonis a subclass ofPersonwith a warmth of 90 and a fashion of 50, always. -
BigFashionPersonis a subclass ofPersonthat has anArrayList<Clothes>.* Constructor takes in a name and an arraylist of clothes. * Their warmth is the sum of the warmth of their clothes. * Their fashion is the *minimum* of the fashion of their clothes.
Example
- Full TestClothes code.
- Add the repl.it testing code starter to get the test going on Repl.it.
// copy and paste? define public static void aassert(bool b)...true=ok, false=fail
public static void main(String[] args) {
Clothes hat = new HeadCovering(20, 105);
Clothes underArmor = new WoolClothes (hat);
Clothes denim = new HeadCovering(95,40);
ArrayList<Clothes> w = new ArrayList<>();
w.add(hat);
w.add(underArmor);
w.add(denim);
Person p1 = new PlainPerson("Jane");
aassert(50 == p1.getFashion());
aassert(90 == p1.getWarmth());
aassert("Jane".equals(p1.getName()));
Person p2 = new BigFashionPerson("Sanjana", w);
aassert("Sanjana".equals(p2.getName()));
aassert(20+25+95 == p2.getWarmth());
aassert(40 == p2.getFashion());
}
Pedantic
An object that implements the Clothes interface has an integer value
for its fashion level and its warmth rating. The methods in the
Clothes interface are accessor methods that return those values.