Exercises

  1. What are the four attributes that distinguish object-oriented programming? What are some advantages and disadvantages of OOP?

  2. Give three examples of primitive types and three examples of predefined Java classes (i.e., object types).

  3. What is the default constructor, and when is it called? What is a no-arg constructor?

  4. Describe overriding, and write some code to show an example.

  5. Consider the following three related classes:

    class Mammal {}
    class Dog extends Mammal { }
    class Cat extends Mammal { }

    There are these variables of each class:

    Mammal m;
    Dog d = new Dog( );
    Cat c = new Cat( );

    Which of these statements will cause an error at compile time, and why? Which of these statements may cause an error at run-time, and why?

    m = d;       // 1.
    d = m;       // 2.
    d = (Dog) m; // 3.
    d = c;       // 4.
    d = (Dog) c; // 5.

  6. Create a class that provides all the same methods as java.lang.Math, but which operate on degrees, not radians. Do this by creating a wrapper for each method in Math, in your class. Recall that a wrapper is a thin layer around a type or a method call to make some convenient adaptation, then call the underlying version.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.242.235