Exercise (Solutions)

E1

For a bookstore as described in the lesson, create Customer class. Every customer is identified by their name, lastName, gender and customerId which is a long number. Create a class to represent these customer information. Ensure you use the principles of encapsulation. Create a BookStore class and instantiate the Customer class and set values for two customers.

E2

Extend the above problem, by creating OnlineCustomer and InStoreCustomer. OnlineCustomer should have an emailId field, and InStoreCustomer should have a mailing address. Instantiate these two classes in BookStore class and set some imaginary customer values.

E3

You get compilation error on this program.

public class SomethingIsWrong {
   public static void main(String[] args) {
       Rectangle myRect;
       myRect.width = 40;
       myRect.height = 50;
       System.out.println("myRect's area is " + myRect.area());
   }
}

class Rectangle{
    int width;
    int height;
    public int area(){
        return this.width * this.height;
    }
}

Why are you not able to compile? How do you fix it?

results matching ""

    No results matching ""