Review of some OOP and Java concepts.

 

What is an abstract data type?

Why define abstract data types?

What are the advantages of information hiding?

What are the advantages of encapsulation?

What is the difference between a class and an object?

What is the difference between a class and a (Java) package?
We probably won't do much with packages because they require a directory hierarchy.

Here is "almost stub" version of Throttle class

public class Throttle
{
	private int top;
	private int position;
	public Throttle(int size) { }
	public double getFlow( ) { }
	public boolean isOn( ) { }
	public void shift(int amount) { }
	public void shutOff( ) { }
}

 

Why did I say "almost" stub?
What additional code would be required to make this a "stub of a class"?
What is the constructor?
What are the methods?
What are the instance variables?
Why are some components public and others private?
What purpose would a private method serve?
What file would this class definition be stored in?

 

Here is a driver to test the class (also see ThrottleDemonstration on page 60):

import Throttle;

public class ThrottleDriver
{
	public static void main(String args[])
	{
		final int HIGH_POSITION = 10;	// what does "final" mean?
		Throttle myControl;		// what is value of myControl?
		myControl = new Throttle(HIGH_POSITION);
		Throttle yourControl = myControl;	// what happens here?
		myControl.shift(2);
		if (myControl == yourControl) 
			System.out.println("same");
		else
			System.out.println("different");
		// What is output of above 'if' statement?
		yourControl = new Throttle(HIGH_POSITION); // what happens here?
		yourControl.shift(2);
		if (myControl == yourControl) 
			System.out.println("same");
		else
			System.out.println("different");
		// What is output of above 'if' statement?
		if (myControl.getFlow() == yourControl.getFlow()) 
			System.out.println("same");
		else
			System.out.println("different");
		// What is output of above 'if' statement? Any peril here?
		int upward = 5;
		testAndShift(myControl, upward); 
		System.out.println(myControl.getFlow());
		System.out.println(upward);
		// What are the outputs of these two?  Why?
	}
	
	static int testAndShift(Throttle control, int val)
	{
		int result = 0;
		if (control.isOn())
		{
			control.shift(val);
			result = val;
		}
		val = result;	// unnecesary, used for arg. demonstration.
		return result;
	}
}

 

 

Here is complete Throttle source code, also found on textbook page 58.

public class Throttle
{
	private int top;
	private int position;
	public Throttle(int size)
	{
		if (size <= 0)
			throw new IllegalArgumentException("Size<=0"+size);
		top = size;
		// position automatically initialized to 0
	}
	public double getFlow( )
	{
		return (double) position / (double) top; 
		// what does "(double)" mean and why used?
	}
	public boolean isOn( )
	{
		return (getFlow( ) > 0); // Some prefer "this.getFlow( )"
	}
	
	public void shift(int amount)
	{
		if (amount > top - position)
		// adding amount would put position over the top
			position = top;
		else if (position + amount < 0)
			// adding amount would put position below zero.
			position = 0;
		else
			position += amount;
	}
	public void shutOff( )
	{
		position = 0;
	}
}


[ Lectures | CSC 132 | Peter Sanderson | Computer Science | SMSU ]


Last reviewed: 30 August 2000

Peter Sanderson ( PeteSanderson@smsu.edu )