Please briefly describe best practices as it relates to method names.
Describe the criteria you would use when deciding whether or not to use composition in an application. Include a brief explanation of the advantages of using one.

Composition can be used when we need to change the implementation dynamically at run time. The implementation detail about the class is abstracted always from the users. So, if the implementation needs to change, the users of the class will be immune from these changes.

If we don’t want to take burden of maintaining previous behavior, we may like to go for inheritance. This is the criteria whether or not to use composition in an application

Example:

Option 1 (Inheritance):

public class SalesList : List<Sales>

{

//methods that add extra behavior List<Sales>

}

Option 2 (Composition):

public class SalesList

{

private List<Sales> _list;

//methods that add extra behavior to List<Sales>

}

Advantage of option2: The implementation detail about the storage of the sales data is abstracted from the consumer. So, if the implementation needs to change (say a dictionary, for example), the consumer of the class will be immune from these changes.

Write the code for an interface that models the behavior of a DVD player. Include at least three methods.

public interface DVDPlayer {

public static final int STOP = -1;

public static final int PLAY = 1;

public static final int PAUSE = 0;

public void stop();

public void play();

public void pause();

}

Consider the following information about a Vehicle/Car inheritance hierarchy and answer each of the following questions:
i. Which would be the child class?

Vehicle. As car it a vehicle

ii. Which class would use the protected access modifier for its data members?

Vehicle. As the members will be inherited to class car.

iii. Assume a public Move() method was included in the Vehicle class but not in the Car class. Also assume that a Vehicle object, aVehicle, and a Car object, aCar, had been instantiated in a Main method. Write a statement to call the Move() method for each of the objects. If the object cannot call the method, explain why.

public static void main(String[] args) {

Vehicle aVehicle = new Vehicle();

Car aCar = new Car();

aVehicle. Move() // a statement to call the Move() method for vehicle class

aCar. Move() // a statement to call the Move() method for car class

}

As vehicle is the super class, so the public Move() method will be inherited to car class. We can call move method from any of the objects.

iv. Assume a protected (string) data member, name, and a method named setName was coded in the Car class, but not in the Vehicle class. Also assume that a Vehicle object, aVehicle, and a Car object, aCar, had been instantiated in a Main method. Write a statement to set the name to "Custom" for each of the objects. If the object cannot use the method, explain why.

public static void main(String[] args) {

Vehicle aVehicle = new Vehicle();

Car aCar = new Car();

aVehicle.setName (“Custom”);// Error

aCar.setName (“Custom”); // a statement to set the name to "Custom" for car class

}

The statement

aVehicle.setName (“Custom”);

Will produce error as car is subclass and only it knows about a protected (string) data member, name, and a method named setName.

Based on the previous question, instantiate two objects. One that has no parameters and one the passes all the parameters to the class.

public static void main(String[] args) {

Vehicle aVehicle = new Vehicle();

Car aCar = new Car(); //no parameters

Car bCar = new Car(“Custom”); //the passes all the parameters

}

Code a partial and simplified Student Assignment class. The purpose of the class will be to provide the assignment description, maximum score, and actual score. Code the “get” and “set” methods just for the assignment description and maximum score. Also code the calcScore method that returns maximum score minus actual score. Don’t worry about all the other things that should be in a Student Assignment class

//a partial and simplified Student Assignment class

public class StudentAssignment {

//assignment description, maximum score, and actual score

String description;

int maxScore, actualScore;

//the “get” and “set” methods just for the assignment description and maximum score

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

public int getMaxScore() {

return maxScore;

}

public void setMaxScore(int maxScore) {

this.maxScore = maxScore;

}

//the calcScore method that returns maximum score minus actual score

public int calcScore()

{

return maxScore- actualScore;

}

}