StaticTest2.java Answers

Click here to go back to the questions
// ANSWERS:
//
// (1) Add code to the StaticTest2 class to construct two
// new Character objects, one to represent "Barney Rubble"
// and the other to represent "Wilma Flintstone".  Make sure
// that the values of the properties are set appropriately.
// 

CartoonCharacter w = new CartoonCharacter("Wilma Flintstone","yellow", 63);
CartoonCharacter b = new CartoonCharacter("Barney Rubble",   "red",    88);

// 
// (2) Add some code to the main method to call Fred
// Flintstone's displayMe method.
 
// HINT: Calling a non-static method.
// 

f.displayMe();

// 
// (3) Add some code to the main method to print out Barney
// Rubble's favourite colour WITHOUT using Barney Rubble's
// displayMe method.
 
// HINT: Accessing a non-static property.
// 

System.out.println(b.favouriteColour);

// 
// (4) Why is it better to have a Character class for a
// general character rather than a class for each character?
//
// HINT: Imagine that you had 100 characters to consider.
// 

// ANSWER: It means that the code size will be about 100
// times smaller if you have only one class for all the
// characters.

// 
// (5) Under what circumstances would it be better to have a
// separate class for each character, rather than a separate
// object for each character?
// 

// If each character had different methods, it would be wise
// to have a separate class for each character.

//