COSC 2011 3.0 Fundamentals of Data Structures

Assignment 2

  1. Consider the following class definition.
    class X {
        int x = 0;
        int y = 1;
    
        int set(int x, int y) {
            this.x = x;
            this.y = y;
            x = 1;
            y = 0;
            return x;
        }
    
        public static void main(String[] args) { 
            X obj = new X();
            int v = 3;
            int w = 4;
            v = obj.set(v, w);
            /* this point */
        }
    }
    
    What are the values of obj's x and y, and the local v and w at /* this point */?

  2. Complete the class definition
    class Body {
        int id;                         // its unique identifier
        String name;                    // its name
    
        /* instance variable orbits refers to the Body around which it orbits */
     
        static int nextId = 0;          // next identifier to use
     
        Body() {}                       // default constructor
     
        /* constructor Body(name, orbits) initializes a new Body object
           by setting its unique identifier to the next identifier to use,
           and initializing its name and the Body around which it orbits */
     
        /* instance method getId returns its unique identifier */
     
        /* instance method putName(name) sets its name */
     
        /* class method numberOfBodies returns the number of Body objects 
           created so far */
    }
    
    by
    1. declaring the instance variable orbits,
    2. defining the constructor Body(name, orbits),
    3. defining the instance method getId(),
    4. defining the instance method putName(name) and
    5. defining the class method numberOfBodies.

  3. Consider the following class definitions.
    class Point {
        int x;                     // its x-coordinate
        int y;                     // its y-coordinate
     
        private static int n = 0;  // number of Points created
     
        Point(int x, int y) {
            this.x = x;
            this.y = y;
            n++;
        }
     
        static int numberOfPointsCreated() {
            return n;
        }
    }
    
    class Color {
        String name;    // its name
        int intensity;  // its intensity
     
        Color(String name, int intensity) {
            this.name = name;
            this.intensity = intensity;
        }
    }
    
    Complete the class definition
    class ColoredCircle {
        Point origin; // its origin
        int radius;   // its radius
        Color color;  // its color
     
        ColoredCircle(Point origin, int radius, Color color) {
            this.origin = origin;
            this.radius = radius;
            this.color = color;
        }
     
        ColoredCircle setOrigin(Point origin) { 
            this.origin = origin;
            return this;
        }
     
        public static void main(String[] args) {
            
            /* create a ColoredCircle object named myColoredCircle
               with x-coordinate 5, y-coordinate 6, radius 7,
               color red and color intensity 8 */
     
            int myRadius;
     
            /* initialize myRadius to myColoredCircle's radius */
     
            Point newOrigin;
     
            /* instantiate and initialize Point object named
               newOrigin with x-coordinate 9 and y-coordinate 10 */
     
            /* change myColoredCircle's origin to newOrigin using
               setOrigin */
     
            int numberOfPointsCreated;
     
            /* initialize numberOfPointsCreated to the number of Points 
               created so far using numberOfPointsCreated */
        }
    }
    
    by
    1. creating a ColoredCircle object named myColoredCircle with x-coordinate 5, y-coordinate 6, radius 7, color red and color intensity 8,
    2. initializing myRadius to myColoredCircle's radius,
    3. instantiating and initializing Point object named newOrigin with x-coordinate 9 and y-coordinate 10,
    4. changing myColoredCircle's origin to newOrigin using setOrigin and
    5. initializing numberOfPointsCreated to the number of Points created so far using numberOfPointsCreated.