Wednesday, July 29, 2015

ques set

Workbook on Constructors
Video 1
The following four questions are concerned with the following program.                               
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Car {
    String name;

        public Car() {
                name = "My Car";
        }
}

class SampleProgram {
        public static void main(String args[]) {
                Car car;
                car = new Car();
                System.out.println(car.name);
        }
}


1.       The output of this program is _____________________.

2.       Which line represents the creation of the object inside the main method?
a.        9           b.  11            c. 12             d. 13
3.       Which line represents an object declaration inside the main method?
a.        9           b.  11           c. 12              d. 13
4.       Lines 11 and 12 can be replaced by a single line as _____________________.
5.       Object creation in java utilizes the keyword.
a.        new
b.       Car()
c.        SampleProgram
d.       class
6.       How does object declaration differ from object creation?
a.        In object declaration space is allocated in memory for the object, while in object creation, the identifier of the object is set to refer to a space in the memory.
b.       In object declaration, an identifier is created, while in object creation, the actual object is created.
c.        In object declaration, an identifier of an object is set to refer to a space in memory, and in object creation, the space in memory is actually created for the object.
d.       Other than the fact that the syntax of the two operations differs, they perform the same function of creating the object.

Video 2
7.       Which of the following shows the syntax for the object creation for a defined object?
a.        < object reference name > = new <arguments>(<class name>);
b.       <class name> = new <object name>(<arguments>);
c.        <object reference name> = <class name> <identifier>;
d.       < object reference name > = new <class name>(<arguments>);               

8.       Assume getAge() is a method of the Person class and katie is a Person. Which of the following represents a valid method call?
a.        Katie.getAge(Person);
b.       katie = Person.getAge();
c.        katie.getAge();
d.       Person = katie.getAge();
9.       What is the output?
class Student{
                String name;
                public void Student(String name){
                                name = name;
}
public static void main(String args[]){
                Student stu = new Student(“Ram”);
                System.out.println(stu.name);
}
}

Video 3
10.    What is the output?

class employee{
                int id;
                public void disp(){
                                System.out.println(id);
}
public static void main(String args[]){
                Employee emp = new Employee();
                emp.disp();
}
}

11.    Create a class Rectangle. The class has attributes x1,y1,x2,y2 where (x1,y1) is the bottom-left corner and (x2,y2) is the top-right corner. Write two constructors, one to take x1,y1,x2,y2 as arguments and the other to take width and height (in this case, (0,0) will be the bottom-left corner.).  It should have methods to: calculate the perimeter and the area of the rectangle, move the rectangle by deltax and deltay, find out if a point is inside the rectangle or not. It should also have methods to get the union and intersection of two rectangles. Write a drive program to test your class.


//Rectangle.java
/**
 * This class represents a rectangle.  Its fields represent the coordinates
 * of the corners of the rectangle.  Its methods define operations that can
 * be performed on Rectangle objects.
 **/
public class Rectangle {
    // These are the data fields of the class
    public int x1, y1, x2, y2;

    /**
     * The is the main constructor for the class.  It simply uses its arguments
     * to initialize each of the fields of the new object.  Note that it has
     * the same name as the class, and that it has no return value declared in
     * its signature.
     **/
    public Rectangle(int x1, int y1, int x2, int y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

    /**
     * This is another constructor.  It takes width and height (in this case, (0,0) will be the bottom-left corner.)
     **/
    public Rectangle(int width, int height) {
 this(0, 0, width, height);

}
   
    /** This is yet another constructor. */
    public Rectangle( ) { this(0, 0, 0, 0); }

    /** Move the rectangle by the specified amounts */
    public void move(int deltax, int deltay) {
        x1 += deltax; x2 += deltax;
        y1 += deltay; y2 += deltay;
  
 }

    /** Test whether the specified point is inside the rectangle */
    public boolean isInside(int x, int y) {
        return ((x >= x1)&& (x <= x2)&& (y >= y1)&& (y <= y2));
    }

/**
     * Return the union of this rectangle with another.  I.e. return the
     * smallest rectangle that includes them both.
     **/
    public Rectangle union(Rectangle r) {
        return new Rectangle((this.x1 < r.x1) ? this.x1 : r.x1,
                        (this.y1 < r.y1) ? this.y1 : r.y1,
                        (this.x2 > r.x2) ? this.x2 : r.x2,
                        (this.y2 > r.y2) ? this.y2 : r.y2);
    }
   
    /**
     * Return the intersection of this rectangle with another.
     * I.e. return their overlap.
     **/
    public Rectangle intersection(Rectangle r) {

Rectangle result =  new Rectangle ((this.x1 > r.x1) ? this.x1 : r.x1,
                                (this.y1 > r.y1) ? this.y1 : r.y1,
                                (this.x2 < r.x2) ? this.x2 : r.x2,
                                (this.y2 < r.y2) ? this.y2 : r.y2);
        if (result.x1 > result.x2) { result.x1 = result.x2 = 0; }
        if (result.y1 > result.y2) { result.y1 = result.y2 = 0; }
        return result;
    }

    /**
     * This is a method of our superclass, Object.  We override it so that
     * Rectangle objects can be meaningfully converted to strings, can be
     * concatenated to strings with the + operator, and can be passed to
     * methods like System.out.println( )
     **/
    public String toString( ) {
        return "[" + x1 + "," + y1 + "; " + x2 + "," + y2 + "]";
    }
}


// RectTest.java
/** This class demonstrates how you might use the Rectangle class */
public class RectTest {
    public static void main(String[  ] args) {

Rectangle rect = new Rectangle(1, 3, 6, 8); // Create Rectangle objects

                                int choice = Integer.parseInt(args[0]);
                                if (choice == 1) {
                                                int delX;
                                                int delY;
                                                // move the rectangle rect by delx and dely
                                               


                                                System.out.println(rect);
                                } else if (choice == 2) {
                                                int x;
                                                int y;
                                                // check if x , y lies inside the rectangle rect
                                               







                                } else if (choice == 3) {
                                // create a rectangle object named rect3 and find its union with rect
                                                int x1;
                                                int y1;
                                                int x2;
                                                int y2;
                                               






                                                System.out.println(rect3);

                                } else if (choice == 4) {
                                // create a rectangle object named rect3 with height 5, and width 6 and
// find its intersection with rect

                                                int width;
                                                int height;
 







                                                System.out.println(rect3);
                                } else {
                                                System.out.println("Invalid choice");
                                }
                }

}