Answers to T#1 Sample


A1	2
	641
	
A2	6
	3

A3	NT
	false
	
A4	Syntax error in i3 = i2 / r2; because you cannot assign
	a double to an int w/o casting.  

A5	0
	-22

A6	4
	21

A7	false
	case 3

A8	cdefg
	false
	
A9	1002030
	3410020

A10	false
	false
Question B.1) Something like this:

	Department dept = new Department("R&D", 2000000);
	Employee e1 = new Employee("John", 3);
	Employee e2 = new Employee("Debbie", 2);
	dept.assign(e1);
	dept.assign(e2);
	int count = dept.getHeadCount();
	if (count > 10)
	{  int delta = (int) (0.05 * dept.getBudget()); // or 100000
   	   dept.changeBudget(delta);
	} else
	{  int delta = (int) (0.02 * dept.getBudget()); // or 40000
	   dept.changeBudget(-delta);
	}
Question B.2) Input and validation can be done like this:

	IO.print("Enter a string: ");
	String entry = IO.readLine();
	SE.require(entry.length() < WIDTH, "String too long!"); 
or like this:

	if (entry.length() >= WIDTH)
	{  IO.println("String too long!");
	} else
	{
	  ...
	}
Output generation can be done like this:

	IO.fillChar = '+';
	IO.println(entry, "C"+WIDTH);
or like this:

	final String PLUS = IO.repeat(WIDTH, '+');
	int left = (WIDTH - entry.length()) / 2;
	int right = WIDTH - entry.length() - left;
	IO.print(PLUS.substring(0, left));
	IO.print(entry);
	IO.println(PLUS.substring(0, right));
or via loops.

Question B.3) Input and validation can be done like this:

	final int MAX = 6;
	final int INITIAL_BALANCE = 10;
	final int WIN = 2;
	Random rng = new Random();
	int balance = INITIAL_BALANCE;

	IO.println("Enter a guess...");
	int guess = IO.readInt();

	for (; balance > 0 && guess >= 1 && guess <= MAX;)
	{  int rand = 1 + rng.nextInt(MAX);
	   if (guess == rand)
	   {  balance = balance + WIN;
	   } else
	   {  balance = balance - WIN;
	   }
	   int guess = IO.readInt();
	}

	IO.println("Final balance = " + balance); 
The readInt statement could optionally be absorbed in the first and last parts of the for; viz.

	for (int guess = IO.readInt();
	     balance > 0 && guess >= 1 && guess <= MAX;
	     guess = IO.readInt())
        {  int rand = 1 + rng.nextInt(MAX - 1);
           if (guess == rand)
           {  balance = balance + WIN;
           } else
           {  balance = balance - WIN;
           }
        }