CSE 109 Test 1  Friday 26 March 2010
!!!!!!!!!!!!!!!!!!! SUGGESTED ANSWERS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1. Assume the program below compiles. State the output of the program when run.
#include <iostream.h>
   void one(int a, int &b, int&c){
      a=b-c;
      b=a+c;
      c+=4;
   }
   void two(int *&a, int *b){
     int *temp;
     temp=a;
     a=b;
     b=temp;
     a[0]=88;
     b[0]=99;
   }
   class A{
     public:
      A(int j=0){k=j;}
      int k;
     };
   void three(A &a, A b,A*c){
     b.k=32;
     a=b;
     c=&b;
     c->k=33;
   }
   int main(){
     int x,y,z;
     x=y=z=2;
     one(x,y,z);
     cout<<x<<" "<<y<<" "<<z<<endl;
   //OUTPUT:-------------------------->  2 2 6
     x=2;
     one(x,x,x);
     cout<<x<<endl;
    //OUTPUT:-------------------------->  6
     int *u,*v;
     x=2;
     y=3;
     u=&x;
     v=&y;
     two(u,v);
     cout<<*u<<" "<<*v<<endl;
     //OUTPUT:-------------------------->  88 88
     A s,t,*r;
     r=new A();
     three(s,t,r);
     cout<<s.k<<" "<<t.k<<" "<<r->k<<endl;
   //OUTPUT:-------------------------->    32 0 0
   }
2. Consider the ADT for the color of a pixel, which is specified by the
amount of Red, Green, and Blue (the RGB scale), each of which ranges from
0 to 255.  Thus black is represented by (0,0,0) and white by (255,255,255).
Write the declaration and definitions for the class RGB sufficient for the
code below to compile and produce the indicated output. You may assume you
have available the code for the function
check(bool,const char*,int, const char*). Read question 3 before proceeding.
  RGB a(100,80,40), b(a),c,d(a);
  b.set(b.R,50).set(b.G,99);
  cout<<a<<b<<c<<endl;  // RGB(100,80,40)RGB(50,99,40)RGB(0,0,0)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
class RGB{
protected:
  int r,g,b;
public:
  RGB(int u=0,int s=0,int t=0);
  RGB(const RGB &t);
  static const int R=0,G=1,B=2;
  RGB & set(int p,int val);
  friend ostream & operator<<(ostream &out,const RGB&t);
};

RGB::RGB(int u, int s, int t):r(u),g(s),b(t){
  check(u>=0 && u<=255 && s>=0 && s<=255 && t>=0 && t<=255,
    "pixel color out of range [0,255]",__LINE__,__FILE__);
}
RGB::RGB(const RGB & t):r(t.r),g(t.g),b(t.b){}
RGB & RGB::set(int p, int val){
  check(p>=0 && p<=2,"Bad color",__LINE__,__FILE__);
  check(val>=0 && val<=255,"Bad pixel value",__LINE__,__FILE__);
  switch(p){
  case R: r=val; break;
  case G: g=val; break;
  case B: b=val; break;
  }
  return *this;
}
ostream & operator<<(ostream &out,const RGB &t){
  return out<<"RGB("<<t.r<<","<<t.g<<","<<t.b<<")";
}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3. Now consider the ADT for a pixel, which includes the notion of location
and color. The location is indicated by a pair of ints giving the horizontal
and vertical  ("X" and "Y") locations of the pixel on the computer screen.
Write the declaration and definitions for the class Pixel that is a subclass
of class RGB so that the code below compiles and produces the indicated output.
You may assume you have available the code for the function
check(bool,const char*,int,const char*).
   Pixel a(5,10,70,80,90),b(15,10,55,80,90),c(15,10,70,80,90);
   a.set(a.X,15).set(a.R,55);
   cout<<a<<endl; //  Pixel((15,10),RGB(55,80,90))
   cout<<(a==b)<<(a==c)<<endl; //10
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
class Pixel: public RGB{
private:
  int x,y;
public:
  static const int X=3,Y=4;
  Pixel(int f=0,int g=0,int h=0, int i=0, int j=0);
  Pixel& set(int p,int q);
  bool operator==(const Pixel&p)const;
  friend ostream & operator<<(ostream&out,const Pixel &p);
};
Pixel::Pixel(int f, int g, int h, int i, int j):RGB(h,i,j),x(f),y(g){
 check(f>=0 && g>=0,"Cannot have negative pixel value",__LINE__,__FILE__);
 }
Pixel & Pixel::set(int p, int q){
  if(p<X || p>Y)
    RGB::set(p,q);
  else{
   check(q>=0,"Cannot have negative pixel value",__LINE__,__FILE__);
   if(p==X)
    x=q;
  else
    y=q;
  }
  return *this;
}
ostream & operator<<(ostream &out,const Pixel &p){
  return out<<"Pixel(("<<p.x<<","<<p.y<<"),"<<(RGB)p<<")";
}
bool Pixel::operator==(const Pixel&p)const{
  return r==p.r && g==p.g && b==p.b && x==p.x && y==p.y;
}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4. Java arrays are superior to C++ arrays, because they restrict the range of
the index to the actual array and because they provide information about
their length. Write the declaration and definitions for the class JavaRay,
which implements the ADT for the way arrays of doubles are implemented in
Java. Your implementation need only be sufficient to enable the code below to
compile and produce the output shown. You can assume you have available the
code for the function check(bool,const char*,int,const char*).
  JavaRay x(10),  //an array of 10 ints
          z(800); // an array of 800 ints
  for(int j=0;j<5;j++)
      x[j]=j*j;
  cout<<"The array has capacity for "<<x.length()<<" ints\n";
     // The array has capacity for 10 ints
  JavaRay y(x);
  cout<<"The value of y[2] = "<<y[2]<<endl; // The value of y[2] = 4
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
class JavaRay{
private:
  int *ray,size;
public:
  JavaRay(int j);
  int & operator[](int index);
  int length()const;
};
JavaRay::JavaRay(int j):size(j){
  check(j>0,"Bad size of array",__LINE__,__FILE__);
  ray=new int[size];
  check(ray!=NULL,"Heap Overflow",__LINE__,__FILE__);
}
int & JavaRay::operator[](int index){
  check(index>=0 && index<size,"Range error",__LINE__,__FILE__);
  return ray[index];
}
int JavaRay::length()const{return size;}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
