Constructors - public - method responsible for initializing instance variables
Methods - public - does computations that access the class's instance variables. Two types include accessor methods and modification methods
Which kind of method often has a void return type? Why?
Accessor methods have a return type because they are used to get information on an object. Modification methods have a void return type because they make some change to the variables but do not return information.
Write a Throttle constructor with no arguments. Should set the top position to 1 and the current position to off.
public Throttle()
{
top = 1;
position = 0;
}
Write a Throttle constructor with two arguments: the total number of positions for the throttle and its initial position.
public Throttle( int size, int initial)
{
if ( size <= 0 )
throw new IllegalArgumentException("size<=0: " +size);
if ( initial <= 0 )
throw new IllegalArgumentException("initial<=0: " +initial);
if ( initial > size )
throw new IllegalArgumentException("initial too large: " +initial);
top = size;
position = initial;
}
What is the difference between a modification method and an accessor method?
An accessor method does not alter an object but provides information on it. A modification method changes an objects instance variables.
Add a new throttle method that will return true when the current flow is more than half of the maximum flow. Use the getFlow method.
public boolean isAboveHalf()
{
return(getFlow() > 0.5);
}
What kind of constructor does Java usually provide automatically?
Java automatically provides a no argument constructor that sets each instance variablet o its initialization value.