HW1: Solutions

1.2 c

ADT BowlingBall is
  Data
    The radius and weight of the ball in lb./ft.3. The numbers
     are floating point values > 0
  Operations
    Constructor
      Initial Values: The radius and density of the bowling ball.
      Process:   Use the initial values to specify the ADT data.

  Radius:
      Input: None
      Preconditions: None
      Process: Retrieve the radius of the ball.
      Output: return the radius.
      Postconditions: None

  Weight:
      Input: None
      Preconditions: None
      Process: Compute the weight by multiplying the balls volume
               by the balls density.               
      Output: return the weight.
      Postconditions: None.
end ADT BowlingBall.

(2.2)

  1. 5
  2. 14
  3. 55
  4. 127

(2.6)

%
	Input: Two integers u and v.

        Preconditions: Integer  since operation u%0
        is undefined.

        Process: Compute the remainder from the long division.

        Output: Return the integer r.

        Postconditions None.

        <Operator "!=" is not done>

(2.7)

(a) ADT Boolean is
        Data
          Logical values False and True.
        Operations
          Assume P and Q are boolean variables.

        Binary Operators.
		&&      P&& Q  Return the logical value P AND Q
		||	P || Q	Return the logical value P OR Q
		
	Unary Operator
		!	!P	Change the logical value of P.
end ADT Boolean

class Boolean
{
	private:
		int boolvalue;
	public: 
		Boolean(void);
		Boolean operator && (Boolean p, Boolean q);
		Boolean operator || (Boolean p, Boolean q);
		Boolean operator != (Boolean p);
};	
For implementation of the operations
	return p.boolvalue && q.boolvalue;	//operation &&
	return p.boolvalue || q.boolvalue;	//operation ||
	return !p.boolvalue;	//operation !=
	

(2.10) The operation % is not defined for real numbers.

(2.13)

  1. No! The ADT assumes that real numbers have an infinite binary expansion with no smallest positive value.
  2. With computer storage, a number is stored in a fixed bit structure with a finite binary expansion. The storage allows for only a finite number of real numbers, one of which is the smallest.

(2.15)

  1. {January, February, March, April, September, October, November December}
  2. Month 4 is September; October has position 5.
  3. Alphabetical order is {April, December, February, January, March, November, October, September}; No month occupies the same position in both lists.

(2.16)

SUCC
	Input: Enumeration type variable v.
	Preconditions: v cannot be the last item in the list.
	Process: Identify the next item in the list after v.
	Output: Return the next item.
	Postconditions: None.

<PRED is not done >